testing/mozbase/mozfile/tests/test_load.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 #!/usr/bin/env python
     3 """
     4 tests for mozfile.load
     5 """
     7 import mozhttpd
     8 import os
     9 import tempfile
    10 import unittest
    11 from mozfile import load
    14 class TestLoad(unittest.TestCase):
    15     """test the load function"""
    17     def test_http(self):
    18         """test with mozhttpd and a http:// URL"""
    20         def example(request):
    21             """example request handler"""
    22             body = 'example'
    23             return (200, {'Content-type': 'text/plain',
    24                           'Content-length': len(body)
    25                           }, body)
    27         host = '127.0.0.1'
    28         httpd = mozhttpd.MozHttpd(host=host,
    29                                   urlhandlers=[{'method': 'GET',
    30                                                 'path': '.*',
    31                                                 'function': example}])
    32         try:
    33             httpd.start(block=False)
    34             content = load(httpd.get_url()).read()
    35             self.assertEqual(content, 'example')
    36         finally:
    37             httpd.stop()
    39     def test_file_path(self):
    40         """test loading from file path"""
    41         try:
    42             # create a temporary file
    43             tmp = tempfile.NamedTemporaryFile(delete=False)
    44             tmp.write('foo bar')
    45             tmp.close()
    47             # read the file
    48             contents = file(tmp.name).read()
    49             self.assertEqual(contents, 'foo bar')
    51             # read the file with load and a file path
    52             self.assertEqual(load(tmp.name).read(), contents)
    54             # read the file with load and a file URL
    55             self.assertEqual(load('file://%s' % tmp.name).read(), contents)
    56         finally:
    57             # remove the tempfile
    58             if os.path.exists(tmp.name):
    59                 os.remove(tmp.name)
    61 if __name__ == '__main__':
    62     unittest.main()

mercurial