Search in sources :

Example 6 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest 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();
    Assert.assertEquals("ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION));
    if (subProtocol) {
        Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    } else {
        Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    }
    LastHttpContent content = ch2.readInbound();
    Assert.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)

Example 7 with FullHttpRequest

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

the class RtspEncoderTest method testSendGetParameterRequest.

/**
     * Test of a GET_PARAMETER request, with body.
     */
@Test
public void testSendGetParameterRequest() {
    String expected = "GET_PARAMETER rtsp://172.10.20.30:554 RTSP/1.0\r\n" + "session: 2547019973447939919\r\n" + "cseq: 3\r\n" + "content-length: 31\r\n" + "content-type: text/parameters\r\n" + "\r\n" + "stream_state\r\n" + "position\r\n" + "scale\r\n";
    byte[] content = ("stream_state\r\n" + "position\r\n" + "scale\r\n").getBytes(CharsetUtil.UTF_8);
    FullHttpRequest request = new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.GET_PARAMETER, "rtsp://172.10.20.30:554");
    request.headers().add(RtspHeaderNames.SESSION, "2547019973447939919");
    request.headers().add(RtspHeaderNames.CSEQ, "3");
    request.headers().add(RtspHeaderNames.CONTENT_LENGTH, "" + content.length);
    request.headers().add(RtspHeaderNames.CONTENT_TYPE, "text/parameters");
    request.content().writeBytes(content);
    EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
    ch.writeOutbound(request);
    ByteBuf buf = ch.readOutbound();
    String actual = buf.toString(CharsetUtil.UTF_8);
    buf.release();
    assertEquals(expected, actual);
}
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) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 8 with FullHttpRequest

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

the class WebSocketClientHandshaker07Test method testSecOriginWss.

@Test
public void testSecOriginWss() {
    URI uri = URI.create("wss://localhost/path%20with%20ws");
    WebSocketClientHandshaker handshaker = newHandshaker(uri);
    FullHttpRequest request = handshaker.newHandshakeRequest();
    try {
        assertEquals("https://localhost", request.headers().get(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN));
    } finally {
        request.release();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 9 with FullHttpRequest

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

the class WebSocketClientHandshakerTest method testRawPath.

@Test
public void testRawPath() {
    URI uri = URI.create("ws://localhost:9999/path%20with%20ws");
    WebSocketClientHandshaker handshaker = newHandshaker(uri);
    FullHttpRequest request = handshaker.newHandshakeRequest();
    try {
        assertEquals("/path%20with%20ws", request.getUri());
    } finally {
        request.release();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 10 with FullHttpRequest

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

the class WebSocketServerProtocolHandlerTest method testHttpUpgradeRequestInvalidUpgradeHeader.

@Test
public void testHttpUpgradeRequestInvalidUpgradeHeader() {
    EmbeddedChannel ch = createChannel();
    FullHttpRequest httpRequestWithEntity = new WebSocketRequestBuilder().httpVersion(HTTP_1_1).method(HttpMethod.GET).uri("/test").connection("Upgrade").version00().upgrade("BogusSocket").build();
    ch.writeInbound(httpRequestWithEntity);
    FullHttpResponse response = responses.remove();
    assertEquals(BAD_REQUEST, response.status());
    assertEquals("not a WebSocket handshake request: missing upgrade", getResponseMessage(response));
    response.release();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) 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