Search in sources :

Example 16 with TestServer

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());
    }
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) TestServer(org.projectnessie.client.util.TestServer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) URI(java.net.URI) CommitMeta(org.projectnessie.model.CommitMeta) OutputStream(java.io.OutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) IOError(java.io.IOError) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpHandler(com.sun.net.httpserver.HttpHandler) Assertions(org.junit.jupiter.api.Assertions) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) Collections(java.util.Collections) InputStream(java.io.InputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpHandler(com.sun.net.httpserver.HttpHandler) IOError(java.io.IOError) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) TestServer(org.projectnessie.client.util.TestServer) Test(org.junit.jupiter.api.Test)

Example 17 with TestServer

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);
    }
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) TestServer(org.projectnessie.client.util.TestServer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) URI(java.net.URI) CommitMeta(org.projectnessie.model.CommitMeta) OutputStream(java.io.OutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) IOError(java.io.IOError) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpHandler(com.sun.net.httpserver.HttpHandler) Assertions(org.junit.jupiter.api.Assertions) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) Collections(java.util.Collections) InputStream(java.io.InputStream) HttpHandler(com.sun.net.httpserver.HttpHandler) OutputStream(java.io.OutputStream) TestServer(org.projectnessie.client.util.TestServer) Test(org.junit.jupiter.api.Test)

Example 18 with TestServer

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);
    }
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) TestServer(org.projectnessie.client.util.TestServer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) URI(java.net.URI) CommitMeta(org.projectnessie.model.CommitMeta) OutputStream(java.io.OutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) IOError(java.io.IOError) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpHandler(com.sun.net.httpserver.HttpHandler) Assertions(org.junit.jupiter.api.Assertions) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) Collections(java.util.Collections) InputStream(java.io.InputStream) GZIPInputStream(java.util.zip.GZIPInputStream) HttpHandler(com.sun.net.httpserver.HttpHandler) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) TestServer(org.projectnessie.client.util.TestServer) Test(org.junit.jupiter.api.Test)

Example 19 with TestServer

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);
        }
    });
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) TestServer(org.projectnessie.client.util.TestServer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) URI(java.net.URI) CommitMeta(org.projectnessie.model.CommitMeta) OutputStream(java.io.OutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) IOError(java.io.IOError) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) HttpHandler(com.sun.net.httpserver.HttpHandler) Assertions(org.junit.jupiter.api.Assertions) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) Collections(java.util.Collections) InputStream(java.io.InputStream) HttpHandler(com.sun.net.httpserver.HttpHandler) TestServer(org.projectnessie.client.util.TestServer) Test(org.junit.jupiter.api.Test)

Aggregations

TestServer (org.projectnessie.client.util.TestServer)19 Test (org.junit.jupiter.api.Test)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 HttpHandler (com.sun.net.httpserver.HttpHandler)13 OutputStream (java.io.OutputStream)13 URI (java.net.URI)13 Assertions (org.junit.jupiter.api.Assertions)13 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)12 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)12 IOError (java.io.IOError)12 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 InetSocketAddress (java.net.InetSocketAddress)12 Instant (java.time.Instant)12 Arrays (java.util.Arrays)12 Collections (java.util.Collections)12 HashSet (java.util.HashSet)12 List (java.util.List)12 Objects (java.util.Objects)12