1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozhttpd/tests/paths.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,75 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# Any copyright is dedicated to the Public Domain. 1.7 +# http://creativecommons.org/publicdomain/zero/1.0/ 1.8 + 1.9 +from mozfile import TemporaryDirectory 1.10 +import mozhttpd 1.11 +import os 1.12 +import unittest 1.13 +import urllib2 1.14 + 1.15 +class PathTest(unittest.TestCase): 1.16 + def try_get(self, url, expected_contents): 1.17 + f = urllib2.urlopen(url) 1.18 + self.assertEqual(f.getcode(), 200) 1.19 + self.assertEqual(f.read(), expected_contents) 1.20 + 1.21 + def try_get_expect_404(self, url): 1.22 + with self.assertRaises(urllib2.HTTPError) as cm: 1.23 + urllib2.urlopen(url) 1.24 + self.assertEqual(404, cm.exception.code) 1.25 + 1.26 + def test_basic(self): 1.27 + """Test that requests to docroot and a path mapping work as expected.""" 1.28 + with TemporaryDirectory() as d1, TemporaryDirectory() as d2: 1.29 + open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") 1.30 + open(os.path.join(d2, "test2.txt"), "w").write("test 2 contents") 1.31 + httpd = mozhttpd.MozHttpd(port=0, 1.32 + docroot=d1, 1.33 + path_mappings={'/files': d2} 1.34 + ) 1.35 + httpd.start(block=False) 1.36 + self.try_get(httpd.get_url("/test1.txt"), "test 1 contents") 1.37 + self.try_get(httpd.get_url("/files/test2.txt"), "test 2 contents") 1.38 + self.try_get_expect_404(httpd.get_url("/files/test2_nope.txt")) 1.39 + httpd.stop() 1.40 + 1.41 + def test_substring_mappings(self): 1.42 + """Test that a path mapping that's a substring of another works.""" 1.43 + with TemporaryDirectory() as d1, TemporaryDirectory() as d2: 1.44 + open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") 1.45 + open(os.path.join(d2, "test2.txt"), "w").write("test 2 contents") 1.46 + httpd = mozhttpd.MozHttpd(port=0, 1.47 + path_mappings={'/abcxyz': d1, 1.48 + '/abc': d2,} 1.49 + ) 1.50 + httpd.start(block=False) 1.51 + self.try_get(httpd.get_url("/abcxyz/test1.txt"), "test 1 contents") 1.52 + self.try_get(httpd.get_url("/abc/test2.txt"), "test 2 contents") 1.53 + httpd.stop() 1.54 + 1.55 + def test_multipart_path_mapping(self): 1.56 + """Test that a path mapping with multiple directories works.""" 1.57 + with TemporaryDirectory() as d1: 1.58 + open(os.path.join(d1, "test1.txt"), "w").write("test 1 contents") 1.59 + httpd = mozhttpd.MozHttpd(port=0, 1.60 + path_mappings={'/abc/def/ghi': d1} 1.61 + ) 1.62 + httpd.start(block=False) 1.63 + self.try_get(httpd.get_url("/abc/def/ghi/test1.txt"), "test 1 contents") 1.64 + self.try_get_expect_404(httpd.get_url("/abc/test1.txt")) 1.65 + self.try_get_expect_404(httpd.get_url("/abc/def/test1.txt")) 1.66 + httpd.stop() 1.67 + 1.68 + def test_no_docroot(self): 1.69 + """Test that path mappings with no docroot work.""" 1.70 + with TemporaryDirectory() as d1: 1.71 + httpd = mozhttpd.MozHttpd(port=0, 1.72 + path_mappings={'/foo': d1}) 1.73 + httpd.start(block=False) 1.74 + self.try_get_expect_404(httpd.get_url()) 1.75 + httpd.stop() 1.76 + 1.77 +if __name__ == '__main__': 1.78 + unittest.main()