Debian Codegraph @

testing/mozbase/mozhttpd/tests/paths.py

raw
1 #!/usr/bin/env python
2
3 # Any copyright is dedicated to the Public Domain.
4 # http://creativecommons.org/publicdomain/zero/1.0/
5
6 from urllib.error import HTTPError
7 from urllib.request import urlopen
8
9 import mozhttpd
10 import mozunit
11 import pytest
12
13
14 def try_get(url, expected_contents):
15 f = urlopen(url)
16 assert f.getcode() == 200
17 assert f.read() == expected_contents
18
19
20 def try_get_expect_404(url):
21 with pytest.raises(HTTPError) as excinfo:
22 urlopen(url)
23 assert excinfo.value.code == 404
24
25
26 @pytest.fixture(name="httpd_basic")
27 def fixture_httpd_basic(tmpdir):
28 d1 = tmpdir.mkdir("d1")
29 d1.join("test1.txt").write("test 1 contents")
30
31 d2 = tmpdir.mkdir("d2")
32 d2.join("test2.txt").write("test 2 contents")
33
34 httpd = mozhttpd.MozHttpd(
35 port=0,
36 docroot=str(d1),
37 path_mappings={"/files": str(d2)},
38 )
39 httpd.start(block=False)
40
41 yield httpd
42
43 httpd.stop()
44 d1.remove()
45 d2.remove()
46
47
48 def test_basic(httpd_basic):
49 """Test that requests to docroot and a path mapping work as expected."""
50 try_get(httpd_basic.get_url("/test1.txt"), b"test 1 contents")
51 try_get(httpd_basic.get_url("/files/test2.txt"), b"test 2 contents")
52 try_get_expect_404(httpd_basic.get_url("/files/test2_nope.txt"))
53
54
55 @pytest.fixture(name="httpd_substring_mappings")
56 def fixture_httpd_substring_mappings(tmpdir):
57 d1 = tmpdir.mkdir("d1")
58 d1.join("test1.txt").write("test 1 contents")
59
60 d2 = tmpdir.mkdir("d2")
61 d2.join("test2.txt").write("test 2 contents")
62
63 httpd = mozhttpd.MozHttpd(
64 port=0,
65 path_mappings={"/abcxyz": str(d1), "/abc": str(d2)},
66 )
67 httpd.start(block=False)
68 yield httpd
69 httpd.stop()
70 d1.remove()
71 d2.remove()
72
73
74 def test_substring_mappings(httpd_substring_mappings):
75 httpd = httpd_substring_mappings
76 try_get(httpd.get_url("/abcxyz/test1.txt"), b"test 1 contents")
77 try_get(httpd.get_url("/abc/test2.txt"), b"test 2 contents")
78
79
80 @pytest.fixture(name="httpd_multipart_path_mapping")
81 def fixture_httpd_multipart_path_mapping(tmpdir):
82 d1 = tmpdir.mkdir("d1")
83 d1.join("test1.txt").write("test 1 contents")
84
85 httpd = mozhttpd.MozHttpd(
86 port=0,
87 path_mappings={"/abc/def/ghi": str(d1)},
88 )
89 httpd.start(block=False)
90 yield httpd
91 httpd.stop()
92 d1.remove()
93
94
95 def test_multipart_path_mapping(httpd_multipart_path_mapping):
96 """Test that a path mapping with multiple directories works."""
97 httpd = httpd_multipart_path_mapping
98 try_get(httpd.get_url("/abc/def/ghi/test1.txt"), b"test 1 contents")
99 try_get_expect_404(httpd.get_url("/abc/test1.txt"))
100 try_get_expect_404(httpd.get_url("/abc/def/test1.txt"))
101
102
103 @pytest.fixture(name="httpd_no_docroot")
104 def fixture_httpd_no_docroot(tmpdir):
105 d1 = tmpdir.mkdir("d1")
106 httpd = mozhttpd.MozHttpd(
107 port=0,
108 path_mappings={"/foo": str(d1)},
109 )
110 httpd.start(block=False)
111 yield httpd
112 httpd.stop()
113 d1.remove()
114
115
116 def test_no_docroot(httpd_no_docroot):
117 """Test that path mappings with no docroot work."""
118 try_get_expect_404(httpd_no_docroot.get_url())
119
120
121 if __name__ == "__main__":
122 mozunit.main()