/** * Get a standard attribute * * @param req The request * @param att The attribute * @return The value */ @SuppressWarnings("unchecked") private static<T> T getRequestAttribute(HttpServletRequest req, Attribute att) { return (T)req.getAttribute(att.getId()); } }
/** * Set a standard request attribute. * * @param req The request * @param att The attribute * @param value The value */ private static<T> void setRequestAttribute(HttpServletRequest req, Attribute att, T value) { req.setAttribute(att.getId(), value); }
/** * Get a standard attribute * * @param req The request * @param att The attribute * @return The value */ @SuppressWarnings("unchecked") private static<T> T getRequestAttribute(HttpServletRequest req, Attribute att) { return (T)req.getAttribute(att.getId()); } }
@Test public void testDoOAuthRequest() throws Exception { // Doesn't actually do oauth dance since it returns the standard fetcher. // OAuth tests are in OAuthRequestTest expectGetAndReturnBody(AuthType.OAUTH, RESPONSE_BODY); FakeGadgetToken authToken = new FakeGadgetToken() .setUpdatedToken("updated") .setAppUrl(DUMMY_TOKEN.getAppUrl()) .setContainer(DUMMY_TOKEN.getContainer()); expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(authToken).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.OAUTH.toString()).atLeastOnce(); // This isn't terribly accurate, but is close enough for this test. expect(request.getParameterMap()).andStubReturn(Collections.emptyMap()); expectParameters(request); replay(); handler.fetch(request, recorder); JSONObject results = extractJsonFromResponse(); assertEquals(HttpResponse.SC_OK, results.getInt("rc")); assertEquals(RESPONSE_BODY, results.get("body")); assertTrue(rewriter.responseWasRewritten()); }
@Test public void testChangeSecurityToken() throws Exception { // Doesn't actually sign since it returns the standard fetcher. // Signing tests are in SigningFetcherTest expectGetAndReturnBody(AuthType.SIGNED, RESPONSE_BODY); FakeGadgetToken authToken = new FakeGadgetToken() .setUpdatedToken("updated") .setAppUrl(DUMMY_TOKEN.getAppUrl()) .setContainer(DUMMY_TOKEN.getContainer()); expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(authToken).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); expectParameters(request, MakeRequestHandler.AUTHZ_PARAM); replay(); handler.fetch(request, recorder); JSONObject results = extractJsonFromResponse(); assertEquals(RESPONSE_BODY, results.get("body")); assertEquals("updated", results.getString("st")); assertTrue(rewriter.responseWasRewritten()); }
@Test public void testChangeSecurityToken() throws Exception { // Doesn't actually sign since it returns the standard fetcher. // Signing tests are in SigningFetcherTest expectGetAndReturnBody(AuthType.SIGNED, RESPONSE_BODY); FakeGadgetToken authToken = new FakeGadgetToken() .setUpdatedToken("updated") .setAppUrl(DUMMY_TOKEN.getAppUrl()) .setContainer(DUMMY_TOKEN.getContainer()); expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(authToken).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); expectParameters(request, MakeRequestHandler.AUTHZ_PARAM); replay(); handler.fetch(request, recorder); JSONObject results = extractJsonFromResponse(); assertEquals(RESPONSE_BODY, results.get("body")); assertEquals("updated", results.getString("st")); assertTrue(rewriter.responseWasRewritten()); }
@Test public void testSignedPostRequest() throws Exception { // Doesn't actually sign since it returns the standard fetcher. // Signing tests are in SigningFetcherTest expectPostAndReturnBody(AuthType.SIGNED, REQUEST_BODY, RESPONSE_BODY); expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(DUMMY_TOKEN).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); expectParameters(request, MakeRequestHandler.METHOD_PARAM, MakeRequestHandler.POST_DATA_PARAM, MakeRequestHandler.AUTHZ_PARAM); replay(); handler.fetch(request, recorder); JSONObject results = extractJsonFromResponse(); assertEquals(RESPONSE_BODY, results.get("body")); assertFalse("A security token was returned when it was not requested.", results.has("st")); assertTrue(rewriter.responseWasRewritten()); }
@Test public void testSignedPostRequest() throws Exception { // Doesn't actually sign since it returns the standard fetcher. // Signing tests are in SigningFetcherTest expectPostAndReturnBody(AuthType.SIGNED, REQUEST_BODY, RESPONSE_BODY); expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(DUMMY_TOKEN).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); expectParameters(request, MakeRequestHandler.METHOD_PARAM, MakeRequestHandler.POST_DATA_PARAM, MakeRequestHandler.AUTHZ_PARAM); replay(); handler.fetch(request, recorder); JSONObject results = extractJsonFromResponse(); assertEquals(RESPONSE_BODY, results.get("body")); assertFalse("A security token was returned when it was not requested.", results.has("st")); assertTrue(rewriter.responseWasRewritten()); }
@Test public void testSignedGetRequest() throws Exception { expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(DUMMY_TOKEN).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); HttpRequest expected = new HttpRequest(REQUEST_URL) .setAuthType(AuthType.SIGNED); expect(pipeline.execute(expected)) .andReturn(new HttpResponse(RESPONSE_BODY)); expectParameters(request, MakeRequestHandler.AUTHZ_PARAM); replay(); handler.fetch(request, recorder); JSONObject results = extractJsonFromResponse(); assertEquals(RESPONSE_BODY, results.get("body")); assertTrue(rewriter.responseWasRewritten()); }
@Test public void testSignedGetRequest() throws Exception { expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(DUMMY_TOKEN).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); HttpRequest expected = new HttpRequest(REQUEST_URL) .setAuthType(AuthType.SIGNED); expect(pipeline.execute(expected)) .andReturn(new HttpResponse(RESPONSE_BODY)); expectParameters(request, MakeRequestHandler.AUTHZ_PARAM); replay(); handler.fetch(request, recorder); JSONObject results = extractJsonFromResponse(); assertEquals(RESPONSE_BODY, results.get("body")); assertTrue(rewriter.responseWasRewritten()); }
private void setupRequest(String pathInfo, String actualMethod, String overrideMethod) throws IOException { FakeHttpServletRequest fakeReq = new FakeHttpServletRequest("/social/rest", pathInfo, ""); fakeReq.setPathInfo(pathInfo); fakeReq.setParameter(DataServiceServlet.X_HTTP_METHOD_OVERRIDE, overrideMethod); fakeReq.setCharacterEncoding("UTF-8"); if (!("GET").equals(actualMethod) && !("HEAD").equals(actualMethod)) { fakeReq.setPostData("", "UTF-8"); } fakeReq.setMethod(actualMethod); fakeReq.setAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); fakeReq.setContentType(ContentTypes.OUTPUT_JSON_CONTENT_TYPE); req = fakeReq; }
private void setupRequest(String pathInfo, String actualMethod, String overrideMethod) throws IOException { FakeHttpServletRequest fakeReq = new FakeHttpServletRequest("/social/rest", pathInfo, ""); fakeReq.setPathInfo(pathInfo); fakeReq.setParameter(DataServiceServlet.X_HTTP_METHOD_OVERRIDE, overrideMethod); fakeReq.setCharacterEncoding("UTF-8"); if (!("GET").equals(actualMethod) && !("HEAD").equals(actualMethod)) { fakeReq.setPostData("", "UTF-8"); } fakeReq.setMethod(actualMethod); fakeReq.setAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); fakeReq.setContentType(ContentTypes.OUTPUT_JSON_CONTENT_TYPE); req = fakeReq; }
private void setupRequest(String pathInfo, String actualMethod, String overrideMethod) throws IOException { FakeHttpServletRequest fakeReq = new FakeHttpServletRequest("/social/rest", pathInfo, ""); fakeReq.setPathInfo(pathInfo); fakeReq.setParameter(DataServiceServlet.X_HTTP_METHOD_OVERRIDE, overrideMethod); fakeReq.setCharacterEncoding("UTF-8"); if (!("GET").equals(actualMethod) && !("HEAD").equals(actualMethod)) { fakeReq.setPostData("", "UTF-8"); } fakeReq.setMethod(actualMethod); fakeReq.setAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId(), FAKE_GADGET_TOKEN); fakeReq.setContentType(ContentTypes.OUTPUT_JSON_CONTENT_TYPE); req = fakeReq; }
/** * Set a standard request attribute. * * @param req The request * @param att The attribute * @param value The value */ private static<T> void setRequestAttribute(HttpServletRequest req, Attribute att, T value) { req.setAttribute(att.getId(), value); }
/** * Get a standard attribute * * @param req The request * @param att The attribute * @return The value */ @SuppressWarnings("unchecked") private static<T> T getRequestAttribute(HttpServletRequest req, Attribute att) { return (T)req.getAttribute(att.getId()); } }
/** * Set a standard request attribute. * * @param req The request * @param att The attribute * @param value The value */ private static<T> void setRequestAttribute(HttpServletRequest req, Attribute att, T value) { req.setAttribute(att.getId(), value); }
@Test public void testGetSecurityToken() throws Exception { SecurityToken expected = new AnonymousSecurityToken(); expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())).andReturn(expected); replay(); GadgetContext context = new HttpGadgetContext(request); assertEquals(expected, context.getToken()); }
@Test(expected=GadgetException.class) public void testBadSecurityTokenThrows() throws Exception { expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(null).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); replay(); handler.fetch(request, recorder); }
@Test public void testGetSecurityToken() throws Exception { SecurityToken expected = new AnonymousSecurityToken(); expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())).andReturn(expected); replay(); GadgetContext context = new HttpGadgetContext(request); assertEquals(expected, context.getToken()); }
@Test(expected=GadgetException.class) public void testBadSecurityTokenThrows() throws Exception { expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(null).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); replay(); handler.fetch(request, recorder); }