use of org.apache.flink.shaded.netty4.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();
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();
}
use of org.apache.flink.shaded.netty4.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();
assertFalse(ch.finish());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.
the class WebSocketServerProtocolHandlerTest method writeUpgradeRequest.
private static void writeUpgradeRequest(EmbeddedChannel ch, boolean full) {
HttpRequest request = WebSocketRequestBuilder.successful();
if (full) {
ch.writeInbound(request);
} else {
if (request instanceof FullHttpRequest) {
FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
HttpRequest req = new DefaultHttpRequest(fullHttpRequest.protocolVersion(), fullHttpRequest.method(), fullHttpRequest.uri(), fullHttpRequest.headers().copy());
ch.writeInbound(req);
ch.writeInbound(new DefaultHttpContent(fullHttpRequest.content().copy()));
ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
fullHttpRequest.release();
} else {
ch.writeInbound(request);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.
the class HttpPostRequestDecoderTest method testDecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleHi.
@Test
public void testDecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleHi() {
byte[] bodyBytes = "foo=bar&a=b&empty=%Zc&city=london".getBytes();
ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
content.writeBytes(bodyBytes);
FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
try {
new HttpPostRequestDecoder(req);
fail("Was expecting an ErrorDataDecoderException");
} catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
assertEquals("Invalid hex byte at index '0' in string: '%Zc'", e.getMessage());
} finally {
assertTrue(req.release());
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.
the class HttpPostRequestDecoderTest method testDecodeMultipartRequest.
@Test
public void testDecodeMultipartRequest() {
byte[] bodyBytes = ("--be38b42a9ad2713f\n" + "content-disposition: form-data; name=\"title\"\n" + "content-length: 10\n" + "content-type: text/plain; charset=UTF-8\n" + "\n" + "bar-stream\n" + "--be38b42a9ad2713f\n" + "content-disposition: form-data; name=\"data\"; filename=\"data.json\"\n" + "content-length: 16\n" + "content-type: application/json; charset=UTF-8\n" + "\n" + "{\"title\":\"Test\"}\n" + "--be38b42a9ad2713f--").getBytes();
ByteBuf content = Unpooled.directBuffer(bodyBytes.length);
content.writeBytes(bodyBytes);
FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
req.headers().add("Content-Type", "multipart/form-data;boundary=be38b42a9ad2713f");
try {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), req);
assertEquals(2, decoder.getBodyHttpDatas().size());
InterfaceHttpData data = decoder.getBodyHttpData("title");
assertTrue(data instanceof MemoryAttribute);
assertEquals("bar-stream", ((MemoryAttribute) data).getString());
assertTrue(data.release());
data = decoder.getBodyHttpData("data");
assertTrue(data instanceof MemoryFileUpload);
assertEquals("{\"title\":\"Test\"}", ((MemoryFileUpload) data).getString());
assertTrue(data.release());
decoder.destroy();
} catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
fail("Was not expecting an exception");
} finally {
assertTrue(req.release());
}
}
Aggregations