use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.
the class TestHttpClient method testFilters.
@Test
void testFilters() throws Exception {
AtomicBoolean requestFilterCalled = new AtomicBoolean(false);
AtomicBoolean responseFilterCalled = new AtomicBoolean(false);
AtomicReference<ResponseContext> responseContextGotCallback = new AtomicReference<>();
AtomicReference<ResponseContext> responseContextGotFilter = new AtomicReference<>();
HttpHandler handler = h -> {
Assertions.assertTrue(h.getRequestHeaders().containsKey("x"));
h.sendResponseHeaders(200, 0);
};
try (TestServer server = new TestServer(handler)) {
HttpClient.Builder client = HttpClient.builder().setBaseUri(URI.create("http://localhost:" + server.getAddress().getPort())).setObjectMapper(MAPPER);
client.addRequestFilter(context -> {
requestFilterCalled.set(true);
context.putHeader("x", "y");
context.addResponseCallback((responseContext, failure) -> {
responseContextGotCallback.set(responseContext);
Assertions.assertNull(failure);
});
});
client.addResponseFilter(con -> {
try {
Assertions.assertEquals(Status.OK, con.getResponseCode());
responseFilterCalled.set(true);
responseContextGotFilter.set(con);
} catch (IOException e) {
throw new IOError(e);
}
});
client.build().newRequest().get();
Assertions.assertNotNull(responseContextGotFilter.get());
Assertions.assertSame(responseContextGotFilter.get(), responseContextGotCallback.get());
Assertions.assertTrue(responseFilterCalled.get());
Assertions.assertTrue(requestFilterCalled.get());
}
}
use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.
the class TestHttpClient method testGetTemplate.
@Test
void testGetTemplate() throws Exception {
ExampleBean inputBean = new ExampleBean("x", 1, NOW);
HttpHandler handler = h -> {
Assertions.assertEquals("GET", h.getRequestMethod());
String response = MAPPER.writeValueAsString(inputBean);
h.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = h.getResponseBody();
os.write(response.getBytes());
os.close();
};
try (TestServer server = new TestServer("/a/b", handler)) {
ExampleBean bean = get(server.getAddress()).path("a/b").get().readEntity(ExampleBean.class);
Assertions.assertEquals(inputBean, bean);
bean = get(server.getAddress()).path("a/{b}").resolveTemplate("b", "b").get().readEntity(ExampleBean.class);
Assertions.assertEquals(inputBean, bean);
}
}
use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.
the class TestHttpClient method testPut.
@Test
void testPut() throws Exception {
ExampleBean inputBean = new ExampleBean("x", 1, NOW);
HttpHandler handler = h -> {
Assertions.assertEquals("PUT", h.getRequestMethod());
assertThat(h.getRequestHeaders()).containsEntry("Content-Encoding", Collections.singletonList("gzip"));
try (InputStream in = new GZIPInputStream(h.getRequestBody())) {
Object bean = MAPPER.readerFor(ExampleBean.class).readValue(in);
Assertions.assertEquals(inputBean, bean);
}
h.sendResponseHeaders(200, 0);
};
try (TestServer server = new TestServer(handler)) {
get(server.getAddress()).put(inputBean);
}
}
use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.
the class TestHttpClient method testReadTimeout.
@Test
void testReadTimeout() {
HttpHandler handler = h -> {
};
Assertions.assertThrows(HttpClientReadTimeoutException.class, () -> {
try (TestServer server = new TestServer(handler)) {
get(server.getAddress(), 15000, 1, true).get().readEntity(ExampleBean.class);
}
});
}
Aggregations