Search in sources :

Example 11 with TestServer

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

the class TestNessieHttpClient method testInternalServerError.

@Test
void testInternalServerError() throws IOException {
    try (TestServer server = errorServer(500)) {
        NessieApiV1 api = HttpClientBuilder.builder().withUri(server.getUri().resolve("/broken")).build(NessieApiV1.class);
        assertThatThrownBy(api::getConfig).isInstanceOf(NessieInternalServerException.class).hasMessageContaining("Internal Server Error");
    }
}
Also used : NessieInternalServerException(org.projectnessie.client.rest.NessieInternalServerException) TestServer(org.projectnessie.client.util.TestServer) NessieApiV1(org.projectnessie.client.api.NessieApiV1) Test(org.junit.jupiter.api.Test)

Example 12 with TestServer

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

the class TestHttpClientBuilder method testAuthBasic.

@ParameterizedTest
@MethodSource("basicAuthConfigs")
void testAuthBasic(Function<HttpClientBuilder, HttpClientBuilder> config) throws Exception {
    AtomicReference<String> authHeader = new AtomicReference<>();
    try (TestServer server = new TestServer(handlerForHeaderTest("Authorization", authHeader))) {
        NessieApiV1 client = config.apply(HttpClientBuilder.builder().withUri(server.getUri())).build(NessieApiV1.class);
        client.getConfig();
    }
    assertThat(authHeader.get()).isNotNull().isEqualTo("Basic " + new String(Base64.getUrlEncoder().encode("my_username:very_secret".getBytes(UTF_8)), UTF_8));
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) TestServer(org.projectnessie.client.util.TestServer) NessieApiV1(org.projectnessie.client.api.NessieApiV1) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with TestServer

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

the class TestHttpsClient method testHttps.

@Test
void testHttps() throws Exception {
    HttpHandler handler = h -> {
        Assertions.assertEquals("GET", h.getRequestMethod());
        String response = "hello";
        h.sendResponseHeaders(200, response.getBytes().length);
        OutputStream os = h.getResponseBody();
        os.write(response.getBytes());
        os.close();
    };
    TrustManager[][] trustManager = new TrustManager[1][];
    try (TestServer server = new TestServer("/", handler, s -> {
        try {
            trustManager[0] = ssl(s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    })) {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustManager[0], new java.security.SecureRandom());
        HttpRequest client = HttpClient.builder().setBaseUri(URI.create("https://localhost:" + server.getAddress().getPort())).setObjectMapper(MAPPER).setSslContext(sc).build().newRequest();
        client.get();
        final HttpRequest insecureClient = HttpClient.builder().setBaseUri(URI.create("https://localhost:" + server.getAddress().getPort())).setObjectMapper(MAPPER).build().newRequest();
        Assertions.assertThrows(HttpClientException.class, insecureClient::get);
    }
}
Also used : X509Certificate(java.security.cert.X509Certificate) HttpServer(com.sun.net.httpserver.HttpServer) KeyPair(java.security.KeyPair) SSLContext(javax.net.ssl.SSLContext) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) TestServer(org.projectnessie.client.util.TestServer) Extension(org.bouncycastle.asn1.x509.Extension) ContentSigner(org.bouncycastle.operator.ContentSigner) MessageDigest(java.security.MessageDigest) Date(java.util.Date) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) ZonedDateTime(java.time.ZonedDateTime) SSLParameters(javax.net.ssl.SSLParameters) TrustedCertificateEntry(java.security.KeyStore.TrustedCertificateEntry) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) LoggerFactory(org.slf4j.LoggerFactory) TrustManager(javax.net.ssl.TrustManager) BCStyle(org.bouncycastle.asn1.x500.style.BCStyle) SSLEngine(javax.net.ssl.SSLEngine) SecureRandom(java.security.SecureRandom) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) URI(java.net.URI) X500NameBuilder(org.bouncycastle.asn1.x500.X500NameBuilder) OutputStream(java.io.OutputStream) KeyPairGenerator(java.security.KeyPairGenerator) Logger(org.slf4j.Logger) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) BaseEncoding(com.google.common.io.BaseEncoding) HttpsParameters(com.sun.net.httpserver.HttpsParameters) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) KeyStore(java.security.KeyStore) DERSequence(org.bouncycastle.asn1.DERSequence) Test(org.junit.jupiter.api.Test) GeneralName(org.bouncycastle.asn1.x509.GeneralName) HttpHandler(com.sun.net.httpserver.HttpHandler) Assertions(org.junit.jupiter.api.Assertions) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) HttpsServer(com.sun.net.httpserver.HttpsServer) X509v3CertificateBuilder(org.bouncycastle.cert.X509v3CertificateBuilder) HttpHandler(com.sun.net.httpserver.HttpHandler) OutputStream(java.io.OutputStream) SSLContext(javax.net.ssl.SSLContext) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) GeneralSecurityException(java.security.GeneralSecurityException) TrustManager(javax.net.ssl.TrustManager) TestServer(org.projectnessie.client.util.TestServer) SecureRandom(java.security.SecureRandom) Test(org.junit.jupiter.api.Test)

Example 14 with TestServer

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

the class TestNessieHttpClient method testUnauthorized.

@Test
void testUnauthorized() throws IOException {
    try (TestServer server = errorServer(401)) {
        NessieApiV1 api = HttpClientBuilder.builder().withUri(server.getUri().resolve("/unauthorized")).build(NessieApiV1.class);
        assertThatThrownBy(api::getConfig).isInstanceOf(NessieNotAuthorizedException.class).hasMessageContaining("Unauthorized");
    }
}
Also used : NessieNotAuthorizedException(org.projectnessie.client.rest.NessieNotAuthorizedException) TestServer(org.projectnessie.client.util.TestServer) NessieApiV1(org.projectnessie.client.api.NessieApiV1) Test(org.junit.jupiter.api.Test)

Example 15 with TestServer

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

the class TestNessieHttpClient method testTracingNotEnabled.

@Test
void testTracingNotEnabled() 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(false).build(NessieApiV1.class);
        try (Scope ignore = GlobalTracer.get().activateSpan(GlobalTracer.get().buildSpan("testOpenTracing").start())) {
            api.getConfig();
        }
    }
    assertNull(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)

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