HttpHandler implements AutoCloseable { public HttpResponse get(String path) throws IOException { HttpGet get = new HttpGet(url.resolve(path)); creds.ifPresent(x -> get.setHeader("Authorization", "Basic " + x)); return client.execute(get); } HttpHandler(URI url, OnrampOptions options); HttpResponse get(String path); HttpResponse post(String path, HttpEntity entity); @Override void close(); static HttpHandler paver(OnrampOptions options); static HttpHandler onramp(OnrampOptions options);  }
@Test public void simpleGet() throws Exception { stubFor(get(urlEqualTo("/foo")).willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody("bar"))); try (HttpHandler handler = new HttpHandler(url, options)) { HttpResponse response = handler.get("foo"); try (Reader reader = new InputStreamReader(response.getEntity().getContent())) { assertThat(CharStreams.toString(reader), is("bar")); } } }
@Test public void authenticated() throws Exception { stubFor(get(urlEqualTo("/foo")).willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody("bar"))); try (HttpHandler handler = new HttpHandler(url, options.withUsername("user").withPassword("pass"))) { handler.get("foo"); } verify(getRequestedFor(urlEqualTo("/foo")).withBasicAuth(new BasicCredentials("user", "pass"))); }
@Test public void unauthenticated() throws Exception { stubFor(get(urlEqualTo("/foo")).willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody("bar"))); try (HttpHandler handler = new HttpHandler(url, options)) { handler.get("foo"); } verify(getRequestedFor(urlEqualTo("/foo")).withoutHeader("Authorization")); }