michael@0: #!/usr/bin/env python michael@0: michael@0: import mozhttpd michael@0: import mozfile michael@0: import os michael@0: import tempfile michael@0: import unittest michael@0: michael@0: michael@0: class TestBasic(unittest.TestCase): michael@0: """ Test basic Mozhttpd capabilites """ michael@0: michael@0: def test_basic(self): michael@0: """ Test mozhttpd can serve files """ michael@0: michael@0: tempdir = tempfile.mkdtemp() michael@0: michael@0: # sizes is a dict of the form: name -> [size, binary_string, filepath] michael@0: sizes = {'small': [128], 'large': [16384]} michael@0: michael@0: for k in sizes.keys(): michael@0: # Generate random binary string michael@0: sizes[k].append(os.urandom(sizes[k][0])) michael@0: michael@0: # Add path of file with binary string to list michael@0: fpath = os.path.join(tempdir, k) michael@0: sizes[k].append(fpath) michael@0: michael@0: # Write binary string to file michael@0: with open(fpath, 'wb') as f: michael@0: f.write(sizes[k][1]) michael@0: michael@0: server = mozhttpd.MozHttpd(docroot=tempdir) michael@0: server.start() michael@0: server_url = server.get_url() michael@0: michael@0: # Retrieve file and check contents matchup michael@0: for k in sizes.keys(): michael@0: retrieved_content = mozfile.load(server_url + k).read() michael@0: self.assertEqual(retrieved_content, sizes[k][1]) michael@0: michael@0: # Cleanup tempdir and related files michael@0: mozfile.rmtree(tempdir) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()