1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozfile/tests/test_load.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,62 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +""" 1.7 +tests for mozfile.load 1.8 +""" 1.9 + 1.10 +import mozhttpd 1.11 +import os 1.12 +import tempfile 1.13 +import unittest 1.14 +from mozfile import load 1.15 + 1.16 + 1.17 +class TestLoad(unittest.TestCase): 1.18 + """test the load function""" 1.19 + 1.20 + def test_http(self): 1.21 + """test with mozhttpd and a http:// URL""" 1.22 + 1.23 + def example(request): 1.24 + """example request handler""" 1.25 + body = 'example' 1.26 + return (200, {'Content-type': 'text/plain', 1.27 + 'Content-length': len(body) 1.28 + }, body) 1.29 + 1.30 + host = '127.0.0.1' 1.31 + httpd = mozhttpd.MozHttpd(host=host, 1.32 + urlhandlers=[{'method': 'GET', 1.33 + 'path': '.*', 1.34 + 'function': example}]) 1.35 + try: 1.36 + httpd.start(block=False) 1.37 + content = load(httpd.get_url()).read() 1.38 + self.assertEqual(content, 'example') 1.39 + finally: 1.40 + httpd.stop() 1.41 + 1.42 + def test_file_path(self): 1.43 + """test loading from file path""" 1.44 + try: 1.45 + # create a temporary file 1.46 + tmp = tempfile.NamedTemporaryFile(delete=False) 1.47 + tmp.write('foo bar') 1.48 + tmp.close() 1.49 + 1.50 + # read the file 1.51 + contents = file(tmp.name).read() 1.52 + self.assertEqual(contents, 'foo bar') 1.53 + 1.54 + # read the file with load and a file path 1.55 + self.assertEqual(load(tmp.name).read(), contents) 1.56 + 1.57 + # read the file with load and a file URL 1.58 + self.assertEqual(load('file://%s' % tmp.name).read(), contents) 1.59 + finally: 1.60 + # remove the tempfile 1.61 + if os.path.exists(tmp.name): 1.62 + os.remove(tmp.name) 1.63 + 1.64 +if __name__ == '__main__': 1.65 + unittest.main()