use of io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class WebSocketClientHandshakerTest method testWebSocketClientHandshakeException.
@Test
public void testWebSocketClientHandshakeException() {
URI uri = URI.create("ws://localhost:9999/exception");
WebSocketClientHandshaker handshaker = newHandshaker(uri, null, null, false);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
response.headers().set(HttpHeaderNames.WWW_AUTHENTICATE, "realm = access token required");
try {
handshaker.finishHandshake(null, response);
} catch (WebSocketClientHandshakeException exception) {
assertEquals("Invalid handshake response getStatus: 401 Unauthorized", exception.getMessage());
assertEquals(HttpResponseStatus.UNAUTHORIZED, exception.response().status());
assertTrue(exception.response().headers().contains(HttpHeaderNames.WWW_AUTHENTICATE, "realm = access token required", false));
} finally {
response.release();
}
}
use of io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class WebSocketClientHandshakerTest method testHttpResponseAndFrameInSameBuffer.
private void testHttpResponseAndFrameInSameBuffer(boolean codec) {
String url = "ws://localhost:9999/ws";
final WebSocketClientHandshaker shaker = newHandshaker(URI.create(url));
final WebSocketClientHandshaker handshaker = new WebSocketClientHandshaker(shaker.uri(), shaker.version(), null, EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE, -1) {
@Override
protected FullHttpRequest newHandshakeRequest() {
return shaker.newHandshakeRequest();
}
@Override
protected void verify(FullHttpResponse response) {
// Not do any verification, so we not need to care sending the correct headers etc in the test,
// which would just make things more complicated.
}
@Override
protected WebSocketFrameDecoder newWebsocketDecoder() {
return shaker.newWebsocketDecoder();
}
@Override
protected WebSocketFrameEncoder newWebSocketEncoder() {
return shaker.newWebSocketEncoder();
}
};
// use randomBytes helper from utils to check that it functions properly
byte[] data = WebSocketUtil.randomBytes(24);
// Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
// to test the actual handshaker.
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(url, null, false);
FullHttpRequest request = shaker.newHandshakeRequest();
WebSocketServerHandshaker socketServerHandshaker = factory.newHandshaker(request);
request.release();
EmbeddedChannel websocketChannel = new EmbeddedChannel(socketServerHandshaker.newWebSocketEncoder(), socketServerHandshaker.newWebsocketDecoder());
assertTrue(websocketChannel.writeOutbound(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(data))));
byte[] bytes = "HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n".getBytes(CharsetUtil.US_ASCII);
CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
compositeByteBuf.addComponent(true, Unpooled.wrappedBuffer(bytes));
for (; ; ) {
ByteBuf frameBytes = websocketChannel.readOutbound();
if (frameBytes == null) {
break;
}
compositeByteBuf.addComponent(true, frameBytes);
}
EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(Integer.MAX_VALUE), new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
handshaker.finishHandshake(ctx.channel(), msg);
ctx.pipeline().remove(this);
}
});
if (codec) {
ch.pipeline().addFirst(new HttpClientCodec());
} else {
ch.pipeline().addFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
}
// We need to first write the request as HttpClientCodec will fail if we receive a response before a request
// was written.
shaker.handshake(ch).syncUninterruptibly();
for (; ; ) {
// Just consume the bytes, we are not interested in these.
ByteBuf buf = ch.readOutbound();
if (buf == null) {
break;
}
buf.release();
}
assertTrue(ch.writeInbound(compositeByteBuf));
assertTrue(ch.finish());
BinaryWebSocketFrame frame = ch.readInbound();
ByteBuf expect = Unpooled.wrappedBuffer(data);
try {
assertEquals(expect, frame.content());
assertTrue(frame.isFinalFragment());
assertEquals(0, frame.rsv());
} finally {
expect.release();
frame.release();
}
}
use of io.netty.handler.codec.http.FullHttpResponse 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 io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class WebSocketServerProtocolHandlerTest method testDoNotCreateUTF8Validator.
@Test
public void testDoNotCreateUTF8Validator() {
WebSocketServerProtocolConfig config = WebSocketServerProtocolConfig.newBuilder().websocketPath("/test").withUTF8Validator(false).build();
EmbeddedChannel ch = new EmbeddedChannel(new WebSocketServerProtocolHandler(config), new HttpRequestDecoder(), new HttpResponseEncoder(), new MockOutboundHandler());
writeUpgradeRequest(ch);
FullHttpResponse response = responses.remove();
assertEquals(SWITCHING_PROTOCOLS, response.status());
response.release();
assertNull(ch.pipeline().get(Utf8FrameValidator.class));
}
use of io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class WebSocketServerProtocolHandlerTest method testHandleTextFrame.
@Test
public void testHandleTextFrame() {
CustomTextFrameHandler customTextFrameHandler = new CustomTextFrameHandler();
EmbeddedChannel ch = createChannel(customTextFrameHandler);
writeUpgradeRequest(ch);
FullHttpResponse response = responses.remove();
assertEquals(SWITCHING_PROTOCOLS, response.status());
response.release();
if (ch.pipeline().context(HttpRequestDecoder.class) != null) {
// Removing the HttpRequestDecoder because we are writing a TextWebSocketFrame and thus
// decoding is not necessary.
ch.pipeline().remove(HttpRequestDecoder.class);
}
ch.writeInbound(new TextWebSocketFrame("payload"));
assertEquals("processed: payload", customTextFrameHandler.getContent());
assertFalse(ch.finish());
}
Aggregations