|
1 | 1 | import os |
2 | 2 | from http.client import HTTPConnection |
| 3 | +import pytest |
3 | 4 |
|
4 | 5 | PORT = os.getenv('TEST_PORT', 8888) |
5 | 6 | TOKEN = os.getenv('JUPYTER_TOKEN', 'secret') |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def request_get(port, path, token, host='localhost'): |
9 | 10 | h = HTTPConnection(host, port, 10) |
10 | | - h.request('GET', '{}?token={}'.format(path, token)) |
| 11 | + if '?' in path: |
| 12 | + url = '{}&token={}'.format(path, token) |
| 13 | + else: |
| 14 | + url = '{}?token={}'.format(path, token) |
| 15 | + h.request('GET', url) |
11 | 16 | return h.getresponse() |
12 | 17 |
|
13 | 18 |
|
@@ -59,22 +64,36 @@ def test_server_proxy_port_absolute(): |
59 | 64 | assert 'X-Proxycontextpath' not in s |
60 | 65 |
|
61 | 66 |
|
62 | | -def test_server_proxy_indexpage_index(): |
63 | | - r = request_get(PORT, '/python-http-indexpage/', TOKEN) |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "requestpath,expected", [ |
| 69 | + ('/', '/index.html?token='), |
| 70 | + ('/?q=1', '/index.html?q=1&token='), |
| 71 | + ('/pqr?q=2', '/pqr?q=2&token='), |
| 72 | + ] |
| 73 | +) |
| 74 | +def test_server_proxy_mappath_dict(requestpath, expected): |
| 75 | + r = request_get(PORT, '/python-http-mappath' + requestpath, TOKEN) |
64 | 76 | assert r.code == 200 |
65 | 77 | s = r.read().decode('ascii') |
66 | | - assert s.startswith('GET /index.html?token=') |
67 | | - assert 'X-Forwarded-Context: /python-http-indexpage\n' in s |
68 | | - assert 'X-Proxycontextpath: /python-http-indexpage\n' in s |
69 | | - |
70 | | - |
71 | | -def test_server_proxy_indexpage_other(): |
72 | | - r = request_get(PORT, '/python-http-indexpage/pqr', TOKEN) |
| 78 | + assert s.startswith('GET ' + expected) |
| 79 | + assert 'X-Forwarded-Context: /python-http-mappath\n' in s |
| 80 | + assert 'X-Proxycontextpath: /python-http-mappath\n' in s |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize( |
| 84 | + "requestpath,expected", [ |
| 85 | + ('/', '/mapped?token='), |
| 86 | + ('/?q=1', '/mapped?q=1&token='), |
| 87 | + ('/stu?q=2', '/stumapped?q=2&token='), |
| 88 | + ] |
| 89 | +) |
| 90 | +def test_server_proxy_mappath_callable(requestpath, expected): |
| 91 | + r = request_get(PORT, '/python-http-mappathf' + requestpath, TOKEN) |
73 | 92 | assert r.code == 200 |
74 | 93 | s = r.read().decode('ascii') |
75 | | - assert s.startswith('GET /pqr?token=') |
76 | | - assert 'X-Forwarded-Context: /python-http-indexpage\n' in s |
77 | | - assert 'X-Proxycontextpath: /python-http-indexpage\n' in s |
| 94 | + assert s.startswith('GET ' + expected) |
| 95 | + assert 'X-Forwarded-Context: /python-http-mappathf\n' in s |
| 96 | + assert 'X-Proxycontextpath: /python-http-mappathf\n' in s |
78 | 97 |
|
79 | 98 |
|
80 | 99 | def test_server_proxy_remote(): |
|
0 commit comments