use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project graylog2-server by Graylog2.
the class HttpHandlerTest method withJSONContentType.
@Test
public void withJSONContentType() {
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/gelf");
httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
httpRequest.content().writeBytes(GELF_MESSAGE);
channel.writeInbound(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.readOutbound();
assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project graylog2-server by Graylog2.
the class HttpHandlerTest method withKeepalive.
@Test
public void withKeepalive() {
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/gelf");
httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
httpRequest.content().writeBytes(GELF_MESSAGE);
channel.writeInbound(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.readOutbound();
assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.KEEP_ALIVE.toString());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project rest.li by linkedin.
the class TestHttp2ProtocolUpgradeHandler method testInitialization.
@Test
public void testInitialization() throws Exception {
Http2UpgradeHandler handler = new Http2UpgradeHandler();
EmbeddedChannel channel = new EmbeddedChannel(handler);
Assert.assertTrue(channel.finish());
Assert.assertEquals(channel.outboundMessages().size(), 1);
DefaultFullHttpRequest message = channel.readOutbound();
Assert.assertNotNull(message);
Assert.assertEquals(message.method(), HttpMethod.OPTIONS);
Assert.assertEquals(message.uri(), PATH);
// 1) any value is ok in the host for the upgrade request
// 2) since we are using the EmbeddedChannel, which uses an EmbeddedSocketAddress and not a InetSocketAddress
// we cannot extract host and port from the channel context and "localhost" is used as default
Assert.assertEquals(message.headers().get(HttpHeaderNames.HOST), "localhost");
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project rest.li by linkedin.
the class Http2ProtocolUpgradeHandler method processChannelActive.
public static void processChannelActive(ChannelHandlerContext ctx, Logger log, ChannelPromise upgradePromise) {
// For an upgrade request, clients should use an OPTIONS request for path “*” or a HEAD request for “/”.
// RFC: https://tools.ietf.org/html/rfc7540#section-3.2
// Implementation detail: https://http2.github.io/faq/#can-i-implement-http2-without-implementing-http11
final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "*");
final String hostname;
if (ctx.channel().remoteAddress() instanceof InetSocketAddress) {
// 1) The documentation of remoteAddress says that it should be down-casted to InetSocketAddress.
// 2) The getHostString doesn't attempt a reverse lookup
InetSocketAddress inetAddress = ((InetSocketAddress) ctx.channel().remoteAddress());
hostname = inetAddress.getHostString() + ":" + inetAddress.getPort();
} else {
// if it is not a InetSocketAddress, it is a DomainSocketAddress, a LocalAddress or a EmbeddedSocketAddress.
// In the R2 stack it should never happen
hostname = "localhost";
log.warn("The remoteAddress is not an InetSocketAddress, therefore it has been used '" + hostname + "'" + " for the HOST of the upgrade request", ctx.channel().remoteAddress());
}
// The host is required given rfc2616 14.23 also for the upgrade request.
// Without it, the host the upgrade request fails
// https://tools.ietf.org/html/rfc2616#section-14.23
request.headers().add(HttpHeaderNames.HOST, hostname);
ctx.writeAndFlush(request);
// Fail the upgrade promise when channel is closed
ctx.channel().closeFuture().addListener(future -> {
if (!upgradePromise.isDone()) {
upgradePromise.setFailure(new ChannelException("HTTP/2 upgrade did not complete before channel closed"));
}
});
ctx.fireChannelActive();
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project carbon-apimgt by wso2.
the class WebsocketInboundHandlerTestCase method testWSHandshakeResponse.
@Test
public void testWSHandshakeResponse() throws Exception {
InboundMessageContext inboundMessageContext = createWebSocketApiMessageContext();
InboundMessageContextDataHolder.getInstance().addInboundMessageContextForConnection(channelIdString, inboundMessageContext);
String headerName = "test-header";
String headerValue = "test-header-value";
String strWebSocket = "websocket";
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
FullHttpRequest fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "ws://localhost:8080/graphql");
fullHttpRequest.headers().set(headerName, headerValue);
fullHttpRequest.headers().set(HttpHeaders.UPGRADE, strWebSocket);
Mockito.when(inboundWebSocketProcessor.handleHandshake(fullHttpRequest, channelHandlerContext, inboundMessageContext)).thenReturn(responseDTO);
websocketInboundHandler.channelRead(channelHandlerContext, fullHttpRequest);
Assert.assertTrue((InboundMessageContextDataHolder.getInstance().getInboundMessageContextMap().containsKey(// No error has occurred context exists in data-holder map.
channelIdString)));
Assert.assertEquals(inboundMessageContext.getRequestHeaders().get(headerName), headerValue);
Assert.assertEquals(inboundMessageContext.getToken(), fullHttpRequest.headers().get(APIMgtGatewayConstants.WS_JWT_TOKEN_HEADER));
Assert.assertEquals(inboundMessageContext.getUserIP(), remoteIP);
// error response
responseDTO.setError(true);
responseDTO.setErrorMessage("error");
fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "ws://localhost:8080/graphql");
Mockito.when(inboundWebSocketProcessor.handleHandshake(fullHttpRequest, channelHandlerContext, inboundMessageContext)).thenReturn(responseDTO);
fullHttpRequest.headers().set(headerName, headerValue);
fullHttpRequest.headers().set(HttpHeaders.UPGRADE, strWebSocket);
websocketInboundHandler.channelRead(channelHandlerContext, fullHttpRequest);
Assert.assertFalse(InboundMessageContextDataHolder.getInstance().getInboundMessageContextMap().containsKey(// Closing connection error has occurred
channelIdString));
Assert.assertFalse(fullHttpRequest.headers().contains(APIMgtGatewayConstants.WS_JWT_TOKEN_HEADER));
}
Aggregations