Search in sources :

Example 6 with TestServer

use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.

the class TestHttpClient method testPostNoCompression.

@Test
void testPostNoCompression() throws Exception {
    ExampleBean inputBean = new ExampleBean("x", 1, NOW);
    HttpHandler handler = h -> {
        Assertions.assertEquals("POST", h.getRequestMethod());
        assertThat(h.getRequestHeaders()).doesNotContainKeys("Content-Encoding");
        try (InputStream in = 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(), true).post(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) 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 7 with TestServer

use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.

the class TestHttpClient method testGetMultipleQueryParam.

@Test
void testGetMultipleQueryParam() throws Exception {
    ExampleBean inputBean = new ExampleBean("x", 1, NOW);
    HttpHandler handler = h -> {
        String[] queryParams = h.getRequestURI().getQuery().split("&");
        Assertions.assertEquals(2, queryParams.length);
        Set<String> queryParamSet = new HashSet<>(Arrays.asList(queryParams));
        Assertions.assertTrue(queryParamSet.contains("x=y"));
        Assertions.assertTrue(queryParamSet.contains("a=b"));
        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(handler)) {
        ExampleBean bean = get(server.getAddress()).queryParam("x", "y").queryParam("a", "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) HashSet(java.util.HashSet) Set(java.util.Set) OutputStream(java.io.OutputStream) TestServer(org.projectnessie.client.util.TestServer) Test(org.junit.jupiter.api.Test)

Example 8 with TestServer

use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.

the class TestHttpClient method testPost.

@Test
void testPost() throws Exception {
    ExampleBean inputBean = new ExampleBean("x", 1, NOW);
    HttpHandler handler = h -> {
        Assertions.assertEquals("POST", 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()).post(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 9 with TestServer

use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.

the class TestNessieHttpClient method testTracing.

@Test
void testTracing() throws Exception {
    AtomicReference<String> traceId = new AtomicReference<>();
    try (TestServer server = new TestServer(handlerForHeaderTest("Uber-trace-id", traceId))) {
        NessieApiV1 api = HttpClientBuilder.builder().withUri(server.getUri()).withTracing(true).build(NessieApiV1.class);
        try (Scope ignore = GlobalTracer.get().activateSpan(GlobalTracer.get().buildSpan("testOpenTracing").start())) {
            api.getConfig();
        }
    }
    // Cannot really assert on the value of the Uber-trace-id header, because the APIs don't
    // give us access to that. Verifying that the Uber-trace-id header is being sent should
    // be good enough though.
    assertNotNull(traceId.get());
}
Also used : Scope(io.opentracing.Scope) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestServer(org.projectnessie.client.util.TestServer) NessieApiV1(org.projectnessie.client.api.NessieApiV1) Test(org.junit.jupiter.api.Test)

Example 10 with TestServer

use of org.projectnessie.client.util.TestServer in project nessie by projectnessie.

the class TestNessieHttpClient method testNotFoundOnBaseUri.

@Test
void testNotFoundOnBaseUri() throws IOException {
    try (TestServer server = errorServer(404)) {
        NessieApiV1 api = HttpClientBuilder.builder().withUri(server.getUri().resolve("/unknownPath")).build(NessieApiV1.class);
        assertThatThrownBy(api::getConfig).isInstanceOf(RuntimeException.class).hasMessageContaining("Not Found");
    }
}
Also used : TestServer(org.projectnessie.client.util.TestServer) NessieApiV1(org.projectnessie.client.api.NessieApiV1) 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