Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | import mozhttpd |
michael@0 | 4 | import mozfile |
michael@0 | 5 | import os |
michael@0 | 6 | import tempfile |
michael@0 | 7 | import unittest |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | class TestBasic(unittest.TestCase): |
michael@0 | 11 | """ Test basic Mozhttpd capabilites """ |
michael@0 | 12 | |
michael@0 | 13 | def test_basic(self): |
michael@0 | 14 | """ Test mozhttpd can serve files """ |
michael@0 | 15 | |
michael@0 | 16 | tempdir = tempfile.mkdtemp() |
michael@0 | 17 | |
michael@0 | 18 | # sizes is a dict of the form: name -> [size, binary_string, filepath] |
michael@0 | 19 | sizes = {'small': [128], 'large': [16384]} |
michael@0 | 20 | |
michael@0 | 21 | for k in sizes.keys(): |
michael@0 | 22 | # Generate random binary string |
michael@0 | 23 | sizes[k].append(os.urandom(sizes[k][0])) |
michael@0 | 24 | |
michael@0 | 25 | # Add path of file with binary string to list |
michael@0 | 26 | fpath = os.path.join(tempdir, k) |
michael@0 | 27 | sizes[k].append(fpath) |
michael@0 | 28 | |
michael@0 | 29 | # Write binary string to file |
michael@0 | 30 | with open(fpath, 'wb') as f: |
michael@0 | 31 | f.write(sizes[k][1]) |
michael@0 | 32 | |
michael@0 | 33 | server = mozhttpd.MozHttpd(docroot=tempdir) |
michael@0 | 34 | server.start() |
michael@0 | 35 | server_url = server.get_url() |
michael@0 | 36 | |
michael@0 | 37 | # Retrieve file and check contents matchup |
michael@0 | 38 | for k in sizes.keys(): |
michael@0 | 39 | retrieved_content = mozfile.load(server_url + k).read() |
michael@0 | 40 | self.assertEqual(retrieved_content, sizes[k][1]) |
michael@0 | 41 | |
michael@0 | 42 | # Cleanup tempdir and related files |
michael@0 | 43 | mozfile.rmtree(tempdir) |
michael@0 | 44 | |
michael@0 | 45 | if __name__ == '__main__': |
michael@0 | 46 | unittest.main() |