Search in sources :

Example 16 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse in project airlift by airlift.

the class TestHttpServerProvider method assertForward.

private void assertForward(ForwardedServlet servlet, Optional<String> proto, Optional<String> host, Optional<String> remoteHost) {
    servlet.reset();
    HttpUriBuilder uriBuilder = HttpUriBuilder.uriBuilderFrom(httpServerInfo.getHttpUri()).replacePath("/some/path");
    try (HttpClient client = new JettyHttpClient()) {
        Builder builder = prepareGet().setUri(uriBuilder.build());
        proto.ifPresent(value -> builder.addHeader(X_FORWARDED_PROTO, value));
        host.ifPresent(value -> builder.addHeader(X_FORWARDED_HOST, value));
        remoteHost.ifPresent(value -> builder.addHeader(X_FORWARDED_FOR, value));
        StringResponse response = client.execute(builder.build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), 200);
    }
    proto.ifPresent(uriBuilder::scheme);
    host.map(HostAndPort::fromString).ifPresent(uriBuilder::hostAndPort);
    URI forwardedUri = uriBuilder.build();
    assertEquals(servlet.getRequestUrl(), forwardedUri.toString());
    assertEquals(servlet.getScheme(), forwardedUri.getScheme());
    assertEquals(servlet.getIsSecure(), (Boolean) forwardedUri.getScheme().equals("https"));
    remoteHost.ifPresent(value -> assertEquals(servlet.getRemoteAddress(), value));
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) Builder(io.airlift.http.client.Request.Builder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) URI(java.net.URI)

Example 17 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse in project airlift by airlift.

the class TestJaxrsTestingHttpProcessor method testOptions.

@Test
public void testOptions() {
    Request request = new Request.Builder().setMethod("OPTIONS").setUri(URI.create("http://fake.invalid/get-it/get/xyz")).build();
    StringResponse response = HTTP_CLIENT.execute(request, createStringResponseHandler());
    assertThat(response.getStatusCode()).isEqualTo(HTTP_OK);
    assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo("application/vnd.sun.wadl+xml");
    assertThat(response.getBody()).startsWith("<?xml ").contains("<application ");
}
Also used : Request(io.airlift.http.client.Request) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) Test(org.testng.annotations.Test)

Example 18 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse in project airlift by airlift.

the class TestJaxrsTestingHttpProcessor method test.

@Test
public void test() {
    Request request = prepareGet().setUri(URI.create("http://fake.invalid/get-it/get/xyz")).setHeader("X-Test", "abc").build();
    StringResponse response = HTTP_CLIENT.execute(request, createStringResponseHandler());
    assertEquals(response.getStatusCode(), HttpStatus.OK.code());
    assertEquals(response.getBody(), "Got xyz");
    assertEquals(response.getHeader("X-Test-Out"), "Got abc");
}
Also used : Request(io.airlift.http.client.Request) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) Test(org.testng.annotations.Test)

Example 19 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse in project airlift by airlift.

the class TestMBeanResource method jsonRequest.

private JsonNode jsonRequest(URI uri) throws IOException {
    Request request = prepareGet().setUri(uri).build();
    StringResponse response = client.execute(request, createStringResponseHandler());
    assertEquals(response.getStatusCode(), 200, response.getBody());
    assertContentType(response, JSON_UTF_8);
    return new ObjectMapperProvider().get().readTree(response.getBody());
}
Also used : Request(io.airlift.http.client.Request) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider)

Example 20 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse in project airlift by airlift.

the class TestMBeanResource method jsonpRequest.

private JsonNode jsonpRequest(URI uri) throws IOException {
    uri = uriBuilderFrom(uri).addParameter("jsonp", "test").build();
    Request request = prepareGet().setUri(uri).build();
    StringResponse response = client.execute(request, createStringResponseHandler());
    assertEquals(response.getStatusCode(), 200, response.getBody());
    assertContentType(response, JSON_UTF_8);
    String jsonp = response.getBody().trim();
    assertTrue(jsonp.startsWith("test("), jsonp);
    assertTrue(jsonp.endsWith(")"), jsonp);
    jsonp = jsonp.substring(5, jsonp.length() - 1);
    return new ObjectMapperProvider().get().readTree(jsonp);
}
Also used : Request(io.airlift.http.client.Request) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider)

Aggregations

StringResponse (io.airlift.http.client.StringResponseHandler.StringResponse)21 Test (org.testng.annotations.Test)13 JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)7 Request (io.airlift.http.client.Request)6 HttpClient (io.airlift.http.client.HttpClient)4 HttpUriBuilder (io.airlift.http.client.HttpUriBuilder)3 URI (java.net.URI)3 MediaType (com.google.common.net.MediaType)2 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)2 File (java.io.File)2 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ListMultimap (com.google.common.collect.ListMultimap)1 CONTENT_TYPE (com.google.common.net.HttpHeaders.CONTENT_TYPE)1 Injector (com.google.inject.Injector)1 Bootstrap (io.airlift.bootstrap.Bootstrap)1 AbstractEventClient (io.airlift.event.client.AbstractEventClient)1 EventClient (io.airlift.event.client.EventClient)1 InMemoryEventModule (io.airlift.event.client.InMemoryEventModule)1