@Override public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext, HttpExecutionAware execAware) throws IOException, HttpException { Proxy proxy = (Proxy) clientContext.getAttribute(VSCrawlerConstant.VSCRAWLER_AVPROXY_KEY); if (proxy != null) { proxy.recordUsage(); } try { return delegate.execute(route, request, clientContext, execAware); } catch (IOException ioe) { if (proxy != null) { proxy.recordFailed(); } throw ioe; } }
@Test public void testPassesOnOrigin401ResponseWithoutWWWAuthenticateHeader() throws Exception { originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HTTP_1_1, 401, "Unauthorized")); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, HttpRequestWrapper.wrap(request), context, null); verifyMocks(); Assert.assertSame(originResponse, result); }
private CloseableHttpResponse revalidateCacheEntry( final HttpRoute route, final HttpRequestWrapper request, final HttpClientContext context, final HttpExecutionAware execAware, final HttpCacheEntry entry, final Date now) throws HttpException { try { if (asynchRevalidator != null && !staleResponseNotAllowed(request, entry, now) && validityPolicy.mayReturnStaleWhileRevalidating(entry, now)) { log.trace("Serving stale with asynchronous revalidation"); final CloseableHttpResponse resp = generateCachedResponse(request, context, entry, now); asynchRevalidator.revalidateCacheEntry(this, route, request, context, execAware, entry); return resp; } return revalidateCacheEntry(route, request, context, execAware, entry); } catch (final IOException ioex) { return handleRevalidationFailure(request, context, entry, now); } }
private CloseableHttpResponse handleCacheMiss( final HttpRoute route, final HttpRequestWrapper request, final HttpClientContext context, final HttpExecutionAware execAware) throws IOException, HttpException { final HttpHost target = context.getTargetHost(); recordCacheMiss(target, request); if (!mayCallBackend(request)) { return Proxies.enhanceResponse( new BasicHttpResponse( HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout")); } final Map<String, Variant> variants = getExistingCacheVariants(target, request); if (variants != null && !variants.isEmpty()) { return negotiateResponseFromVariants(route, request, context, execAware, variants); } return callBackend(route, request, context, execAware); }
@Test public void testPassesOnOrigin407WithoutAProxyAuthenticateHeader() throws Exception { originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HTTP_1_1, 407, "Proxy Authentication Required")); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, HttpRequestWrapper.wrap(request), context, null); verifyMocks(); Assert.assertSame(originResponse, result); }
/** * Schedules an asynchronous revalidation */ public synchronized void revalidateCacheEntry( final CachingExec cachingExec, final HttpRoute route, final HttpRequestWrapper request, final HttpClientContext context, final HttpExecutionAware execAware, final HttpCacheEntry entry) { // getVariantURI will fall back on getURI if no variants exist final String uri = cacheKeyGenerator.getVariantURI(context.getTargetHost(), request, entry); if (!queued.contains(uri)) { final int consecutiveFailedAttempts = failureCache.getErrorCount(uri); final AsynchronousValidationRequest revalidationRequest = new AsynchronousValidationRequest( this, cachingExec, route, request, context, execAware, entry, uri, consecutiveFailedAttempts); try { schedulingStrategy.schedule(revalidationRequest); queued.add(uri); } catch (final RejectedExecutionException ree) { log.debug("Revalidation for [" + uri + "] not scheduled: " + ree); } } }
private void testDoesNotModifyHeaderOnRequests(final String headerName) throws Exception { final String headerValue = HttpTestUtils.getCanonicalHeaderValue(request, headerName); final Capture<HttpRequestWrapper> cap = new Capture<HttpRequestWrapper>(); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.capture(cap), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); assertEquals(headerValue, HttpTestUtils.getCanonicalHeaderValue(cap.getValue(), headerName)); }
@Test(expected = HttpException.class) public void testPostProcessHttpException() throws Exception { final HttpRoute route = new HttpRoute(target); final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("/test")); final HttpClientContext context = HttpClientContext.create(); final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); Mockito.when(requestExecutor.execute( Mockito.eq(route), Mockito.<HttpRequestWrapper>any(), Mockito.<HttpClientContext>any(), Mockito.<HttpExecutionAware>any())).thenReturn(response); Mockito.doThrow(new HttpException("Ooopsie")).when(httpProcessor).process( Mockito.same(response), Mockito.<HttpContext>any()); try { protocolExec.execute(route, request, context, execAware); } catch (final Exception ex) { Mockito.verify(response).close(); throw ex; } }
@Test(expected = RuntimeException.class) public void testStrategyRuntimeException() throws Exception { final HttpRoute route = new HttpRoute(target); final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("/test")); final HttpClientContext context = HttpClientContext.create(); final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); Mockito.when(requestExecutor.execute( Mockito.eq(route), Mockito.<HttpRequestWrapper>any(), Mockito.<HttpClientContext>any(), Mockito.<HttpExecutionAware>any())).thenReturn(response); Mockito.doThrow(new RuntimeException("Ooopsie")).when(retryStrategy).retryRequest( Mockito.<HttpResponse>any(), Mockito.anyInt(), Mockito.<HttpContext>any()); try { retryExec.execute(route, request, context, execAware); } catch (final Exception ex) { Mockito.verify(response).close(); throw ex; } }
@Test public void testPassesOnOrigin405WithoutAllowHeader() throws Exception { originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HTTP_1_1, 405, "Method Not Allowed")); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, HttpRequestWrapper.wrap(request), context, null); verifyMocks(); Assert.assertSame(originResponse, result); }
@Test public void testHigherMajorProtocolVersionsOnRequestSwitchToTunnelBehavior() throws Exception { // tunnel behavior: I don't muck with request or response in // any way request = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", new ProtocolVersion("HTTP", 2, 13))); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), eqRequest(request), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertSame(originResponse, result); }
@Test public void testHigher1_XProtocolVersionsDowngradeTo1_1() throws Exception { request = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", new ProtocolVersion("HTTP", 1, 2))); final HttpRequestWrapper downgraded = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", HttpVersion.HTTP_1_1)); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), eqRequest(downgraded), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertTrue(HttpTestUtils.semanticallyTransparent(originResponse, result)); }
@Test public void testRequestsWithLowerProtocolVersionsGetUpgradedTo1_1() throws Exception { request = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", new ProtocolVersion("HTTP", 1, 0))); final HttpRequestWrapper upgraded = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", HttpVersion.HTTP_1_1)); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), eqRequest(upgraded), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertTrue(HttpTestUtils.semanticallyTransparent(originResponse, result)); }
@Test public void testLowerOriginResponsesUpgradedToOurVersion1_1() throws Exception { originResponse = Proxies.enhanceResponse( new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 2), HttpStatus.SC_OK, "OK")); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockOrigin/1.0"); originResponse.setEntity(body); // not testing this internal behavior in this test, just want // to check the protocol version that comes out the other end EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertEquals(HttpVersion.HTTP_1_1, result.getProtocolVersion()); }
@Test public void testResponseToA1_0RequestShouldUse1_1() throws Exception { request = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", new ProtocolVersion("HTTP", 1, 0))); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertEquals(HttpVersion.HTTP_1_1, result.getProtocolVersion()); }
@Test public void testForwardsUnknownHeadersOnRequestsFromHigherProtocolVersions() throws Exception { request = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", new ProtocolVersion("HTTP", 1, 2))); request.removeHeaders("Connection"); request.addHeader("X-Unknown-Header", "some-value"); final HttpRequestWrapper downgraded = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", HttpVersion.HTTP_1_1)); downgraded.removeHeaders("Connection"); downgraded.addHeader("X-Unknown-Header", "some-value"); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), eqRequest(downgraded), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); }
@Test public void testTransferCodingsAreNotSentToAnHTTP_1_0Client() throws Exception { originResponse.setHeader("Transfer-Encoding", "identity"); request = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/foo", new ProtocolVersion("HTTP", 1, 0))); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertNull(result.getFirstHeader("TE")); Assert.assertNull(result.getFirstHeader("Transfer-Encoding")); }
private void testOrderOfMultipleHeadersIsPreservedOnRequests(final String h, final HttpRequestWrapper request) throws Exception { final Capture<HttpRequestWrapper> reqCapture = new Capture<HttpRequestWrapper>(); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.capture(reqCapture), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); final HttpRequest forwarded = reqCapture.getValue(); Assert.assertNotNull(forwarded); final String expected = HttpTestUtils.getCanonicalHeaderValue(request, h); final String actual = HttpTestUtils.getCanonicalHeaderValue(forwarded, h); if (!actual.contains(expected)) { Assert.assertEquals(expected, actual); } }
private void testOrderOfMultipleHeadersIsPreservedOnResponses(final String h) throws Exception { EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertNotNull(result); Assert.assertEquals(HttpTestUtils.getCanonicalHeaderValue(originResponse, h), HttpTestUtils .getCanonicalHeaderValue(result, h)); }
private void testUnknownResponseStatusCodeIsNotCached(final int code) throws Exception { emptyMockCacheExpectsNoPuts(); originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HttpVersion.HTTP_1_1, code, "Moo")); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockOrigin/1.0"); originResponse.setHeader("Cache-Control", "max-age=3600"); originResponse.setEntity(body); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); // in particular, there were no storage calls on the cache verifyMocks(); }
@Test public void testUnknownHeadersOnRequestsAreForwarded() throws Exception { request.addHeader("X-Unknown-Header", "blahblah"); final Capture<HttpRequestWrapper> reqCap = new Capture<HttpRequestWrapper>(); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.capture(reqCap), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); final HttpRequest forwarded = reqCap.getValue(); final Header[] hdrs = forwarded.getHeaders("X-Unknown-Header"); Assert.assertEquals(1, hdrs.length); Assert.assertEquals("blahblah", hdrs[0].getValue()); }
@Test public void test200ResponseToOPTIONSWithNoBodyShouldIncludeContentLengthZero() throws Exception { request = HttpRequestWrapper.wrap(new BasicHttpRequest("OPTIONS", "/", HttpVersion.HTTP_1_1)); originResponse.setEntity(null); originResponse.setHeader("Content-Length", "0"); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); final Header contentLength = result.getFirstHeader("Content-Length"); Assert.assertNotNull(contentLength); Assert.assertEquals("0", contentLength.getValue()); }
@Test public void testDecrementsMaxForwardsWhenForwardingOPTIONSRequest() throws Exception { request = HttpRequestWrapper.wrap(new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1)); request.setHeader("Max-Forwards", "7"); final Capture<HttpRequestWrapper> cap = new Capture<HttpRequestWrapper>(); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.capture(cap), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); final HttpRequest captured = cap.getValue(); Assert.assertEquals("6", captured.getFirstHeader("Max-Forwards").getValue()); }
@Test public void testFundamentals() throws Exception { final HttpRoute route = new HttpRoute(target); final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("/test")); final HttpClientContext context = HttpClientContext.create(); final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); Mockito.when(requestExecutor.execute( Mockito.eq(route), Mockito.<HttpRequestWrapper>any(), Mockito.<HttpClientContext>any(), Mockito.<HttpExecutionAware>any())).thenReturn(response); protocolExec.execute(route, request, context, execAware); Mockito.verify(httpProcessor).process(request, context); Mockito.verify(requestExecutor).execute(route, request, context, execAware); Mockito.verify(httpProcessor).process(response, context); Assert.assertEquals(new HttpHost("foo", 80), context.getTargetHost()); Assert.assertEquals(target, context.getTargetHost()); Assert.assertEquals(route, context.getHttpRoute()); Assert.assertSame(request, context.getRequest()); Assert.assertSame(response, context.getResponse()); }
@Test public void testResponseToAHEADRequestMustNotHaveABody() throws Exception { request = HttpRequestWrapper.wrap(new BasicHttpRequest("HEAD", "/", HttpVersion.HTTP_1_1)); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertTrue(result.getEntity() == null || result.getEntity().getContentLength() == 0); }
@Test public void testResponsesToPOSTWithoutCacheControlOrExpiresAreNotCached() throws Exception { emptyMockCacheExpectsNoPuts(); final BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest("POST", "/", HttpVersion.HTTP_1_1); post.setHeader("Content-Length", "128"); post.setEntity(HttpTestUtils.makeBody(128)); originResponse.removeHeaders("Cache-Control"); originResponse.removeHeaders("Expires"); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, HttpRequestWrapper.wrap(post), context, null); verifyMocks(); }
@Test public void testResponsesToPUTsAreNotCached() throws Exception { emptyMockCacheExpectsNoPuts(); final BasicHttpEntityEnclosingRequest put = new BasicHttpEntityEnclosingRequest("PUT", "/", HttpVersion.HTTP_1_1); put.setEntity(HttpTestUtils.makeBody(128)); put.addHeader("Content-Length", "128"); originResponse.setHeader("Cache-Control", "max-age=3600"); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, HttpRequestWrapper.wrap(put), context, null); verifyMocks(); }
@Test public void testResponsesToDELETEsAreNotCached() throws Exception { emptyMockCacheExpectsNoPuts(); request = HttpRequestWrapper.wrap(new BasicHttpRequest("DELETE", "/", HttpVersion.HTTP_1_1)); originResponse.setHeader("Cache-Control", "max-age=3600"); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); }
@Test public void testResponsesToTRACEsAreNotCached() throws Exception { emptyMockCacheExpectsNoPuts(); request = HttpRequestWrapper.wrap(new BasicHttpRequest("TRACE", "/", HttpVersion.HTTP_1_1)); originResponse.setHeader("Cache-Control", "max-age=3600"); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); }
@Test public void test204ResponsesDoNotContainMessageBodies() throws Exception { originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NO_CONTENT, "No Content")); originResponse.setEntity(HttpTestUtils.makeBody(entityLength)); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertTrue(result.getEntity() == null || result.getEntity().getContentLength() == 0); }
@Test(expected = RuntimeException.class) public void testPostProcessRuntimeException() throws Exception { final HttpRoute route = new HttpRoute(target); final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("/test")); final HttpClientContext context = HttpClientContext.create(); final CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class); Mockito.when(requestExecutor.execute( Mockito.eq(route), Mockito.<HttpRequestWrapper>any(), Mockito.<HttpClientContext>any(), Mockito.<HttpExecutionAware>any())).thenReturn(response); Mockito.doThrow(new RuntimeException("Ooopsie")).when(httpProcessor).process( Mockito.same(response), Mockito.<HttpContext>any()); try { protocolExec.execute(route, request, context, execAware); } catch (final Exception ex) { Mockito.verify(response).close(); throw ex; } }
@Test public void test303ResponsesAreNotCached() throws Exception { emptyMockCacheExpectsNoPuts(); request = HttpRequestWrapper.wrap(new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1)); originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_SEE_OTHER, "See Other")); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockServer/1.0"); originResponse.setHeader("Cache-Control", "max-age=3600"); originResponse.setHeader("Content-Type", "application/x-cachingclient-test"); originResponse.setHeader("Location", "http://foo.example.com/other"); originResponse.setEntity(HttpTestUtils.makeBody(entityLength)); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); }
@Test public void test304ResponseWithDateHeaderForwardedFromOriginIncludesDateHeader() throws Exception { request.setHeader("If-None-Match", "\"etag\""); originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_MODIFIED, "Not Modified")); originResponse.setHeader("Date", DateUtils.formatDate(new Date())); originResponse.setHeader("Server", "MockServer/1.0"); originResponse.setHeader("ETag", "\"etag\""); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); verifyMocks(); Assert.assertNotNull(result.getFirstHeader("Date")); }
@Test public void testMustIncludeWWWAuthenticateHeaderOnAnOrigin401Response() throws Exception { originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HttpVersion.HTTP_1_1, 401, "Unauthorized")); originResponse.setHeader("WWW-Authenticate", "x-scheme x-param"); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); if (result.getStatusLine().getStatusCode() == 401) { Assert.assertNotNull(result.getFirstHeader("WWW-Authenticate")); } verifyMocks(); }
@Test public void testMustIncludeProxyAuthenticateHeaderFromAnOrigin407Response() throws Exception { originResponse = Proxies.enhanceResponse( new BasicHttpResponse(HttpVersion.HTTP_1_1, 407, "Proxy Authentication Required")); originResponse.setHeader("Proxy-Authenticate", "x-scheme x-param"); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.isA(HttpRequestWrapper.class), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); final HttpResponse result = impl.execute(route, request, context, null); if (result.getStatusLine().getStatusCode() == 407) { Assert.assertNotNull(result.getFirstHeader("Proxy-Authenticate")); } verifyMocks(); }
private HttpResponse testRequestWithWeakETagValidatorIsNotAllowed(final String header) throws Exception { final Capture<HttpRequestWrapper> cap = new Capture<HttpRequestWrapper>(); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.capture(cap), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse).times(0, 1); replayMocks(); final HttpResponse response = impl.execute(route, request, context, null); verifyMocks(); // it's probably ok to return a 400 (Bad Request) to this client if (cap.hasCaptured()) { final HttpRequest forwarded = cap.getValue(); final Header h = forwarded.getFirstHeader(header); if (h != null) { Assert.assertFalse(h.getValue().startsWith("W/")); } } return response; }
private void testDoesNotModifyHeaderOnRequest(final String header, final String value) throws Exception { final BasicHttpEntityEnclosingRequest req = new BasicHttpEntityEnclosingRequest("POST","/",HttpVersion.HTTP_1_1); req.setEntity(HttpTestUtils.makeBody(128)); req.setHeader("Content-Length","128"); req.setHeader(header,value); final Capture<HttpRequestWrapper> cap = new Capture<HttpRequestWrapper>(); EasyMock.expect( mockBackend.execute( EasyMock.eq(route), EasyMock.capture(cap), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, HttpRequestWrapper.wrap(req), context, null); verifyMocks(); final HttpRequest captured = cap.getValue(); Assert.assertEquals(value, captured.getFirstHeader(header).getValue()); }
@Test public void testViaHeaderOnRequestProperlyRecordsClientProtocol() throws Exception { request = HttpRequestWrapper.wrap(new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_0)); request.removeHeaders("Via"); final Capture<HttpRequestWrapper> cap = new Capture<HttpRequestWrapper>(); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.capture(cap), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); final HttpRequest captured = cap.getValue(); final String via = captured.getFirstHeader("Via").getValue(); final String protocol = via.split("\\s+")[0]; final String[] protoParts = protocol.split("/"); if (protoParts.length > 1) { Assert.assertTrue("http".equalsIgnoreCase(protoParts[0])); } Assert.assertEquals("1.0",protoParts[protoParts.length-1]); }
@Test public void testProperlyFormattedViaHeaderIsAddedToRequests() throws Exception { final Capture<HttpRequestWrapper> cap = new Capture<HttpRequestWrapper>(); request.removeHeaders("Via"); EasyMock.expect( mockBackend.execute( EasyMock.isA(HttpRoute.class), EasyMock.capture(cap), EasyMock.isA(HttpClientContext.class), EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse); replayMocks(); impl.execute(route, request, context, null); verifyMocks(); final HttpRequest captured = cap.getValue(); final String via = captured.getFirstHeader("Via").getValue(); assertValidViaHeader(via); }
@Override public CloseableHttpResponse execute( HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext, HttpExecutionAware execAware) throws IOException, HttpException { ActiveSpan localSpan = clientContext.getAttribute(ACTIVE_SPAN, ActiveSpan.class); CloseableHttpResponse response = null; try { if (localSpan == null) { localSpan = handleLocalSpan(request, clientContext); } return (response = handleNetworkProcessing(localSpan, route, request, clientContext, execAware)); } catch (Exception e) { localSpan.deactivate(); throw e; } finally { if (response != null) { /** * This exec runs after {@link org.apache.http.impl.execchain.RedirectExec} which loops * until there is no redirect or reaches max redirect count. * {@link RedirectStrategy} is used to decide whether localSpan should be finished or not. * If there is a redirect localSpan is not finished and redirect is logged. */ Integer redirectCount = clientContext.getAttribute(REDIRECT_COUNT, Integer.class); if (!redirectHandlingDisabled && clientContext.getRequestConfig().isRedirectsEnabled() && redirectStrategy.isRedirected(request, response, clientContext) && ++redirectCount < clientContext.getRequestConfig().getMaxRedirects()) { clientContext.setAttribute(REDIRECT_COUNT, redirectCount); } else { localSpan.deactivate(); } } } }