OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import StringIO | 5 import StringIO |
6 import gzip | 6 import gzip |
7 import imp | 7 import imp |
8 import os.path | 8 import os.path |
9 import shutil | 9 import shutil |
10 import sys | 10 import sys |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 dir=self.other_dir) | 49 dir=self.other_dir) |
50 | 50 |
51 def tearDown(self): | 51 def tearDown(self): |
52 shutil.rmtree(self.parent_dir) | 52 shutil.rmtree(self.parent_dir) |
53 | 53 |
54 def test_mappings(self): | 54 def test_mappings(self): |
55 """Maps two directories and verifies that the server serves files placed | 55 """Maps two directories and verifies that the server serves files placed |
56 there. | 56 there. |
57 """ | 57 """ |
58 mappings = [ | 58 mappings = [ |
59 ('hello/', self.hello_dir), | 59 ('hello/', [self.hello_dir]), |
60 ('other/', self.other_dir), | 60 ('other/', [self.other_dir]), |
61 ] | 61 ] |
62 server_address = ('http://%s:%u/' % | 62 server_address = ('http://%s:%u/' % |
63 http_server.start_http_server(mappings)) | 63 http_server.start_http_server(mappings)) |
64 | 64 |
65 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) | 65 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) |
66 hello_response = urllib2.urlopen(server_address + 'hello/' + | 66 hello_response = urllib2.urlopen(server_address + 'hello/' + |
67 hello_relpath) | 67 hello_relpath) |
68 self.assertEquals(200, hello_response.getcode()) | 68 self.assertEquals(200, hello_response.getcode()) |
69 | 69 |
70 other_relpath = os.path.relpath(self.other_file.name, self.other_dir) | 70 other_relpath = os.path.relpath(self.other_file.name, self.other_dir) |
71 other_response = urllib2.urlopen(server_address + 'other/' + | 71 other_response = urllib2.urlopen(server_address + 'other/' + |
72 other_relpath) | 72 other_relpath) |
73 self.assertEquals(200, other_response.getcode()) | 73 self.assertEquals(200, other_response.getcode()) |
74 | 74 |
| 75 def test_unmapped_path(self): |
| 76 """Verifies that the server returns 404 when a request for unmapped url |
| 77 prefix is made. |
| 78 """ |
| 79 mappings = [ |
| 80 ('hello/', [self.hello_dir]), |
| 81 ] |
| 82 server_address = ('http://%s:%u/' % |
| 83 http_server.start_http_server(mappings)) |
| 84 |
| 85 error_code = None |
| 86 try: |
| 87 urllib2.urlopen(server_address + 'unmapped/abc') |
| 88 except urllib2.HTTPError as error: |
| 89 error_code = error.code |
| 90 self.assertEquals(404, error_code) |
| 91 |
| 92 def test_multiple_paths(self): |
| 93 """Verfies mapping multiple local paths under the same url prefix.""" |
| 94 mappings = [ |
| 95 ('singularity/', [self.hello_dir, self.other_dir]), |
| 96 ] |
| 97 server_address = ('http://%s:%u/' % |
| 98 http_server.start_http_server(mappings)) |
| 99 |
| 100 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) |
| 101 hello_response = urllib2.urlopen(server_address + 'singularity/' + |
| 102 hello_relpath) |
| 103 self.assertEquals(200, hello_response.getcode()) |
| 104 |
| 105 other_relpath = os.path.relpath(self.other_file.name, self.other_dir) |
| 106 other_response = urllib2.urlopen(server_address + 'singularity/' + |
| 107 other_relpath) |
| 108 self.assertEquals(200, other_response.getcode()) |
| 109 |
| 110 # Verify that a request for a file not present under any of the mapped |
| 111 # directories results in 404. |
| 112 error_code = None |
| 113 try: |
| 114 urllib2.urlopen(server_address + 'singularity/unavailable') |
| 115 except urllib2.HTTPError as error: |
| 116 error_code = error.code |
| 117 self.assertEquals(404, error_code) |
| 118 |
75 def test_gzip(self): | 119 def test_gzip(self): |
76 """Verifies the gzip content encoding of the files being served.""" | 120 """Verifies the gzip content encoding of the files being served.""" |
77 mappings = [ | 121 mappings = [ |
78 ('hello/', self.hello_dir), | 122 ('hello/', [self.hello_dir]), |
79 ] | 123 ] |
80 server_address = ('http://%s:%u/' % | 124 server_address = ('http://%s:%u/' % |
81 http_server.start_http_server(mappings)) | 125 http_server.start_http_server(mappings)) |
82 | 126 |
83 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) | 127 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) |
84 hello_response = urllib2.urlopen(server_address + 'hello/' + | 128 hello_response = urllib2.urlopen(server_address + 'hello/' + |
85 hello_relpath) | 129 hello_relpath) |
86 self.assertEquals(200, hello_response.getcode()) | 130 self.assertEquals(200, hello_response.getcode()) |
87 self.assertTrue('Content-Encoding' in hello_response.info()) | 131 self.assertTrue('Content-Encoding' in hello_response.info()) |
88 self.assertEquals('gzip', hello_response.info().get('Content-Encoding')) | 132 self.assertEquals('gzip', hello_response.info().get('Content-Encoding')) |
89 | 133 |
90 content = gzip.GzipFile( | 134 content = gzip.GzipFile( |
91 fileobj=StringIO.StringIO(hello_response.read())).read() | 135 fileobj=StringIO.StringIO(hello_response.read())).read() |
92 self.assertEquals('hello', content) | 136 self.assertEquals('hello', content) |
93 | 137 |
94 | 138 |
95 if __name__ == "__main__": | 139 if __name__ == "__main__": |
96 unittest.main() | 140 unittest.main() |
OLD | NEW |