@Override public String getHash(HttpResponse resp) { return resp.getResponseAsString(); } };
public void output(Uri uri, HttpResponse resp) throws IOException { if (resp.getHttpStatusCode() != HttpServletResponse.SC_OK) { println(formatHttpError(resp.getHttpStatusCode(), resp.getResponseAsString(), uri)); } else { outputJs(uri, resp.getResponseAsString()); } }
@Test public void addResponseWithNoCachingHeaders() { HttpRequest request = new HttpRequest(DEFAULT_URI); String key = cache.createKey(request); HttpResponse response = new HttpResponse("no headers"); assertNotNull(cache.addResponse(request, response)); assertEquals("no headers", cache.map.get(key).getResponseAsString()); assertNotNull(extendedStrictNoCacheTtlCache.addResponse(request, response)); assertEquals("no headers", extendedStrictNoCacheTtlCache.map.get(key).getResponseAsString()); }
@Test public void setResponseStringWithContentType() { HttpResponse resp = new HttpResponseBuilder() .addHeader("Content-Type", "text/html") .setResponseString("foo") .create(); Multimap<String, String> headers = resp.getHeaders(); assertEquals("text/html; charset=UTF-8", headers.get("Content-Type").iterator().next()); assertEquals("foo", resp.getResponseAsString()); }
@Test public void setResponseStringWithContentType() { HttpResponse resp = new HttpResponseBuilder() .addHeader("Content-Type", "text/html") .setResponseString("foo") .create(); Multimap<String, String> headers = resp.getHeaders(); assertEquals("text/html; charset=UTF-8", headers.get("Content-Type").iterator().next()); assertEquals("foo", resp.getResponseAsString()); }
@Test public void testExtraParamsSuppressed() throws Exception { serviceProvider.setRejectExtraParams(true); MakeRequestClient client = makeStrictNonSocialClient("owner", "owner", GADGET_URL); HttpResponse response = client.sendGet(FakeOAuthServiceProvider.RESOURCE_URL); assertEquals("", response.getResponseAsString()); client.approveToken("user_data=hello-oauth"); response = client.sendGet(FakeOAuthServiceProvider.RESOURCE_URL); assertEquals("User data is hello-oauth", response.getResponseAsString()); }
@Test public void testGetWithRawBody() throws Exception { MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendGetWithBody(FakeOAuthServiceProvider.RESOURCE_URL, "application/json", "war=peace&yes=no".getBytes()); assertEquals("war=peace&yes=no", resp.getHeader(FakeOAuthServiceProvider.BODY_ECHO_HEADER)); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); checkContains(queryParams, "oauth_body_hash", "MfhwxPN6ns5CwQAZN9OcJXu3Jv4="); }
@Test public void testGetWithRawBody() throws Exception { MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendGetWithBody(FakeOAuthServiceProvider.RESOURCE_URL, "application/json", "war=peace&yes=no".getBytes()); assertEquals("war=peace&yes=no", resp.getHeader(FakeOAuthServiceProvider.BODY_ECHO_HEADER)); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); checkContains(queryParams, "oauth_body_hash", "MfhwxPN6ns5CwQAZN9OcJXu3Jv4="); }
@Test public void addResponseWithNoCachingHeaders() { HttpRequest request = new HttpRequest(DEFAULT_URI); String key = cache.createKey(request); HttpResponse response = new HttpResponse("no headers"); assertTrue(cache.addResponse(request, response)); assertEquals("no headers", cache.map.get(key).getResponseAsString()); }
@Test public void testEncodingDetectionLatin1NoCharsetSpecified() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Content-Type", "text/plain;") .setResponse(LATIN1_DATA) .create(); assertEquals("ISO-8859-1", response.getEncoding().toUpperCase()); assertEquals(LATIN1_STRING, response.getResponseAsString()); }
@Test public void testRewriteInvalidImageContentWithValidFileExtn() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_BOGUS, "This is not an image".getBytes()); rewriter.rewrite(new HttpRequest(Uri.parse("some.png")), response, null); assertEquals(HttpResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getHttpStatusCode()); assertEquals(CONTENT_TYPE_AND_EXTENSION_MISMATCH, response.create().getResponseAsString()); }
@Test public void testPut_noBody() throws Exception { HttpRequest request = new HttpRequest(BASE_URL).setMethod("PUT"); HttpResponse response = fetcher.fetch(request); assertEquals("PUT", response.getHeader("x-method")); assertEquals("", response.getResponseAsString()); }
@Test public void testLockedDomainEmbed() throws Exception { setupNoArgsProxyRequestMock("www.example.com", URL_ONE); expectGetAndReturnData(URL_ONE, DATA_ONE.getBytes()); setupGadgetAdminMock(true); replay(); HttpResponse response = proxyHandler.fetch(request); verify(); assertEquals(DATA_ONE, response.getResponseAsString()); assertTrue(rewriter.responseWasRewritten()); }
@Test public void testPostWithQueryNoData() throws Exception { MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendFormPost( FakeOAuthServiceProvider.RESOURCE_URL + "?name=value", null); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); assertTrue(contains(queryParams, "name", "value")); assertEquals("", resp.getHeader(FakeOAuthServiceProvider.BODY_ECHO_HEADER)); }
@Test public void testPostNoQueryWithData() throws Exception { MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendFormPost( FakeOAuthServiceProvider.RESOURCE_URL, "name=value"); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); assertFalse(contains(queryParams, "name", "value")); assertEquals("name=value", resp.getHeader(FakeOAuthServiceProvider.BODY_ECHO_HEADER)); }
@Test public void testHttp404() throws Exception { String content = "Hello, world!"; Uri uri = new UriBuilder(BASE_URL) .addQueryParameter("body", content) .addQueryParameter("status", "404") .toUri(); HttpRequest request = new HttpRequest(uri); HttpResponse response = fetcher.fetch(request); assertEquals(404, response.getHttpStatusCode()); assertEquals(content, response.getResponseAsString()); }
@Test public void testHttp404() throws Exception { String content = "Hello, world!"; Uri uri = new UriBuilder(BASE_URL) .addQueryParameter("body", content) .addQueryParameter("status", "404") .toUri(); HttpRequest request = new HttpRequest(uri); HttpResponse response = fetcher.fetch(request); assertEquals(404, response.getHttpStatusCode()); assertEquals(content, response.getResponseAsString()); }
@Test public void testFollowRelativeRedirects() throws Exception { String content = ""; Uri uri = new UriBuilder(BASE_URL) .addQueryParameter("body", content) .addQueryParameter("status", "302") .addQueryParameter("header", "Location=/?body=redirected") .toUri(); HttpRequest request = new HttpRequest(uri); HttpResponse response = fetcher.fetch(request); assertEquals(200, response.getHttpStatusCode()); assertEquals("redirected", response.getResponseAsString()); }
@Test public void testWithProxy() throws Exception { String content = "Hello, Gagan!"; Uri uri = new UriBuilder(Uri.parse("http://www.google.com/search")) .addQueryParameter("body", content) .addQueryParameter("status", "201") .toUri(); HttpRequest request = new HttpRequest(uri); HttpResponse response = fetcher.fetch(request); assertEquals(201, response.getHttpStatusCode()); assertEquals(content, response.getResponseAsString()); }
@Test public void testFollowRedirects() throws Exception { String content = ""; Uri uri = new UriBuilder(BASE_URL) .addQueryParameter("body", content) .addQueryParameter("status", "302") .addQueryParameter("header", "Location=" + BASE_URL.toString() + "?body=redirected") .toUri(); HttpRequest request = new HttpRequest(uri); HttpResponse response = fetcher.fetch(request); assertEquals(200, response.getHttpStatusCode()); assertEquals("redirected", response.getResponseAsString()); }