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");
}
}
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));
}
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);
}
}
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");
}
}
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());
}
Aggregations