Search in sources :

Example 61 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project camel by apache.

the class NettyHttpProducerSimpleTest method testHttpSimpleExchange.

@Test
public void testHttpSimpleExchange() throws Exception {
    getMockEndpoint("mock:input").expectedBodiesReceived("Hello World");
    Exchange out = template.request("netty4-http:http://localhost:{{port}}/foo", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello World");
        }
    });
    assertNotNull(out);
    assertTrue(out.hasOut());
    NettyHttpMessage response = out.getOut(NettyHttpMessage.class);
    assertNotNull(response);
    assertEquals(200, response.getHttpResponse().status().code());
    // we can also get the response as body
    HttpResponse body = out.getOut().getBody(HttpResponse.class);
    assertNotNull(body);
    assertEquals(200, body.status().code());
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 62 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project camel by apache.

the class NettyHttpAccessHttpRequestAndResponseBeanTest method myTransformer.

/**
     * We can use both a netty http request and response type for transformation
     */
public static HttpResponse myTransformer(FullHttpRequest request) {
    String in = request.content().toString(Charset.forName("UTF-8"));
    String reply = "Bye " + in;
    request.content().release();
    HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, NettyConverter.toByteBuffer(reply.getBytes()));
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH.toString(), reply.length());
    return response;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 63 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project apn-proxy by apn-proxy.

the class TestHttpClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    //        }
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        TestResultHolder.httpStatusCode(response.getStatus().code());
    }
    if (msg instanceof LastHttpContent) {
        ctx.close();
    }
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 64 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project elasticsearch by elastic.

the class Netty4HttpChannelTests method testThatStringLiteralWorksOnMatch.

public void testThatStringLiteralWorksOnMatch() {
    final String originValue = "remote-host";
    Settings settings = Settings.builder().put(SETTING_CORS_ENABLED.getKey(), true).put(SETTING_CORS_ALLOW_ORIGIN.getKey(), originValue).put(SETTING_CORS_ALLOW_METHODS.getKey(), "get, options, post").put(SETTING_CORS_ALLOW_CREDENTIALS.getKey(), true).build();
    HttpResponse response = executeRequest(settings, originValue, "request-host");
    // inspect response and validate
    assertThat(response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN), notNullValue());
    String allowedOrigins = response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat(allowedOrigins, is(originValue));
    assertThat(response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS), equalTo("true"));
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings)

Example 65 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project elasticsearch by elastic.

the class Netty4HttpChannelTests method testThatAnyOriginWorks.

public void testThatAnyOriginWorks() {
    final String originValue = Netty4CorsHandler.ANY_ORIGIN;
    Settings settings = Settings.builder().put(SETTING_CORS_ENABLED.getKey(), true).put(SETTING_CORS_ALLOW_ORIGIN.getKey(), originValue).build();
    HttpResponse response = executeRequest(settings, originValue, "request-host");
    // inspect response and validate
    assertThat(response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN), notNullValue());
    String allowedOrigins = response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat(allowedOrigins, is(originValue));
    assertThat(response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS), nullValue());
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)127 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)57 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)30 HttpRequest (io.netty.handler.codec.http.HttpRequest)27 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)26 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)24 HttpContent (io.netty.handler.codec.http.HttpContent)18 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)18 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 ChannelFuture (io.netty.channel.ChannelFuture)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 ByteBuf (io.netty.buffer.ByteBuf)10 AsciiString (io.netty.util.AsciiString)9 HttpObject (io.netty.handler.codec.http.HttpObject)7 Map (java.util.Map)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 WebSocketExtensionData (io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData)6 IOException (java.io.IOException)6 Settings (org.elasticsearch.common.settings.Settings)6