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 | # Any copyright is dedicated to the Public Domain. |
michael@0 | 4 | # http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 5 | |
michael@0 | 6 | from mozfile import TemporaryDirectory |
michael@0 | 7 | import mozhttpd |
michael@0 | 8 | import os |
michael@0 | 9 | import unittest |
michael@0 | 10 | import urllib2 |
michael@0 | 11 | |
michael@0 | 12 | class PathTest(unittest.TestCase): |
michael@0 | 13 | def try_get(self, url, expected_contents): |
michael@0 | 14 | f = urllib2.urlopen(url) |
michael@0 | 15 | self.assertEqual(f.getcode(), 200) |
michael@0 | 16 | self.assertEqual(f.read(), expected_contents) |
michael@0 | 17 | |
michael@0 | 18 | def try_get_expect_404(self, url): |
michael@0 | 19 | with self.assertRaises(urllib2.HTTPError) as cm: |
michael@0 | 20 | urllib2.urlopen(url) |
michael@0 | 21 | self.assertEqual(404, cm.exception.code) |
michael@0 | 22 | |
michael@0 | 23 | def test_basic(self): |
michael@0 | 24 | """Test that requests to docroot and a path mapping work as expected.""" |
michael@0 | 25 | with TemporaryDirectory() as d1, TemporaryDirectory() as d2: |
michael@0 | 26 | open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") |
michael@0 | 27 | open(os.path.join(d2, "test2.txt"), "w").write("test 2 contents") |
michael@0 | 28 | httpd = mozhttpd.MozHttpd(port=0, |
michael@0 | 29 | docroot=d1, |
michael@0 | 30 | path_mappings={'/files': d2} |
michael@0 | 31 | ) |
michael@0 | 32 | httpd.start(block=False) |
michael@0 | 33 | self.try_get(httpd.get_url("/test1.txt"), "test 1 contents") |
michael@0 | 34 | self.try_get(httpd.get_url("/files/test2.txt"), "test 2 contents") |
michael@0 | 35 | self.try_get_expect_404(httpd.get_url("/files/test2_nope.txt")) |
michael@0 | 36 | httpd.stop() |
michael@0 | 37 | |
michael@0 | 38 | def test_substring_mappings(self): |
michael@0 | 39 | """Test that a path mapping that's a substring of another works.""" |
michael@0 | 40 | with TemporaryDirectory() as d1, TemporaryDirectory() as d2: |
michael@0 | 41 | open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") |
michael@0 | 42 | open(os.path.join(d2, "test2.txt"), "w").write("test 2 contents") |
michael@0 | 43 | httpd = mozhttpd.MozHttpd(port=0, |
michael@0 | 44 | path_mappings={'/abcxyz': d1, |
michael@0 | 45 | '/abc': d2,} |
michael@0 | 46 | ) |
michael@0 | 47 | httpd.start(block=False) |
michael@0 | 48 | self.try_get(httpd.get_url("/abcxyz/test1.txt"), "test 1 contents") |
michael@0 | 49 | self.try_get(httpd.get_url("/abc/test2.txt"), "test 2 contents") |
michael@0 | 50 | httpd.stop() |
michael@0 | 51 | |
michael@0 | 52 | def test_multipart_path_mapping(self): |
michael@0 | 53 | """Test that a path mapping with multiple directories works.""" |
michael@0 | 54 | with TemporaryDirectory() as d1: |
michael@0 | 55 | open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") |
michael@0 | 56 | httpd = mozhttpd.MozHttpd(port=0, |
michael@0 | 57 | path_mappings={'/abc/def/ghi': d1} |
michael@0 | 58 | ) |
michael@0 | 59 | httpd.start(block=False) |
michael@0 | 60 | self.try_get(httpd.get_url("/abc/def/ghi/test1.txt"), "test 1 contents") |
michael@0 | 61 | self.try_get_expect_404(httpd.get_url("/abc/test1.txt")) |
michael@0 | 62 | self.try_get_expect_404(httpd.get_url("/abc/def/test1.txt")) |
michael@0 | 63 | httpd.stop() |
michael@0 | 64 | |
michael@0 | 65 | def test_no_docroot(self): |
michael@0 | 66 | """Test that path mappings with no docroot work.""" |
michael@0 | 67 | with TemporaryDirectory() as d1: |
michael@0 | 68 | httpd = mozhttpd.MozHttpd(port=0, |
michael@0 | 69 | path_mappings={'/foo': d1}) |
michael@0 | 70 | httpd.start(block=False) |
michael@0 | 71 | self.try_get_expect_404(httpd.get_url()) |
michael@0 | 72 | httpd.stop() |
michael@0 | 73 | |
michael@0 | 74 | if __name__ == '__main__': |
michael@0 | 75 | unittest.main() |