michael@0: #!/usr/bin/env python michael@0: michael@0: """ michael@0: tests for mozfile.load michael@0: """ michael@0: michael@0: import mozhttpd michael@0: import os michael@0: import tempfile michael@0: import unittest michael@0: from mozfile import load michael@0: michael@0: michael@0: class TestLoad(unittest.TestCase): michael@0: """test the load function""" michael@0: michael@0: def test_http(self): michael@0: """test with mozhttpd and a http:// URL""" michael@0: michael@0: def example(request): michael@0: """example request handler""" michael@0: body = 'example' michael@0: return (200, {'Content-type': 'text/plain', michael@0: 'Content-length': len(body) michael@0: }, body) michael@0: michael@0: host = '127.0.0.1' michael@0: httpd = mozhttpd.MozHttpd(host=host, michael@0: urlhandlers=[{'method': 'GET', michael@0: 'path': '.*', michael@0: 'function': example}]) michael@0: try: michael@0: httpd.start(block=False) michael@0: content = load(httpd.get_url()).read() michael@0: self.assertEqual(content, 'example') michael@0: finally: michael@0: httpd.stop() michael@0: michael@0: def test_file_path(self): michael@0: """test loading from file path""" michael@0: try: michael@0: # create a temporary file michael@0: tmp = tempfile.NamedTemporaryFile(delete=False) michael@0: tmp.write('foo bar') michael@0: tmp.close() michael@0: michael@0: # read the file michael@0: contents = file(tmp.name).read() michael@0: self.assertEqual(contents, 'foo bar') michael@0: michael@0: # read the file with load and a file path michael@0: self.assertEqual(load(tmp.name).read(), contents) michael@0: michael@0: # read the file with load and a file URL michael@0: self.assertEqual(load('file://%s' % tmp.name).read(), contents) michael@0: finally: michael@0: # remove the tempfile michael@0: if os.path.exists(tmp.name): michael@0: os.remove(tmp.name) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()