public static void copyResponseToServlet(HttpResponse response, HttpServletResponse servletResponse) throws IOException { servletResponse.setStatus(response.getHttpStatusCode()); servletResponse.setContentLength(response.getContentLength()); for (Map.Entry<String, String> header : response.getHeaders().entries()) { servletResponse.addHeader(header.getKey(), header.getValue()); } HttpUtil.setCachingHeaders(servletResponse, (int)response.getCacheTtl()); IOUtils.copy(response.getResponse(), servletResponse.getOutputStream()); }
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()); } }
public HttpResponse execute(HttpRequest request) { this.request = request; return new HttpResponse(BASIC_BUNDLE); } }
public static void copyContentToServletResponse( HttpResponse response, HttpServletResponse servletResponse) throws IOException { servletResponse.setContentLength(response.getContentLength()); IOUtils.copy(response.getResponse(), servletResponse.getOutputStream()); } public static void copyHeadersAndStatusToServletResponse(
/** * @return Consolidated ttl in milliseconds or -1. */ public long getCacheTtl() { long expiration = getCacheExpiration(); if (expiration != -1) { return expiration - getTimeSource().currentTimeMillis(); } return -1; }
@Test public void testEncodingDetectionUtf8WithBomNoContentHeader() throws Exception { HttpResponse response = new HttpResponseBuilder() .setResponse(UTF8_DATA) .create(); assertEquals("UTF-8", response.getEncoding().toUpperCase()); assertEquals(UTF8_STRING, response.getResponseAsString()); }
@Test public void testNegativeCaching() { assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.error().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.notFound().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.timeout().getCacheExpiration() > timeSource.currentTimeMillis()); long ttl = HttpResponse.error().getCacheTtl(); assertTrue(ttl <= HttpResponse.DEFAULT_TTL && ttl > 0); }
@Test public void testOAuthFlow_unauthUser() throws Exception { MakeRequestClient client = makeNonSocialClient(null, null, GADGET_URL); HttpResponse response = client.sendGet(FakeOAuthServiceProvider.RESOURCE_URL); assertEquals("", response.getResponseAsString()); assertEquals(403, response.getHttpStatusCode()); assertEquals(-1, response.getCacheTtl()); assertEquals(OAuthError.UNAUTHENTICATED.name(), response.getMetadata().get("oauthError")); }
@Test public void testRewriteInefficientPNG() throws Exception { HttpResponse resp = createResponse("org/apache/shindig/gadgets/rewrite/image/inefficient.png", "image/png"); HttpResponse httpResponse = rewrite(resp); assertTrue(httpResponse.getContentLength() <= resp.getContentLength()); assertEquals("image/png", httpResponse.getHeader("Content-Type")); }
@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 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 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); }
public HttpResponseBuilder(GadgetHtmlParser parser, HttpResponse response) { super(parser, response); if (response != null) { httpStatusCode = response.getHttpStatusCode(); headers.putAll(response.getHeaders()); metadata.putAll(response.getMetadata()); } else { setResponse(null); } responseObj = response; responseObjNumChanges = getNumChanges(); }
@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); }
@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(expected = GadgetException.class) public void badFetchThrows() throws Exception { HttpRequest request = createIgnoreCacheRequest(); expect(pipeline.execute(request)).andReturn(HttpResponse.error()); replay(pipeline); specFactory.getGadgetSpec(createContext(SPEC_URL, true)); }
@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()); }
public static void copyHeadersAndStatusToServletResponse( HttpResponse response, HttpServletResponse servletResponse) { servletResponse.setStatus(response.getHttpStatusCode()); for (Map.Entry<String, String> header : response.getHeaders().entries()) { servletResponse.addHeader(header.getKey(), header.getValue()); } }
private static void addHeaders(Map<String, Collection<String>> headers, HttpResponse response, String name) { Collection<String> values = response.getHeaders(name); if (!values.isEmpty()) { headers.put(name.toLowerCase(), values); } }
@Test public void testCacheExpirationForStrictNoCacheResponse() throws Exception { assertEquals(-1, new HttpResponseBuilder() .setStrictNoCache() .setRefetchStrictNoCacheAfterMs(10000) .create() .getCacheExpiration()); }