testing/mozbase/mozhttpd/tests/basic.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozhttpd/tests/basic.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,46 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +import mozhttpd
     1.7 +import mozfile
     1.8 +import os
     1.9 +import tempfile
    1.10 +import unittest
    1.11 +
    1.12 +
    1.13 +class TestBasic(unittest.TestCase):
    1.14 +    """ Test basic Mozhttpd capabilites """
    1.15 +
    1.16 +    def test_basic(self):
    1.17 +        """ Test mozhttpd can serve files """
    1.18 +
    1.19 +        tempdir = tempfile.mkdtemp()
    1.20 +
    1.21 +        # sizes is a dict of the form: name -> [size, binary_string, filepath]
    1.22 +        sizes = {'small': [128], 'large': [16384]}
    1.23 +
    1.24 +        for k in sizes.keys():
    1.25 +            # Generate random binary string
    1.26 +            sizes[k].append(os.urandom(sizes[k][0]))
    1.27 +
    1.28 +            # Add path of file with binary string to list
    1.29 +            fpath = os.path.join(tempdir, k)
    1.30 +            sizes[k].append(fpath)
    1.31 +
    1.32 +            # Write binary string to file
    1.33 +            with open(fpath, 'wb') as f:
    1.34 +                f.write(sizes[k][1])
    1.35 +
    1.36 +        server = mozhttpd.MozHttpd(docroot=tempdir)
    1.37 +        server.start()
    1.38 +        server_url = server.get_url()
    1.39 +
    1.40 +        # Retrieve file and check contents matchup
    1.41 +        for k in sizes.keys():
    1.42 +            retrieved_content = mozfile.load(server_url + k).read()
    1.43 +            self.assertEqual(retrieved_content, sizes[k][1])
    1.44 +
    1.45 +        # Cleanup tempdir and related files
    1.46 +        mozfile.rmtree(tempdir)
    1.47 +
    1.48 +if __name__ == '__main__':
    1.49 +    unittest.main()

mercurial