Debian Codegraph @

testing/mozbase/mozhttpd/tests/filelisting.py

raw
1 #!/usr/bin/env python
2
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 # You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 import os
8 import re
9 from urllib.request import urlopen
10
11 import mozhttpd
12 import mozunit
13 import pytest
14 from six import ensure_str
15
16
17 @pytest.fixture(name="docroot")
18 def fixture_docroot():
19 """Returns a docroot path."""
20 return os.path.dirname(os.path.abspath(__file__))
21
22
23 @pytest.fixture(name="httpd")
24 def fixture_httpd(docroot):
25 """Yields a started MozHttpd server."""
26 httpd = mozhttpd.MozHttpd(port=0, docroot=docroot)
27 httpd.start(block=False)
28 yield httpd
29 httpd.stop()
30
31
32 @pytest.mark.parametrize(
33 "path",
34 [
35 pytest.param("", id="no_params"),
36 pytest.param("?foo=bar&fleem=&foo=fleem", id="with_params"),
37 ],
38 )
39 def test_filelist(httpd, docroot, path):
40 f = urlopen(
41 "http://{host}:{port}/{path}".format(
42 host="127.0.0.1", port=httpd.httpd.server_port, path=path
43 )
44 )
45
46 filelist = os.listdir(docroot)
47
48 pattern = r"""\<[a-zA-Z0-9\-\_\.\="'\/\\%\!\@\#\$\^\&\*\(\) :;]*\>"""
49
50 for line in f.readlines():
51 subbed_lined = re.sub(pattern, "", ensure_str(line).strip("\n"))
52 webline = subbed_lined.strip("/").strip().strip("@")
53
54 if (
55 webline
56 and not webline.startswith("Directory listing for")
57 and not webline.startswith("<!DOCTYPE")
58 ):
59 msg = f"File {webline} in dir listing corresponds to a file"
60 assert webline in filelist, msg
61 filelist.remove(webline)
62
63 msg = f"Should have no items in filelist ({filelist}) unaccounted for"
64 assert len(filelist) == 0, msg
65
66
67 if __name__ == "__main__":
68 mozunit.main()