Search in sources :

Example 31 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class CorsHandlerTest method preflightRequestWithoutConnectionShouldStayOpen.

@Test
public void preflightRequestWithoutConnectionShouldStayOpen() throws Exception {
    final CorsConfig config = forOrigin("http://localhost:8888").build();
    final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config));
    final FullHttpRequest request = optionsRequest("http://localhost:8888", "", null);
    assertThat(channel.writeInbound(request), is(false));
    final HttpResponse response = channel.readOutbound();
    assertThat(HttpUtil.isKeepAlive(response), is(true));
    assertThat(channel.isOpen(), is(true));
    assertThat(response.status(), is(OK));
    assertThat(ReferenceCountUtil.release(response), is(true));
    assertThat(channel.finish(), is(false));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 32 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class CorsHandlerTest method preflightRequestWithConnectionKeepAliveShouldStayOpen.

@Test
public void preflightRequestWithConnectionKeepAliveShouldStayOpen() throws Exception {
    final CorsConfig config = forOrigin("http://localhost:8888").build();
    final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config));
    final FullHttpRequest request = optionsRequest("http://localhost:8888", "", KEEP_ALIVE);
    assertThat(channel.writeInbound(request), is(false));
    final HttpResponse response = channel.readOutbound();
    assertThat(HttpUtil.isKeepAlive(response), is(true));
    assertThat(channel.isOpen(), is(true));
    assertThat(response.status(), is(OK));
    assertThat(ReferenceCountUtil.release(response), is(true));
    assertThat(channel.finish(), is(false));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 33 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class CorsHandlerTest method preflightRequestShouldReleaseRequest.

@Test
public void preflightRequestShouldReleaseRequest() {
    final CorsConfig config = forOrigin("http://localhost:8888").preflightResponseHeader("CustomHeader", Arrays.asList("value1", "value2")).build();
    final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config));
    final FullHttpRequest request = optionsRequest("http://localhost:8888", "content-type, xheader1", null);
    assertThat(channel.writeInbound(request), is(false));
    assertThat(request.refCnt(), is(0));
    assertThat(ReferenceCountUtil.release(channel.readOutbound()), is(true));
    assertThat(channel.finish(), is(false));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 34 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class CorsHandlerTest method forbiddenShouldReleaseRequest.

@Test
public void forbiddenShouldReleaseRequest() {
    final CorsConfig config = forOrigin("https://localhost").shortCircuit().build();
    final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config), new EchoHandler());
    final FullHttpRequest request = createHttpRequest(GET);
    request.headers().set(ORIGIN, "http://localhost:8888");
    assertThat(channel.writeInbound(request), is(false));
    assertThat(request.refCnt(), is(0));
    assertThat(ReferenceCountUtil.release(channel.readOutbound()), is(true));
    assertThat(channel.finish(), is(false));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 35 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class CorsHandlerTest method simpleRequest.

private static HttpResponse simpleRequest(final CorsConfig config, final String origin, final String requestHeaders, final HttpMethod method) {
    final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config), new EchoHandler());
    final FullHttpRequest httpRequest = createHttpRequest(method);
    if (origin != null) {
        httpRequest.headers().set(ORIGIN, origin);
    }
    if (requestHeaders != null) {
        httpRequest.headers().set(ACCESS_CONTROL_REQUEST_HEADERS, requestHeaders);
    }
    assertThat(channel.writeInbound(httpRequest), is(false));
    return (HttpResponse) channel.readOutbound();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)97 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)65 Test (org.junit.Test)51 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)32 AsciiString (io.netty.util.AsciiString)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 ByteBuf (io.netty.buffer.ByteBuf)19 ChannelPromise (io.netty.channel.ChannelPromise)16 HttpResponse (io.netty.handler.codec.http.HttpResponse)13 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)12 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)11 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)10 URI (java.net.URI)10 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)9 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)8 ChannelFuture (io.netty.channel.ChannelFuture)7 Channel (io.netty.channel.Channel)6 NullDispatcher (org.elasticsearch.http.NullDispatcher)6 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)5