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