()
| 14 | |
| 15 | |
| 16 | def test_basic_routing(): |
| 17 | map = r.Map( |
| 18 | [ |
| 19 | r.Rule("/", endpoint="index"), |
| 20 | r.Rule("/foo", endpoint="foo"), |
| 21 | r.Rule("/bar/", endpoint="bar"), |
| 22 | r.Rule("/ws", endpoint="ws", websocket=True), |
| 23 | r.Rule("/", endpoint="indexws", websocket=True), |
| 24 | ] |
| 25 | ) |
| 26 | adapter = map.bind("example.org", "/") |
| 27 | assert adapter.match("/") == ("index", {}) |
| 28 | assert adapter.match("/foo") == ("foo", {}) |
| 29 | assert adapter.match("/bar/") == ("bar", {}) |
| 30 | pytest.raises(r.RequestRedirect, lambda: adapter.match("/bar")) |
| 31 | pytest.raises(NotFound, lambda: adapter.match("/blub")) |
| 32 | |
| 33 | adapter = map.bind("example.org", "/", url_scheme="ws") |
| 34 | assert adapter.match("/") == ("indexws", {}) |
| 35 | |
| 36 | adapter = map.bind("example.org", "/test") |
| 37 | with pytest.raises(r.RequestRedirect) as excinfo: |
| 38 | adapter.match("/bar") |
| 39 | assert excinfo.value.new_url == "http://example.org/test/bar/" |
| 40 | |
| 41 | adapter = map.bind("example.org", "/") |
| 42 | with pytest.raises(r.RequestRedirect) as excinfo: |
| 43 | adapter.match("/bar") |
| 44 | assert excinfo.value.new_url == "http://example.org/bar/" |
| 45 | |
| 46 | adapter = map.bind("example.org", "/") |
| 47 | with pytest.raises(r.RequestRedirect) as excinfo: |
| 48 | adapter.match("/bar", query_args={"aha": "muhaha"}) |
| 49 | assert excinfo.value.new_url == "http://example.org/bar/?aha=muhaha" |
| 50 | |
| 51 | adapter = map.bind("example.org", "/") |
| 52 | with pytest.raises(r.RequestRedirect) as excinfo: |
| 53 | adapter.match("/bar", query_args="aha=muhaha") |
| 54 | assert excinfo.value.new_url == "http://example.org/bar/?aha=muhaha" |
| 55 | |
| 56 | adapter = map.bind_to_environ(create_environ("/bar?foo=bar", "http://example.org/")) |
| 57 | with pytest.raises(r.RequestRedirect) as excinfo: |
| 58 | adapter.match() |
| 59 | assert excinfo.value.new_url == "http://example.org/bar/?foo=bar" |
| 60 | |
| 61 | adapter = map.bind("example.org", "/ws", url_scheme="wss") |
| 62 | assert adapter.match("/ws", websocket=True) == ("ws", {}) |
| 63 | with pytest.raises(r.WebsocketMismatch): |
| 64 | adapter.match("/ws", websocket=False) |
| 65 | with pytest.raises(r.WebsocketMismatch): |
| 66 | adapter.match("/foo", websocket=True) |
| 67 | |
| 68 | adapter = map.bind_to_environ( |
| 69 | create_environ( |
| 70 | "/ws?foo=bar", |
| 71 | "http://example.org/", |
| 72 | headers=[("Connection", "Upgrade"), ("upgrade", "WebSocket")], |
| 73 | ) |
nothing calls this directly
no test coverage detected