basic.py (1157B)
1 #!/usr/bin/env python 2 3 import os 4 5 import mozfile 6 import mozhttpd 7 import mozunit 8 import pytest 9 10 11 @pytest.fixture(name="files") 12 def fixture_files(): 13 """Return a list of tuples with name and binary_string.""" 14 return [("small", os.urandom(128)), ("large", os.urandom(16384))] 15 16 17 @pytest.fixture(name="docroot") 18 def fixture_docroot(tmpdir, files): 19 """Yield a str path to docroot.""" 20 docroot = tmpdir.mkdir("docroot") 21 22 for name, binary_string in files: 23 filename = docroot.join(name) 24 filename.write_binary(binary_string) 25 26 yield str(docroot) 27 28 docroot.remove() 29 30 31 @pytest.fixture(name="httpd_url") 32 def fixture_httpd_url(docroot): 33 """Yield the URL to a started MozHttpd server.""" 34 httpd = mozhttpd.MozHttpd(docroot=docroot) 35 httpd.start() 36 yield httpd.get_url() 37 httpd.stop() 38 39 40 def test_basic(httpd_url, files): 41 """Test that mozhttpd can serve files.""" 42 43 # Retrieve file and check contents matchup 44 for name, binary_string in files: 45 retrieved_content = mozfile.load(httpd_url + name).read() 46 assert retrieved_content == binary_string 47 48 49 if __name__ == "__main__": 50 mozunit.main()