def test_mixed_unicode_bytes(self): """ Tests that having the message key be bytes and pattern unicode (or vice-versa) still works. """ # Unicode patterns, byte message router = Router([ route("websocket.connect", consumer_1, path="^/foo/"), include("channels.tests.test_routing.chatroom_routing", path="^/ws/v(?P<version>[0-9]+)"), ]) self.assertRoute( router, channel="websocket.connect", content={"path": b"/boom/"}, consumer=None, ) self.assertRoute( router, channel="websocket.connect", content={"path": b"/foo/"}, consumer=consumer_1, ) self.assertRoute( router, channel="websocket.connect", content={"path": b"/ws/v2/chat/django/"}, consumer=consumer_2, kwargs={"version": "2", "room": "django"}, ) # Byte patterns, unicode message router = Router([ route("websocket.connect", consumer_1, path=b"^/foo/"), include("channels.tests.test_routing.chatroom_routing", path=b"^/ws/v(?P<version>[0-9]+)"), ]) self.assertRoute( router, channel="websocket.connect", content={"path": "/boom/"}, consumer=None, ) self.assertRoute( router, channel="websocket.connect", content={"path": "/foo/"}, consumer=consumer_1, ) self.assertRoute( router, channel="websocket.connect", content={"path": "/ws/v2/chat/django/"}, consumer=consumer_2, kwargs={"version": "2", "room": "django"}, )
def test_include_prefix(self): """ Tests inclusion with a prefix """ router = Router([ include("channels.tests.test_routing.chatroom_routing", path="^/ws/v(?P<version>[0-9]+)"), ]) self.assertRoute( router, channel="websocket.connect", content={"path": "/boom/"}, consumer=None, ) self.assertRoute( router, channel="websocket.connect", content={"path": "/chat/django/"}, consumer=None, ) self.assertRoute( router, channel="websocket.connect", content={"path": "/ws/v2/chat/django/"}, consumer=consumer_2, kwargs={ "version": "2", "room": "django" }, ) self.assertRoute( router, channel="websocket.connect", content={"path": "/ws/v1/mentions/"}, consumer=consumer_3, kwargs={"version": "1"}, ) # Check it works without the ^s too. router = Router([ include("channels.tests.test_routing.chatroom_routing_nolinestart", path="/ws/v(?P<version>[0-9]+)"), ]) self.assertRoute( router, channel="websocket.connect", content={"path": "/ws/v2/chat/django/"}, consumer=consumer_2, kwargs={ "version": "2", "room": "django" }, )
def test_route_class(self): """ Tests route_class with/without prefix """ router = Router([ include("channels.tests.test_routing.class_routing"), ]) self.assertRoute( router, channel="websocket.connect", content={"path": "/foobar/"}, consumer=None, ) self.assertRoute( router, channel="test.channel", content={"path": "/foobar/"}, consumer=TestClassConsumer, ) self.assertRoute( router, channel="test.channel", content={"path": "/"}, consumer=None, )
def test_include(self): """ Tests inclusion without a prefix """ router = Router([ include("channels.tests.test_routing.chatroom_routing"), ]) self.assertRoute( router, channel="websocket.connect", content={"path": "/boom/"}, consumer=None, ) self.assertRoute( router, channel="websocket.connect", content={"path": "/chat/django/"}, consumer=consumer_2, kwargs={"room": "django"}, ) self.assertRoute( router, channel="websocket.connect", content={"path": "/mentions/"}, consumer=consumer_3, kwargs={}, )
def test_filters(self): """ Tests that filters catch things correctly. """ router = Router([ route("http.request", consumer_1, path=r"^/chat/$"), route("http.disconnect", consumer_2), route("http.request", consumer_3), ]) # Filter hit self.assertRoute( router, channel="http.request", content={"path": "/chat/"}, consumer=consumer_1, kwargs={}, ) # Fall-through self.assertRoute( router, channel="http.request", content={}, consumer=consumer_3, kwargs={}, ) self.assertRoute( router, channel="http.request", content={"path": "/liveblog/"}, consumer=consumer_3, kwargs={}, )
def test_dict(self): """ Tests dict expansion """ router = Router({ "http.request": consumer_1, "http.disconnect": consumer_2, }) self.assertRoute( router, channel="http.request", content={}, consumer=consumer_1, kwargs={}, ) self.assertRoute( router, channel="http.request", content={"path": "/chat/"}, consumer=consumer_1, kwargs={}, ) self.assertRoute( router, channel="http.disconnect", content={}, consumer=consumer_2, kwargs={}, )
def test_positional_pattern(self): """ Tests that regexes with positional groups are rejected. """ with self.assertRaises(ValueError): Router([ route("http.request", consumer_1, path=r"^/chat/([^/]+)/$"), ])
def test_channels(self): """ Tests that the router reports channels to listen on correctly """ router = Router([ route("http.request", consumer_1, path=r"^/chat/$"), route("http.disconnect", consumer_2), route("http.request", consumer_3), ]) # Initial check self.assertEqual( router.channels, {"http.request", "http.disconnect"}, ) # Dynamically add route, recheck router.add_route(route("websocket.receive", consumer_1)) self.assertEqual( router.channels, {"http.request", "http.disconnect", "websocket.receive"}, )