testing/mozbase/mozfile/tests/test_load.py

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:c1934e1eb066
1 #!/usr/bin/env python
2
3 """
4 tests for mozfile.load
5 """
6
7 import mozhttpd
8 import os
9 import tempfile
10 import unittest
11 from mozfile import load
12
13
14 class TestLoad(unittest.TestCase):
15 """test the load function"""
16
17 def test_http(self):
18 """test with mozhttpd and a http:// URL"""
19
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)
26
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()
38
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()
46
47 # read the file
48 contents = file(tmp.name).read()
49 self.assertEqual(contents, 'foo bar')
50
51 # read the file with load and a file path
52 self.assertEqual(load(tmp.name).read(), contents)
53
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)
60
61 if __name__ == '__main__':
62 unittest.main()

mercurial