Search in sources :

Example 11 with StringResponse

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

the class AbstractHttpClientTest method testResponseBody.

@Test
public void testResponseBody() throws Exception {
    servlet.setResponseBody("body text");
    Request request = prepareGet().setUri(baseURI).build();
    StringResponse response = executeRequest(request, createStringResponseHandler());
    assertEquals(response.getStatusCode(), 200);
    assertEquals(response.getBody(), "body text");
}
Also used : StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) Test(org.testng.annotations.Test)

Example 12 with StringResponse

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

the class StringResponseHandler method handle.

@Override
public StringResponse handle(Request request, Response response) {
    byte[] bytes = readResponseBytes(request, response);
    Charset charset = Optional.ofNullable(response.getHeader(CONTENT_TYPE)).map(MediaType::parse).flatMap(mediaType -> mediaType.charset().toJavaUtil()).orElse(UTF_8);
    return new StringResponse(response.getStatusCode(), response.getHeaders(), new String(bytes, charset));
}
Also used : MediaType(com.google.common.net.MediaType) List(java.util.List) Charset(java.nio.charset.Charset) ResponseHandlerUtils.readResponseBytes(io.airlift.http.client.ResponseHandlerUtils.readResponseBytes) ListMultimap(com.google.common.collect.ListMultimap) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ResponseHandlerUtils.propagate(io.airlift.http.client.ResponseHandlerUtils.propagate) Optional(java.util.Optional) CONTENT_TYPE(com.google.common.net.HttpHeaders.CONTENT_TYPE) Nullable(javax.annotation.Nullable) Charset(java.nio.charset.Charset) MediaType(com.google.common.net.MediaType) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse)

Example 13 with StringResponse

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

the class TestHttpServerModule method testHttpRequestEvent.

@Test
public void testHttpRequestEvent() throws Exception {
    Map<String, String> properties = new ImmutableMap.Builder<String, String>().put("http-server.http.port", "0").put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath()).build();
    SingleUseEventClient eventClient = new SingleUseEventClient();
    Bootstrap app = new Bootstrap(new HttpServerModule(), new TestingNodeModule(), new InMemoryEventModule(), new TraceTokenModule(), binder -> newSetBinder(binder, EventClient.class).addBinding().toInstance(eventClient), binder -> binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(EchoServlet.class).in(Scopes.SINGLETON));
    Injector injector = app.setRequiredConfigurationProperties(properties).doNotInitializeLogging().initialize();
    HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
    EchoServlet echoServlet = (EchoServlet) injector.getInstance(Key.get(Servlet.class, TheServlet.class));
    HttpServer server = injector.getInstance(HttpServer.class);
    server.start();
    URI requestUri = httpServerInfo.getHttpUri().resolve("/my/path");
    String userAgent = "my-user-agent";
    String referrer = "http://www.google.com";
    String token = "this is a trace token";
    String requestBody = Joiner.on(" ").join(nCopies(50, "request"));
    String requestContentType = "request/type";
    int responseCode = 555;
    String responseBody = Joiner.on(" ").join(nCopies(100, "response"));
    String responseContentType = "response/type";
    echoServlet.responseBody = responseBody;
    echoServlet.responseStatusCode = responseCode;
    echoServlet.responseHeaders.put("Content-Type", responseContentType);
    long beforeRequest = System.currentTimeMillis();
    long afterRequest;
    HttpRequestEvent event;
    try (JettyHttpClient client = new JettyHttpClient()) {
        // test servlet bound correctly
        StringResponse response = client.execute(preparePost().setUri(requestUri).addHeader(USER_AGENT, userAgent).addHeader(CONTENT_TYPE, requestContentType).addHeader(REFERER, referrer).addHeader("X-Airlift-TraceToken", token).setBodyGenerator(createStaticBodyGenerator(requestBody, UTF_8)).build(), createStringResponseHandler());
        afterRequest = System.currentTimeMillis();
        assertEquals(response.getStatusCode(), responseCode);
        assertEquals(response.getBody(), responseBody);
        assertEquals(response.getHeader("Content-Type"), responseContentType);
        event = (HttpRequestEvent) eventClient.getEvent().get(10, TimeUnit.SECONDS);
    } finally {
        server.stop();
    }
    assertEquals(event.getClientAddress(), echoServlet.remoteAddress);
    assertEquals(event.getProtocol(), "http");
    assertEquals(event.getMethod(), "POST");
    assertEquals(event.getRequestUri(), requestUri.getPath());
    assertNull(event.getUser());
    assertEquals(event.getAgent(), userAgent);
    assertEquals(event.getReferrer(), referrer);
    assertEquals(event.getTraceToken(), token);
    assertEquals(event.getRequestSize(), requestBody.length());
    assertEquals(event.getRequestContentType(), requestContentType);
    assertEquals(event.getResponseSize(), responseBody.length());
    assertEquals(event.getResponseCode(), responseCode);
    assertEquals(event.getResponseContentType(), responseContentType);
    assertTrue(event.getTimeStamp().toEpochMilli() >= beforeRequest);
    assertTrue(event.getTimeToLastByte() <= afterRequest - beforeRequest);
    assertNotNull(event.getTimeToFirstByte());
    assertTrue(event.getTimeToDispatch() <= event.getTimeToFirstByte());
    assertTrue(event.getTimeToFirstByte() <= event.getTimeToLastByte());
}
Also used : TestingNodeModule(io.airlift.node.testing.TestingNodeModule) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) URI(java.net.URI) ImmutableMap(com.google.common.collect.ImmutableMap) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) AbstractEventClient(io.airlift.event.client.AbstractEventClient) EventClient(io.airlift.event.client.EventClient) InMemoryEventModule(io.airlift.event.client.InMemoryEventModule) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) File(java.io.File) Test(org.testng.annotations.Test)

Example 14 with StringResponse

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

the class TestHttpServerModule method assertRedirect.

private void assertRedirect(URI baseUri, HttpClient client, String path, String redirect) {
    HttpUriBuilder uriBuilder = uriBuilderFrom(baseUri);
    StringResponse response = client.execute(prepareGet().setFollowRedirects(false).setUri(uriBuilder.appendPath(path).build()).build(), createStringResponseHandler());
    assertEquals(response.getStatusCode(), HttpStatus.TEMPORARY_REDIRECT.code());
    assertEquals(response.getHeader(LOCATION), redirect);
    assertNull(response.getHeader(CONTENT_TYPE), CONTENT_TYPE + " header should be absent");
    assertEquals(response.getBody(), "", "Response body");
}
Also used : HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse)

Example 15 with StringResponse

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

the class TestHttpServerProvider method testShowStackTraceDisabled.

@Test
public void testShowStackTraceDisabled() throws Exception {
    config.setShowStackTrace(false);
    createServer(new ErrorServlet());
    server.start();
    try (HttpClient client = new JettyHttpClient()) {
        StringResponse response = client.execute(prepareGet().setUri(httpServerInfo.getHttpUri()).build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), 500);
        assertThat(response.getBody()).doesNotContain("ErrorServlet.java");
    }
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) Test(org.testng.annotations.Test)

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