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