michael@0: #!/usr/bin/env python michael@0: michael@0: # Any copyright is dedicated to the Public Domain. michael@0: # http://creativecommons.org/publicdomain/zero/1.0/ michael@0: michael@0: from mozfile import TemporaryDirectory michael@0: import mozhttpd michael@0: import os michael@0: import unittest michael@0: import urllib2 michael@0: michael@0: class PathTest(unittest.TestCase): michael@0: def try_get(self, url, expected_contents): michael@0: f = urllib2.urlopen(url) michael@0: self.assertEqual(f.getcode(), 200) michael@0: self.assertEqual(f.read(), expected_contents) michael@0: michael@0: def try_get_expect_404(self, url): michael@0: with self.assertRaises(urllib2.HTTPError) as cm: michael@0: urllib2.urlopen(url) michael@0: self.assertEqual(404, cm.exception.code) michael@0: michael@0: def test_basic(self): michael@0: """Test that requests to docroot and a path mapping work as expected.""" michael@0: with TemporaryDirectory() as d1, TemporaryDirectory() as d2: michael@0: open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") michael@0: open(os.path.join(d2, "test2.txt"), "w").write("test 2 contents") michael@0: httpd = mozhttpd.MozHttpd(port=0, michael@0: docroot=d1, michael@0: path_mappings={'/files': d2} michael@0: ) michael@0: httpd.start(block=False) michael@0: self.try_get(httpd.get_url("/test1.txt"), "test 1 contents") michael@0: self.try_get(httpd.get_url("/files/test2.txt"), "test 2 contents") michael@0: self.try_get_expect_404(httpd.get_url("/files/test2_nope.txt")) michael@0: httpd.stop() michael@0: michael@0: def test_substring_mappings(self): michael@0: """Test that a path mapping that's a substring of another works.""" michael@0: with TemporaryDirectory() as d1, TemporaryDirectory() as d2: michael@0: open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") michael@0: open(os.path.join(d2, "test2.txt"), "w").write("test 2 contents") michael@0: httpd = mozhttpd.MozHttpd(port=0, michael@0: path_mappings={'/abcxyz': d1, michael@0: '/abc': d2,} michael@0: ) michael@0: httpd.start(block=False) michael@0: self.try_get(httpd.get_url("/abcxyz/test1.txt"), "test 1 contents") michael@0: self.try_get(httpd.get_url("/abc/test2.txt"), "test 2 contents") michael@0: httpd.stop() michael@0: michael@0: def test_multipart_path_mapping(self): michael@0: """Test that a path mapping with multiple directories works.""" michael@0: with TemporaryDirectory() as d1: michael@0: open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") michael@0: httpd = mozhttpd.MozHttpd(port=0, michael@0: path_mappings={'/abc/def/ghi': d1} michael@0: ) michael@0: httpd.start(block=False) michael@0: self.try_get(httpd.get_url("/abc/def/ghi/test1.txt"), "test 1 contents") michael@0: self.try_get_expect_404(httpd.get_url("/abc/test1.txt")) michael@0: self.try_get_expect_404(httpd.get_url("/abc/def/test1.txt")) michael@0: httpd.stop() michael@0: michael@0: def test_no_docroot(self): michael@0: """Test that path mappings with no docroot work.""" michael@0: with TemporaryDirectory() as d1: michael@0: httpd = mozhttpd.MozHttpd(port=0, michael@0: path_mappings={'/foo': d1}) michael@0: httpd.start(block=False) michael@0: self.try_get_expect_404(httpd.get_url()) michael@0: httpd.stop() michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()