Search in sources :

Example 51 with DefaultFullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.

the class Http2FrameCodecTest method upgradeEventNoRefCntError.

@Test
public void upgradeEventNoRefCntError() throws Exception {
    frameInboundWriter.writeInboundHeaders(Http2CodecUtil.HTTP_UPGRADE_STREAM_ID, request, 31, false);
    // Using reflect as the constructor is package-private and the class is final.
    Constructor<UpgradeEvent> constructor = UpgradeEvent.class.getDeclaredConstructor(CharSequence.class, FullHttpRequest.class);
    // Check if we could make it accessible which may fail on java9.
    Assumptions.assumeTrue(ReflectionUtil.trySetAccessible(constructor, true) == null);
    HttpServerUpgradeHandler.UpgradeEvent upgradeEvent = constructor.newInstance("HTTP/2", new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
    channel.pipeline().fireUserEventTriggered(upgradeEvent);
    assertEquals(1, upgradeEvent.refCnt());
}
Also used : UpgradeEvent(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent) UpgradeEvent(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) Test(org.junit.jupiter.api.Test)

Example 52 with DefaultFullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.

the class Http2StreamFrameToHttpObjectCodecTest method testEncodeHttpsSchemeWhenSslHandlerExists.

@Test
public void testEncodeHttpsSchemeWhenSslHandlerExists() throws Exception {
    final Queue<Http2StreamFrame> frames = new ConcurrentLinkedQueue<Http2StreamFrame>();
    final SslContext ctx = SslContextBuilder.forClient().sslProvider(SslProvider.JDK).build();
    EmbeddedChannel ch = new EmbeddedChannel(ctx.newHandler(ByteBufAllocator.DEFAULT), new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (msg instanceof Http2StreamFrame) {
                frames.add((Http2StreamFrame) msg);
                ctx.write(Unpooled.EMPTY_BUFFER, promise);
            } else {
                ctx.write(msg, promise);
            }
        }
    }, new Http2StreamFrameToHttpObjectCodec(false));
    try {
        FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/hello/world");
        assertTrue(ch.writeOutbound(req));
        ch.finishAndReleaseAll();
        Http2HeadersFrame headersFrame = (Http2HeadersFrame) frames.poll();
        Http2Headers headers = headersFrame.headers();
        assertThat(headers.scheme().toString(), is("https"));
        assertThat(headers.method().toString(), is("GET"));
        assertThat(headers.path().toString(), is("/hello/world"));
        assertTrue(headersFrame.isEndStream());
        assertNull(frames.poll());
    } finally {
        ch.finishAndReleaseAll();
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) EncoderException(io.netty.handler.codec.EncoderException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.jupiter.api.Test)

Example 53 with DefaultFullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.

the class Http2StreamFrameToHttpObjectCodecTest method testEncodeNonEmptyFullRequest.

@Test
public void testEncodeNonEmptyFullRequest() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
    assertTrue(ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "/hello/world", hello)));
    Http2HeadersFrame headersFrame = ch.readOutbound();
    Http2Headers headers = headersFrame.headers();
    assertThat(headers.scheme().toString(), is("http"));
    assertThat(headers.method().toString(), is("PUT"));
    assertThat(headers.path().toString(), is("/hello/world"));
    assertFalse(headersFrame.isEndStream());
    Http2DataFrame dataFrame = ch.readOutbound();
    try {
        assertThat(dataFrame.content().toString(CharsetUtil.UTF_8), is("hello world"));
        assertTrue(dataFrame.isEndStream());
    } finally {
        dataFrame.release();
    }
    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 54 with DefaultFullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.

the class WebSocketServerHandshaker00Test method testPerformHandshakeWithoutOriginHeader.

@Test
public void testPerformHandshakeWithoutOriginHeader() {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
    FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII));
    req.headers().set(HttpHeaderNames.HOST, "server.example.com");
    req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
    req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1  46546xW%0l 1 5");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
    WebSocketServerHandshaker00 handshaker00 = new WebSocketServerHandshaker00("ws://example.com/chat", "chat", Integer.MAX_VALUE);
    try {
        handshaker00.handshake(ch, req);
        fail("Expecting WebSocketHandshakeException");
    } catch (WebSocketHandshakeException e) {
        assertEquals("Missing origin header, got only " + "[host, upgrade, connection, sec-websocket-key1, sec-websocket-protocol]", e.getMessage());
    } finally {
        req.release();
    }
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Example 55 with DefaultFullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.

the class WebSocketServerHandshaker00Test method testPerformOpeningHandshake0.

private static void testPerformOpeningHandshake0(boolean subProtocol) {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
    FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII));
    req.headers().set(HttpHeaderNames.HOST, "server.example.com");
    req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
    req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
    req.headers().set(HttpHeaderNames.ORIGIN, "http://example.com");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1  46546xW%0l 1 5");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1  .P00");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
    if (subProtocol) {
        new WebSocketServerHandshaker00("ws://example.com/chat", "chat", Integer.MAX_VALUE).handshake(ch, req);
    } else {
        new WebSocketServerHandshaker00("ws://example.com/chat", null, Integer.MAX_VALUE).handshake(ch, req);
    }
    EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
    ch2.writeInbound(ch.readOutbound());
    HttpResponse res = ch2.readInbound();
    assertEquals("ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION));
    if (subProtocol) {
        assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    } else {
        assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    }
    LastHttpContent content = ch2.readInbound();
    assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));
    content.release();
    req.release();
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)215 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)117 Test (org.junit.jupiter.api.Test)72 Test (org.junit.Test)61 ByteBuf (io.netty.buffer.ByteBuf)56 HttpRequest (io.netty.handler.codec.http.HttpRequest)47 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)43 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)40 AsciiString (io.netty.util.AsciiString)30 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)23 Channel (io.netty.channel.Channel)20 ChannelPromise (io.netty.channel.ChannelPromise)19 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 IOException (java.io.IOException)17 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)15 HttpResponse (io.netty.handler.codec.http.HttpResponse)15 URI (java.net.URI)15 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)14 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)12