public static String getMimeType(HttpRequest request, HttpResponse original) { String mimeType = request.getRewriteMimeType(); if (mimeType == null) { mimeType = original.getHeader("Content-Type"); } return mimeType != null ? mimeType.toLowerCase() : null; }
@Test public void addResponseWithForcedTtl() { HttpRequest request = new HttpRequest(DEFAULT_URI) .setCacheTtl(10); String key = cache.createKey(request); HttpResponse response = new HttpResponse("result"); assertNotNull(cache.addResponse(request, response)); assertEquals("public,max-age=10", cache.map.get(key).getHeader("Cache-Control")); assertNotNull(extendedStrictNoCacheTtlCache.addResponse(request, response)); assertEquals("public,max-age=10", extendedStrictNoCacheTtlCache.map.get(key).getHeader("Cache-Control")); }
@Test public void addResponseWithForcedTtl() { HttpRequest request = new HttpRequest(DEFAULT_URI) .setCacheTtl(10); String key = cache.createKey(request); HttpResponse response = new HttpResponse("result"); assertNotNull(cache.addResponse(request, response)); assertEquals("public,max-age=10", cache.map.get(key).getHeader("Cache-Control")); assertNotNull(extendedStrictNoCacheTtlCache.addResponse(request, response)); assertEquals("public,max-age=10", extendedStrictNoCacheTtlCache.map.get(key).getHeader("Cache-Control")); }
@Test public void testDelete() throws Exception { HttpRequest request = new HttpRequest(BASE_URL).setMethod("DELETE"); HttpResponse response = fetcher.fetch(request); assertEquals("DELETE", response.getHeader("x-method")); }
@Test public void testDelete() throws Exception { HttpRequest request = new HttpRequest(BASE_URL).setMethod("DELETE"); HttpResponse response = fetcher.fetch(request); assertEquals("DELETE", response.getHeader("x-method")); }
@Test public void addResponseWithForcedTtl() { HttpRequest request = new HttpRequest(DEFAULT_URI) .setCacheTtl(10); String key = cache.createKey(request); HttpResponse response = new HttpResponse("result"); assertTrue(cache.addResponse(request, response)); assertEquals("public,max-age=10", cache.map.get(key).getHeader("Cache-Control")); }
@Test public void testPostBinaryData() throws Exception { byte[] raw = { 0, 1, 2, 3, 4, 5 }; MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendRawPost(FakeOAuthServiceProvider.RESOURCE_URL, null, raw); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); assertTrue(contains(queryParams, "opensocial_owner_id", "o")); assertTrue(contains(queryParams, OAuth.OAUTH_CONSUMER_KEY, "signedfetch")); String echoed = resp.getHeader(FakeOAuthServiceProvider.RAW_BODY_ECHO_HEADER); byte[] echoedBytes = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(echoed)); assertTrue(Arrays.equals(raw, echoedBytes)); }
@Test public void testPostBinaryData() throws Exception { byte[] raw = { 0, 1, 2, 3, 4, 5 }; MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendRawPost(FakeOAuthServiceProvider.RESOURCE_URL, null, raw); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); assertTrue(contains(queryParams, "opensocial_owner_id", "o")); assertTrue(contains(queryParams, OAuth.OAUTH_CONSUMER_KEY, "signedfetch")); String echoed = resp.getHeader(FakeOAuthServiceProvider.RAW_BODY_ECHO_HEADER); byte[] echoedBytes = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(echoed)); assertTrue(Arrays.equals(raw, echoedBytes)); }
@Test public void testSimpleImage() throws Exception { HttpResponse resp = createResponse("org/apache/shindig/gadgets/rewrite/image/simple.bmp", "image/bmp"); HttpResponse rewritten = rewrite(resp); assertTrue(rewritten.getContentLength() < resp.getContentLength()); assertEquals("image/png", rewritten.getHeader("Content-Type")); }
@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 testSimpleImage() throws Exception { HttpResponse resp = createResponse("org/apache/shindig/gadgets/rewrite/image/simple.bmp", "image/bmp"); HttpResponse rewritten = rewrite(resp); assertTrue(rewritten.getContentLength() < resp.getContentLength()); assertEquals("image/png", rewritten.getHeader("Content-Type")); }
@Test public void testPost_noBody() throws Exception { HttpRequest request = new HttpRequest(BASE_URL).setMethod("POST"); HttpResponse response = fetcher.fetch(request); assertEquals("POST", response.getHeader("x-method")); assertEquals("", response.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 testPost_noBody() throws Exception { HttpRequest request = new HttpRequest(BASE_URL).setMethod("POST"); HttpResponse response = fetcher.fetch(request); assertEquals("POST", response.getHeader("x-method")); assertEquals("", response.getResponseAsString()); }
@Test public void addResponseWithForcedTtlAndStrictNoCache() { HttpRequest request = new HttpRequest(DEFAULT_URI) .setCacheTtl(10); String key = cache.createKey(request); HttpResponse response = new HttpResponseBuilder() .setResponseString("result") .setStrictNoCache() .create(); assertTrue(cache.addResponse(request, response)); assertEquals("public,max-age=10", cache.map.get(key).getHeader("Cache-Control")); }
@Test public void testPostNoQueryNoData() throws Exception { MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendFormPost(FakeOAuthServiceProvider.RESOURCE_URL, null); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); assertTrue(contains(queryParams, "opensocial_owner_id", "o")); assertEquals("", resp.getHeader(FakeOAuthServiceProvider.BODY_ECHO_HEADER)); }
@Test public void testPostNoQueryNoData() throws Exception { MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendFormPost(FakeOAuthServiceProvider.RESOURCE_URL, null); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); assertTrue(contains(queryParams, "opensocial_owner_id", "o")); assertEquals("", resp.getHeader(FakeOAuthServiceProvider.BODY_ECHO_HEADER)); }
@Test public void testPostNoQueryNoData() throws Exception { MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); HttpResponse resp = client.sendFormPost(FakeOAuthServiceProvider.RESOURCE_URL, null); List<Parameter> queryParams = OAuth.decodeForm(resp.getResponseAsString()); assertTrue(contains(queryParams, "opensocial_owner_id", "o")); assertEquals("", resp.getHeader(FakeOAuthServiceProvider.BODY_ECHO_HEADER)); }
@Test public void testSmallJPEGIsNotConvertedToPNGWithJpegConversionDisabled() throws Exception { HttpResponse resp = createResponse("org/apache/shindig/gadgets/rewrite/image/small.jpg", "image/jpeg"); HttpResponse rewritten = rewrite(resp, getDefaultConfigWithJpegConversionDisabled()); assertEquals("image/jpeg", rewritten.getHeader("Content-Type")); assertTrue(rewritten.getContentLength() <= resp.getContentLength()); }
@Test public void testSignedFetch_authHeader() throws Exception { serviceProvider.setParamLocation(OAuthParamLocation.AUTH_HEADER); MakeRequestClient client = makeSignedFetchClient("o", "v", "http://www.example.com/app"); client.getBaseArgs().setRequestOption(OAuthArguments.PROGRAMMATIC_CONFIG_PARAM, "true"); client.getBaseArgs().setRequestOption(OAuthArguments.PARAM_LOCATION_PARAM, "auth-header"); HttpResponse resp = client.sendGet(FakeOAuthServiceProvider.RESOURCE_URL); String auth = resp.getHeader(FakeOAuthServiceProvider.AUTHZ_ECHO_HEADER); assertNotNull("Should have echoed authz header", auth); checkStringContains("should have opensocial params in header", auth, "opensocial_owner_id=\"o\""); }