Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | """ |
michael@0 | 4 | tests for mozfile.load |
michael@0 | 5 | """ |
michael@0 | 6 | |
michael@0 | 7 | import mozhttpd |
michael@0 | 8 | import os |
michael@0 | 9 | import tempfile |
michael@0 | 10 | import unittest |
michael@0 | 11 | from mozfile import load |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | class TestLoad(unittest.TestCase): |
michael@0 | 15 | """test the load function""" |
michael@0 | 16 | |
michael@0 | 17 | def test_http(self): |
michael@0 | 18 | """test with mozhttpd and a http:// URL""" |
michael@0 | 19 | |
michael@0 | 20 | def example(request): |
michael@0 | 21 | """example request handler""" |
michael@0 | 22 | body = 'example' |
michael@0 | 23 | return (200, {'Content-type': 'text/plain', |
michael@0 | 24 | 'Content-length': len(body) |
michael@0 | 25 | }, body) |
michael@0 | 26 | |
michael@0 | 27 | host = '127.0.0.1' |
michael@0 | 28 | httpd = mozhttpd.MozHttpd(host=host, |
michael@0 | 29 | urlhandlers=[{'method': 'GET', |
michael@0 | 30 | 'path': '.*', |
michael@0 | 31 | 'function': example}]) |
michael@0 | 32 | try: |
michael@0 | 33 | httpd.start(block=False) |
michael@0 | 34 | content = load(httpd.get_url()).read() |
michael@0 | 35 | self.assertEqual(content, 'example') |
michael@0 | 36 | finally: |
michael@0 | 37 | httpd.stop() |
michael@0 | 38 | |
michael@0 | 39 | def test_file_path(self): |
michael@0 | 40 | """test loading from file path""" |
michael@0 | 41 | try: |
michael@0 | 42 | # create a temporary file |
michael@0 | 43 | tmp = tempfile.NamedTemporaryFile(delete=False) |
michael@0 | 44 | tmp.write('foo bar') |
michael@0 | 45 | tmp.close() |
michael@0 | 46 | |
michael@0 | 47 | # read the file |
michael@0 | 48 | contents = file(tmp.name).read() |
michael@0 | 49 | self.assertEqual(contents, 'foo bar') |
michael@0 | 50 | |
michael@0 | 51 | # read the file with load and a file path |
michael@0 | 52 | self.assertEqual(load(tmp.name).read(), contents) |
michael@0 | 53 | |
michael@0 | 54 | # read the file with load and a file URL |
michael@0 | 55 | self.assertEqual(load('file://%s' % tmp.name).read(), contents) |
michael@0 | 56 | finally: |
michael@0 | 57 | # remove the tempfile |
michael@0 | 58 | if os.path.exists(tmp.name): |
michael@0 | 59 | os.remove(tmp.name) |
michael@0 | 60 | |
michael@0 | 61 | if __name__ == '__main__': |
michael@0 | 62 | unittest.main() |