PerHostServiceWrappedHttpClient extends AbstractHttpClientDecorator { public HttpResponse execute(HttpHost host, HttpRequest req, HttpContext ctx) throws IOException, ClientProtocolException { host = getCanonicalHost(host); HttpClient client = clients.get(host); if (client == null) { ServiceWrapper wrapper = factory.getWrapperWithName(host.toHostString()); client = new ServiceWrappedHttpClient(backend, wrapper); clients.put(host, client); } return client.execute(host, req, ctx); } PerHostServiceWrappedHttpClient(HttpClient backend, ServiceWrapperFactory factory); HttpResponse execute(HttpHost host, HttpRequest req, HttpContext ctx);  }
@Test public void usesRequestHostToCreateWrapper() throws Exception { final ServiceWrapper wrapper = new NullWrapper(); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp); expect(mockFactory.getWrapperWithName(HOST_STRING)).andReturn(wrapper); replayMocks(); impl.execute(host, req, ctx); verifyMocks(); }
@Test public void wiresWrapperUpToRequest() throws Exception { final Flag f = new Flag(); ServiceWrapper wrapper = new NullWrapper(f); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp); expect(mockFactory.getWrapperWithName(isA(String.class))).andReturn(wrapper); replayMocks(); impl.execute(host, req, ctx); verifyMocks(); assertTrue(f.set); }
@Test public void reusesWrapperForRequestsFromSameHost() throws Exception { ServiceWrapper wrapper = new NullWrapper(); HttpUriRequest req1 = new HttpGet("http: HttpUriRequest req2 = new HttpGet("http: HttpResponse resp1 = resp; HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); expect(mockFactory.getWrapperWithName(isA(String.class))).andReturn(wrapper); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp1); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp2); replayMocks(); impl.execute(req1, ctx); impl.execute(req2, ctx); verifyMocks(); }
@Test public void reusesWrapperForRequestsFromEquivalentHostsDefaultHttpPort() throws Exception { ServiceWrapper wrapper = new NullWrapper(); HttpUriRequest req1 = new HttpGet("http: HttpUriRequest req2 = new HttpGet("http: HttpResponse resp1 = resp; HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); expect(mockFactory.getWrapperWithName(isA(String.class))).andReturn(wrapper); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp1); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp2); replayMocks(); impl.execute(req1, ctx); impl.execute(req2, ctx); verifyMocks(); }
@Test public void reusesWrapperForRequestsFromEquivalentHostsDefaultHttpsPort() throws Exception { ServiceWrapper wrapper = new NullWrapper(); HttpUriRequest req1 = new HttpGet("https: HttpUriRequest req2 = new HttpGet("https: HttpResponse resp1 = resp; HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); expect(mockFactory.getWrapperWithName(isA(String.class))).andReturn(wrapper); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp1); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp2); replayMocks(); impl.execute(req1, ctx); impl.execute(req2, ctx); verifyMocks(); }
@Test public void reusesWrapperForRequestsFromEquivalentHostsCaseInsensitiveHostName() throws Exception { ServiceWrapper wrapper = new NullWrapper(); HttpUriRequest req1 = new HttpGet("http: HttpUriRequest req2 = new HttpGet("http: HttpResponse resp1 = resp; HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); expect(mockFactory.getWrapperWithName(isA(String.class))).andReturn(wrapper); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp1); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp2); replayMocks(); impl.execute(req1, ctx); impl.execute(req2, ctx); verifyMocks(); }
@Test public void usesDifferentWrappersForDifferentHosts() throws Exception { Flag f1 = new Flag(); ServiceWrapper wrapper1 = new NullWrapper(f1); Flag f2 = new Flag(); ServiceWrapper wrapper2 = new NullWrapper(f2); HttpUriRequest req1 = new HttpGet("http: HttpUriRequest req2 = new HttpGet("http: HttpResponse resp1 = resp; HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); expect(mockFactory.getWrapperWithName("foo.example.com:80")).andReturn(wrapper1); expect(mockFactory.getWrapperWithName("bar.example.com:80")).andReturn(wrapper2); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp1); expect(mockBackend.execute(isA(HttpHost.class), isA(HttpRequest.class), isA(HttpContext.class))) .andReturn(resp2); replayMocks(); impl.execute(req1, ctx); assertTrue(f1.set && !f2.set); impl.execute(req2, ctx); assertTrue(f1.set && f2.set); verifyMocks(); }