Search in sources :

Example 56 with HttpResponse

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

the class WebSocketServerExtensionHandlerTest method testCompatibleExtensionTogetherSuccess.

@Test
public void testCompatibleExtensionTogetherSuccess() {
    // initialize
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(mainExtensionMock);
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(null);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(fallbackExtensionMock);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(null);
    when(mainExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
    when(mainExtensionMock.newReponseData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
    when(mainExtensionMock.newExtensionEncoder()).thenReturn(new DummyEncoder());
    when(mainExtensionMock.newExtensionDecoder()).thenReturn(new DummyDecoder());
    when(fallbackExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV2);
    when(fallbackExtensionMock.newReponseData()).thenReturn(new WebSocketExtensionData("fallback", Collections.<String, String>emptyMap()));
    when(fallbackExtensionMock.newExtensionEncoder()).thenReturn(new Dummy2Encoder());
    when(fallbackExtensionMock.newExtensionDecoder()).thenReturn(new Dummy2Decoder());
    // execute
    WebSocketServerExtensionHandler extensionHandler = new WebSocketServerExtensionHandler(mainHandshakerMock, fallbackHandshakerMock);
    EmbeddedChannel ch = new EmbeddedChannel(extensionHandler);
    HttpRequest req = newUpgradeRequest("main, fallback");
    ch.writeInbound(req);
    HttpResponse res = newUpgradeResponse(null);
    ch.writeOutbound(res);
    HttpResponse res2 = ch.readOutbound();
    List<WebSocketExtensionData> resExts = WebSocketExtensionUtil.extractExtensions(res2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    // test
    assertNull(ch.pipeline().context(extensionHandler));
    assertEquals(2, resExts.size());
    assertEquals("main", resExts.get(0).name());
    assertEquals("fallback", resExts.get(1).name());
    assertNotNull(ch.pipeline().get(DummyDecoder.class));
    assertNotNull(ch.pipeline().get(DummyEncoder.class));
    assertNotNull(ch.pipeline().get(Dummy2Decoder.class));
    assertNotNull(ch.pipeline().get(Dummy2Encoder.class));
    verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("main"));
    verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(fallbackHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(mainExtensionMock, times(2)).rsv();
    verify(mainExtensionMock).newReponseData();
    verify(mainExtensionMock).newExtensionEncoder();
    verify(mainExtensionMock).newExtensionDecoder();
    verify(fallbackExtensionMock, times(2)).rsv();
    verify(fallbackExtensionMock).newReponseData();
    verify(fallbackExtensionMock).newExtensionEncoder();
    verify(fallbackExtensionMock).newExtensionDecoder();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 57 with HttpResponse

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

the class WebSocketServerExtensionHandlerTest method testNoneExtensionMatchingSuccess.

@Test
public void testNoneExtensionMatchingSuccess() {
    // initialize
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("unknown"))).thenReturn(null);
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("unknown2"))).thenReturn(null);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("unknown"))).thenReturn(null);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("unknown2"))).thenReturn(null);
    // execute
    WebSocketServerExtensionHandler extensionHandler = new WebSocketServerExtensionHandler(mainHandshakerMock, fallbackHandshakerMock);
    EmbeddedChannel ch = new EmbeddedChannel(extensionHandler);
    HttpRequest req = newUpgradeRequest("unknown, unknown2");
    ch.writeInbound(req);
    HttpResponse res = newUpgradeResponse(null);
    ch.writeOutbound(res);
    HttpResponse res2 = ch.readOutbound();
    // test
    assertNull(ch.pipeline().context(extensionHandler));
    assertFalse(res2.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown"));
    verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown2"));
    verify(fallbackHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown"));
    verify(fallbackHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown2"));
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 58 with HttpResponse

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

the class CorsHandlerTest method simpleRequestShortCircuit.

@Test
public void simpleRequestShortCircuit() {
    final CorsConfig config = forOrigin("http://localhost:8080").shortCircuit().build();
    final HttpResponse response = simpleRequest(config, "http://localhost:7777");
    assertThat(response.status(), is(FORBIDDEN));
    assertThat(response.headers().get(CONTENT_LENGTH), is("0"));
    assertThat(ReferenceCountUtil.release(response), is(true));
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 59 with HttpResponse

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

the class CorsHandlerTest method preflightRequestWithConnectionKeepAliveShouldStayOpen.

@Test
public void preflightRequestWithConnectionKeepAliveShouldStayOpen() throws Exception {
    final CorsConfig config = forOrigin("http://localhost:8888").build();
    final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config));
    final FullHttpRequest request = optionsRequest("http://localhost:8888", "", KEEP_ALIVE);
    assertThat(channel.writeInbound(request), is(false));
    final HttpResponse response = channel.readOutbound();
    assertThat(HttpUtil.isKeepAlive(response), is(true));
    assertThat(channel.isOpen(), is(true));
    assertThat(response.status(), is(OK));
    assertThat(ReferenceCountUtil.release(response), is(true));
    assertThat(channel.finish(), is(false));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 60 with HttpResponse

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

the class CorsHandlerTest method simpleRequestAllowPrivateNetwork.

@Test
public void simpleRequestAllowPrivateNetwork() {
    final CorsConfig config = forOrigin("http://localhost:8888").allowPrivateNetwork().build();
    final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config));
    final FullHttpRequest request = optionsRequest("http://localhost:8888", "", null);
    request.headers().set(ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK, "true");
    assertThat(channel.writeInbound(request), is(false));
    final HttpResponse response = channel.readOutbound();
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK), equalTo("true"));
    assertThat(ReferenceCountUtil.release(response), is(true));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)443 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)164 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)109 HttpRequest (io.netty.handler.codec.http.HttpRequest)104 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)86 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)75 Test (org.junit.Test)73 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)72 Test (org.junit.jupiter.api.Test)70 HttpContent (io.netty.handler.codec.http.HttpContent)66 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)64 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)63 ByteBuf (io.netty.buffer.ByteBuf)58 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)51 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)39 IOException (java.io.IOException)39 ChannelFuture (io.netty.channel.ChannelFuture)35 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)33 Map (java.util.Map)33 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)32