|
10 | 10 | from channels.testing import HttpCommunicator, WebsocketCommunicator |
11 | 11 |
|
12 | 12 |
|
| 13 | +ValidURLRouter = URLRouter |
| 14 | + |
| 15 | + |
13 | 16 | class SimpleHttpApp(AsyncConsumer): |
14 | 17 | """ |
15 | 18 | Barebones HTTP ASGI app for testing. |
@@ -194,3 +197,44 @@ async def test_connection_scope(path): |
194 | 197 | connected, _ = await communicator.connect() |
195 | 198 | assert connected |
196 | 199 | await communicator.disconnect() |
| 200 | + |
| 201 | + |
| 202 | +@pytest.mark.skip |
| 203 | +@pytest.mark.asyncio |
| 204 | +async def test_route_validator_http(): |
| 205 | + """ |
| 206 | + Ensures ValidURLRouter returns 404 when route can't be matched. |
| 207 | + """ |
| 208 | + router = ValidURLRouter([path("test/", SimpleHttpApp())]) |
| 209 | + communicator = HttpCommunicator(router, "GET", "/test/?foo=bar") |
| 210 | + response = await communicator.get_response() |
| 211 | + assert response["body"] == b"test response" |
| 212 | + assert response["status"] == 200 |
| 213 | + |
| 214 | + communicator = HttpCommunicator(router, "GET", "/not-test/") |
| 215 | + response = await communicator.get_response() |
| 216 | + assert response["body"] == b"404 Not Found" |
| 217 | + assert response["status"] == 404 |
| 218 | + |
| 219 | + |
| 220 | +@pytest.mark.skip |
| 221 | +@pytest.mark.asyncio |
| 222 | +async def test_route_validator_websocket(): |
| 223 | + """ |
| 224 | + Ensures WebSocket connections are closed on unmatched routes. |
| 225 | +
|
| 226 | + Forces ValidURLRouter to return 403 for unmatched routes during the handshake. |
| 227 | + WebSocket clients will receive a 1008 close code. |
| 228 | +
|
| 229 | + Ideally this should result in a 404, but that is not achievable in this context. |
| 230 | + """ |
| 231 | + router = ValidURLRouter([path("testws/", SimpleWebsocketApp())]) |
| 232 | + communicator = WebsocketCommunicator(router, "/testws/") |
| 233 | + connected, subprotocol = await communicator.connect() |
| 234 | + assert connected |
| 235 | + assert subprotocol is None |
| 236 | + |
| 237 | + communicator = WebsocketCommunicator(router, "/not-testws/") |
| 238 | + connected, subprotocol = await communicator.connect() |
| 239 | + assert connected is False |
| 240 | + assert subprotocol == 1008 |
0 commit comments