AuthorizedRequestFactory implements RequestFactory { @Override public Request createRequest(URI uri, String method) throws IOException { final Request request = delegate.createRequest(uri, method); request.getHeaders().put(Headers.AUTHORIZATION, authorizationProvider.getAuthorizationHeader()); return request; } AuthorizedRequestFactory(final RequestFactory delegate, final AuthorizationProvider authorizationProvider); @Override Request createRequest(URI uri, String method);  }
@Test public void shouldDelegate() throws IOException { final URI uri = URI.create("localhost"); final String method = "GET"; unit.createRequest(uri, method); verify(delegate).createRequest(uri, method); }
@Test public void shouldAddAuthorizationHeader() throws IOException { final Request request = unit.createRequest(URI.create("localhost"), "POST"); assertThat(request.getHeaders().headerNames(), contains(Headers.AUTHORIZATION)); assertThat(request.getHeaders().getFirst(Headers.AUTHORIZATION), equalTo(BEARER_TOKEN)); }