Search in sources :

Example 71 with FullHttpRequest

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

the class InboundHttp2ToHttpAdapterTest method clientRequestMultipleHeaders.

@Test
public void clientRequestMultipleHeaders() throws Exception {
    boostrapEnv(1, 1, 1);
    // writeHeaders will implicitly add an END_HEADERS tag each time and so this test does not follow the HTTP
    // message flow. We currently accept this message flow and just add the second headers to the trailing headers.
    final String text = "";
    final ByteBuf content = Unpooled.copiedBuffer(text.getBytes());
    final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/path/resource2", content, true);
    try {
        HttpHeaders httpHeaders = request.headers();
        httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
        httpHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, text.length());
        httpHeaders.setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), (short) 16);
        HttpHeaders trailingHeaders = request.trailingHeaders();
        trailingHeaders.set(of("FoO"), of("goo"));
        trailingHeaders.set(of("foO2"), of("goo2"));
        trailingHeaders.add(of("fOo2"), of("goo3"));
        final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).path(new AsciiString("/some/path/resource2"));
        final Http2Headers http2Headers2 = new DefaultHttp2Headers().set(new AsciiString("foo"), new AsciiString("goo")).set(new AsciiString("foo2"), new AsciiString("goo2")).add(new AsciiString("foo2"), new AsciiString("goo3"));
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, false, newPromiseClient());
                clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers2, 0, false, newPromiseClient());
                clientHandler.encoder().writeData(ctxClient(), 3, content.retain(), 0, true, newPromiseClient());
                clientChannel.flush();
            }
        });
        awaitRequests();
        ArgumentCaptor<FullHttpMessage> requestCaptor = ArgumentCaptor.forClass(FullHttpMessage.class);
        verify(serverListener).messageReceived(requestCaptor.capture());
        capturedRequests = requestCaptor.getAllValues();
        assertEquals(request, capturedRequests.get(0));
    } finally {
        request.release();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) AsciiString(io.netty.util.AsciiString) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 72 with FullHttpRequest

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

the class Http2ServerDowngraderTest method testDowngradeHeadersWithContentLength.

@Test
public void testDowngradeHeadersWithContentLength() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
    Http2Headers headers = new DefaultHttp2Headers();
    headers.path("/");
    headers.method("GET");
    headers.setInt("content-length", 0);
    assertTrue(ch.writeInbound(new DefaultHttp2HeadersFrame(headers)));
    HttpRequest request = ch.readInbound();
    assertThat(request.uri(), is("/"));
    assertThat(request.method(), is(HttpMethod.GET));
    assertThat(request.protocolVersion(), is(HttpVersion.HTTP_1_1));
    assertFalse(request instanceof FullHttpRequest);
    assertFalse(HttpUtil.isTransferEncodingChunked(request));
    assertThat(ch.readInbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 73 with FullHttpRequest

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

the class Http2ServerDowngraderTest method testDowngradeHeaders.

@Test
public void testDowngradeHeaders() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
    Http2Headers headers = new DefaultHttp2Headers();
    headers.path("/");
    headers.method("GET");
    assertTrue(ch.writeInbound(new DefaultHttp2HeadersFrame(headers)));
    HttpRequest request = ch.readInbound();
    assertThat(request.uri(), is("/"));
    assertThat(request.method(), is(HttpMethod.GET));
    assertThat(request.protocolVersion(), is(HttpVersion.HTTP_1_1));
    assertFalse(request instanceof FullHttpRequest);
    assertTrue(HttpUtil.isTransferEncodingChunked(request));
    assertThat(ch.readInbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 74 with FullHttpRequest

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

the class Http2ServerDowngraderTest method testDowngradeFullHeaders.

@Test
public void testDowngradeFullHeaders() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
    Http2Headers headers = new DefaultHttp2Headers();
    headers.path("/");
    headers.method("GET");
    assertTrue(ch.writeInbound(new DefaultHttp2HeadersFrame(headers, true)));
    FullHttpRequest request = ch.readInbound();
    try {
        assertThat(request.uri(), is("/"));
        assertThat(request.method(), is(HttpMethod.GET));
        assertThat(request.protocolVersion(), is(HttpVersion.HTTP_1_1));
        assertThat(request.content().readableBytes(), is(0));
        assertTrue(request.trailingHeaders().isEmpty());
        assertFalse(HttpUtil.isTransferEncodingChunked(request));
    } finally {
        request.release();
    }
    assertThat(ch.readInbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 75 with FullHttpRequest

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

the class HttpToHttp2ConnectionHandlerTest method testRequestWithBodyAndTrailingHeaders.

@Test
public void testRequestWithBodyAndTrailingHeaders() throws Exception {
    final String text = "foooooogoooo";
    final List<String> receivedBuffers = Collections.synchronizedList(new ArrayList<String>());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock in) throws Throwable {
            receivedBuffers.add(((ByteBuf) in.getArguments()[2]).toString(UTF_8));
            return null;
        }
    }).when(serverListener).onDataRead(any(ChannelHandlerContext.class), eq(3), any(ByteBuf.class), eq(0), eq(false));
    bootstrapEnv(4, 1, 1);
    final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "http://your_user-name123@www.example.org:5555/example", Unpooled.copiedBuffer(text, UTF_8));
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.set(HttpHeaderNames.HOST, "www.example.org:5555");
    httpHeaders.add(of("foo"), of("goo"));
    httpHeaders.add(of("foo"), of("goo2"));
    httpHeaders.add(of("foo2"), of("goo2"));
    final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("POST")).path(new AsciiString("/example")).authority(new AsciiString("www.example.org:5555")).scheme(new AsciiString("http")).add(new AsciiString("foo"), new AsciiString("goo")).add(new AsciiString("foo"), new AsciiString("goo2")).add(new AsciiString("foo2"), new AsciiString("goo2"));
    request.trailingHeaders().add(of("trailing"), of("bar"));
    final Http2Headers http2TrailingHeaders = new DefaultHttp2Headers().add(new AsciiString("trailing"), new AsciiString("bar"));
    ChannelPromise writePromise = newPromise();
    ChannelFuture writeFuture = clientChannel.writeAndFlush(request, writePromise);
    assertTrue(writePromise.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(writePromise.isSuccess());
    assertTrue(writeFuture.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(writeFuture.isSuccess());
    awaitRequests();
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(3), eq(http2Headers), eq(0), anyShort(), anyBoolean(), eq(0), eq(false));
    verify(serverListener).onDataRead(any(ChannelHandlerContext.class), eq(3), any(ByteBuf.class), eq(0), eq(false));
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(3), eq(http2TrailingHeaders), eq(0), anyShort(), anyBoolean(), eq(0), eq(true));
    assertEquals(1, receivedBuffers.size());
    assertEquals(text, receivedBuffers.get(0));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) AsciiString(io.netty.util.AsciiString) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

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