private HttpResponse sendErrorResponse(final Throwable t, final OAuth2Error error, final String contextMessage, final String errorUri, final String errorDescription) { final HttpResponseBuilder responseBuilder = this.getErrorResponseBuilder(t, error, contextMessage, errorUri, errorDescription); return responseBuilder.create(); }
@Test public void noModsReturnsSameResponseBaseCtor() { HttpResponse response = new HttpResponse("foo"); HttpResponseBuilder builder = new HttpResponseBuilder(response); assertSame(response, builder.create()); builder.setHttpStatusCode(HttpResponse.SC_BAD_GATEWAY); HttpResponse newResponse = builder.create(); assertNotSame(response, newResponse); assertSame(newResponse, builder.create()); } }
private void expectGetAndReturnData(String url, byte[] data) throws Exception { HttpRequest req = new HttpRequest(Uri.parse(url)); HttpResponse resp = new HttpResponseBuilder().setResponse(data).create(); expect(pipeline.execute(req)).andReturn(resp); }
private HttpResponse handleEchoUrl(HttpRequest request) throws Exception { String query = request.getUri().getQuery(); if (query.contains("add_oauth_token")) { query = query + "&oauth_token=abc"; } return new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_OK) .setResponseString(query) .create(); }
@Test public void testNullHeaderNamesStripped() { HttpResponse response = new HttpResponseBuilder() .addHeader(null, "dummy") .create(); for (String key : response.getHeaders().keySet()) { assertNotNull("Null header not removed.", key); } }
@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 testHashCodeEmpty() { HttpResponse local = new HttpResponseBuilder() .setHttpStatusCode(200) .create(); verifyHash(local, 1, "qgeopmcf02p09qc016cepu22fo"); }
@Test public void getResponseNotCacheable() { HttpRequest request = new HttpRequest(DEFAULT_URI); String key = cache.createKey(request); HttpResponse response = new HttpResponseBuilder().setStrictNoCache().create(); cache.map.put(key, response); assertNull("Did not return null when response was uncacheable", cache.getResponse(request)); extendedStrictNoCacheTtlCache.map.put(key, response); assertEquals(response, extendedStrictNoCacheTtlCache.getResponse(request)); }
@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 testCacheExpirationForStrictNoCacheResponse() throws Exception { assertEquals(-1, new HttpResponseBuilder() .setStrictNoCache() .setRefetchStrictNoCacheAfterMs(10000) .create() .getCacheExpiration()); }
@Test public void testJs() throws Exception { Gadget gadget = mockGadget(); control.replay(); HttpRequest req = new HttpRequest(CONTENT_URI); req.setRewriteMimeType("text/javascript"); req.setCajaRequested(true); HttpResponse response = new HttpResponseBuilder().setResponseString("var a;").create(); String sanitized = "___.di(IMPORTS___,'a');"; assertTrue(rewrite(req, response).contains(sanitized)); assertTrue(rewrite(req, response, gadget).contains(sanitized)); }
@Test public void testJsWithoutCaja() throws Exception { Gadget gadget = mockGadget(); control.replay(); HttpRequest req = new HttpRequest(CONTENT_URI); req.setRewriteMimeType("text/javascript"); req.setCajaRequested(false); HttpResponse response = new HttpResponseBuilder().setResponseString("var a;").create(); String sanitized = "var a;"; assertTrue(rewrite(req, response).contains(sanitized)); assertTrue(rewrite(req, response, gadget).contains(sanitized)); }
@Test public void testHeaders() throws Exception { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(999) .setHeader("Set-Cookie", "cookie") .setHeader("location", "here") .create(); Map<String, Object> obj = FetchResponseUtils.getResponseAsJson(response, "key", "body", false); JsonAssert.assertObjectEquals( "{rc:999,id:'key',body:'body',headers:{set-cookie:['cookie'],location:['here']}}", obj); }
@Test public void testImgWithPragmaNoCacheReservedAndFetchNotTriggered() throws Exception { cache.addResponse(new HttpRequest(Uri.parse(IMG_URL)), new HttpResponseBuilder().setResponseString("test") .addHeader("Pragma", "no-cache").create()); checkVisitBypassedAndFetchTriggered("img", IMG_URL, false, false); }
@Test public void testStrictCacheControlNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testShouldRefetchForStrictNoCacheResponsePastShouldNotRefetch() throws Exception { assertEquals(false, new HttpResponseBuilder().setStrictNoCache() .setHeader("Date", DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 5000)) .setRefetchStrictNoCacheAfterMs(10000) .create() .shouldRefetch()); }
@Test public void testMaxAgeWithDate() throws Exception { int maxAge = 10; int now = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Date", DateUtil.formatRfc1123Date(1000L * now)) .addHeader("Cache-Control", "public, max-age=" + maxAge) .create(); assertEquals(now + maxAge, roundToSeconds(response.getCacheExpiration())); assertTtlOk(maxAge, response); }
@Test public void testSetNoCache() { int time = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Expires", DateUtil.formatRfc1123Date(1000L * time)) .setStrictNoCache() .create(); assertNull(response.getHeader("Expires")); assertEquals("no-cache", response.getHeader("Pragma")); assertEquals("no-cache", response.getHeader("Cache-Control")); }
@Test public void testStrictPragmaJunk() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "junk") .create(); assertFalse(response.isStrictNoCache()); int expected = roundToSeconds(timeSource.currentTimeMillis() + response.getDefaultTtl()); int expires = roundToSeconds(response.getCacheExpiration()); assertEquals(expected, expires); assertTrue(response.getCacheTtl() <= response.getDefaultTtl() && response.getCacheTtl() > 0); }
@Test public void testFixedDate() throws Exception { int time = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Date", DateUtil.formatRfc1123Date(1000L * time)) .create(); assertEquals(time + roundToSeconds(response.getDefaultTtl()), roundToSeconds(response.getCacheExpiration())); assertEquals(DateUtil.formatRfc1123Date(timeSource.currentTimeMillis()), response.getHeader("Date")); assertTtlOk(roundToSeconds(response.getDefaultTtl()), response); }