|
30 | 30 | import io.netty.handler.codec.http.HttpResponseDecoder; |
31 | 31 | import io.netty.handler.codec.http.HttpResponseEncoder; |
32 | 32 | import io.netty.handler.codec.http.HttpServerCodec; |
| 33 | +import io.netty.handler.codec.http.HttpVersion; |
33 | 34 | import io.netty.util.ReferenceCountUtil; |
34 | 35 | import io.netty.util.ReferenceCounted; |
35 | 36 | import org.hamcrest.CoreMatchers; |
36 | 37 | import org.junit.jupiter.api.Test; |
| 38 | +import org.junit.jupiter.api.function.Executable; |
37 | 39 |
|
38 | 40 | import java.util.Iterator; |
39 | 41 |
|
40 | | -import static io.netty.handler.codec.http.HttpVersion.*; |
| 42 | +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
41 | 43 | import static org.hamcrest.MatcherAssert.assertThat; |
42 | 44 | import static org.junit.jupiter.api.Assertions.assertEquals; |
43 | 45 | import static org.junit.jupiter.api.Assertions.assertFalse; |
44 | 46 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
45 | 49 | import static org.junit.jupiter.api.Assertions.fail; |
46 | 50 |
|
47 | 51 | public class WebSocketServerHandshaker13Test extends WebSocketServerHandshakerTest { |
@@ -91,6 +95,83 @@ public void testCloseReasonWithCodec() { |
91 | 95 | testCloseReason0(new HttpServerCodec()); |
92 | 96 | } |
93 | 97 |
|
| 98 | + @Test |
| 99 | + public void testHandshakeExceptionWhenConnectionHeaderIsAbsent() { |
| 100 | + final WebSocketServerHandshaker serverHandshaker = newHandshaker("ws://example.com/chat", |
| 101 | + "chat", WebSocketDecoderConfig.DEFAULT); |
| 102 | + final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, |
| 103 | + "ws://example.com/chat"); |
| 104 | + request.headers() |
| 105 | + .set(HttpHeaderNames.HOST, "server.example.com") |
| 106 | + .set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET) |
| 107 | + .set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==") |
| 108 | + .set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com") |
| 109 | + .set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat") |
| 110 | + .set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13"); |
| 111 | + Throwable exception = assertThrows(WebSocketServerHandshakeException.class, new Executable() { |
| 112 | + @Override |
| 113 | + public void execute() throws Throwable { |
| 114 | + serverHandshaker.handshake(null, request, null, null); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + assertEquals("not a WebSocket request: a |Connection| header must includes a token 'Upgrade'", |
| 119 | + exception.getMessage()); |
| 120 | + assertTrue(request.release()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testHandshakeExceptionWhenInvalidConnectionHeader() { |
| 125 | + final WebSocketServerHandshaker serverHandshaker = newHandshaker("ws://example.com/chat", |
| 126 | + "chat", WebSocketDecoderConfig.DEFAULT); |
| 127 | + final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, |
| 128 | + "ws://example.com/chat"); |
| 129 | + request.headers() |
| 130 | + .set(HttpHeaderNames.HOST, "server.example.com") |
| 131 | + .set(HttpHeaderNames.CONNECTION, "close") |
| 132 | + .set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET) |
| 133 | + .set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==") |
| 134 | + .set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com") |
| 135 | + .set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat") |
| 136 | + .set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13"); |
| 137 | + Throwable exception = assertThrows(WebSocketServerHandshakeException.class, new Executable() { |
| 138 | + @Override |
| 139 | + public void execute() throws Throwable { |
| 140 | + serverHandshaker.handshake(null, request, null, null); |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + assertEquals("not a WebSocket request: a |Connection| header must includes a token 'Upgrade'", |
| 145 | + exception.getMessage()); |
| 146 | + assertTrue(request.release()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testHandshakeExceptionWhenInvalidUpgradeHeader() { |
| 151 | + final WebSocketServerHandshaker serverHandshaker = newHandshaker("ws://example.com/chat", |
| 152 | + "chat", WebSocketDecoderConfig.DEFAULT); |
| 153 | + final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, |
| 154 | + "ws://example.com/chat"); |
| 155 | + request.headers() |
| 156 | + .set(HttpHeaderNames.HOST, "server.example.com") |
| 157 | + .set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE) |
| 158 | + .set(HttpHeaderNames.UPGRADE, "my_websocket") |
| 159 | + .set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==") |
| 160 | + .set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com") |
| 161 | + .set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat") |
| 162 | + .set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13"); |
| 163 | + Throwable exception = assertThrows(WebSocketServerHandshakeException.class, new Executable() { |
| 164 | + @Override |
| 165 | + public void execute() throws Throwable { |
| 166 | + serverHandshaker.handshake(null, request, null, null); |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + assertEquals("not a WebSocket request: a |Upgrade| header must containing the value 'websocket'", |
| 171 | + exception.getMessage()); |
| 172 | + assertTrue(request.release()); |
| 173 | + } |
| 174 | + |
94 | 175 | private static void testCloseReason0(ChannelHandler... handlers) { |
95 | 176 | EmbeddedChannel ch = new EmbeddedChannel( |
96 | 177 | new HttpObjectAggregator(42)); |
|
0 commit comments