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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 def test_mappings(self): | 61 def test_mappings(self): |
62 """Maps two directories and verifies that the server serves files placed | 62 """Maps two directories and verifies that the server serves files placed |
63 there. | 63 there. |
64 """ | 64 """ |
65 mappings = [ | 65 mappings = [ |
66 ('hello/', [self.hello_dir]), | 66 ('hello/', [self.hello_dir]), |
67 ('other/', [self.other_dir]), | 67 ('other/', [self.other_dir]), |
68 ] | 68 ] |
69 server_address = ('http://%s:%u/' % | 69 server_address = ('http://%s:%u/' % |
70 http_server.start_http_server(mappings)) | 70 http_server.start_http_server(mappings, 0)) |
71 | 71 |
72 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) | 72 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) |
73 hello_response = urllib2.urlopen(server_address + 'hello/' + | 73 hello_response = urllib2.urlopen(server_address + 'hello/' + |
74 hello_relpath) | 74 hello_relpath) |
75 self.assertEquals(200, hello_response.getcode()) | 75 self.assertEquals(200, hello_response.getcode()) |
76 | 76 |
77 other_relpath = os.path.relpath(self.other_file.name, self.other_dir) | 77 other_relpath = os.path.relpath(self.other_file.name, self.other_dir) |
78 other_response = urllib2.urlopen(server_address + 'other/' + | 78 other_response = urllib2.urlopen(server_address + 'other/' + |
79 other_relpath) | 79 other_relpath) |
80 self.assertEquals(200, other_response.getcode()) | 80 self.assertEquals(200, other_response.getcode()) |
81 | 81 |
82 def test_unmapped_path(self): | 82 def test_unmapped_path(self): |
83 """Verifies that the server returns 404 when a request for unmapped url | 83 """Verifies that the server returns 404 when a request for unmapped url |
84 prefix is made. | 84 prefix is made. |
85 """ | 85 """ |
86 mappings = [ | 86 mappings = [ |
87 ('hello/', [self.hello_dir]), | 87 ('hello/', [self.hello_dir]), |
88 ] | 88 ] |
89 server_address = ('http://%s:%u/' % | 89 server_address = ('http://%s:%u/' % |
90 http_server.start_http_server(mappings)) | 90 http_server.start_http_server(mappings, 0)) |
91 | 91 |
92 error_code = None | 92 error_code = None |
93 try: | 93 try: |
94 urllib2.urlopen(server_address + 'unmapped/abc') | 94 urllib2.urlopen(server_address + 'unmapped/abc') |
95 except urllib2.HTTPError as error: | 95 except urllib2.HTTPError as error: |
96 error_code = error.code | 96 error_code = error.code |
97 self.assertEquals(404, error_code) | 97 self.assertEquals(404, error_code) |
98 | 98 |
99 def test_multiple_paths(self): | 99 def test_multiple_paths(self): |
100 """Verfies mapping multiple local paths under the same url prefix.""" | 100 """Verfies mapping multiple local paths under the same url prefix.""" |
101 mappings = [ | 101 mappings = [ |
102 ('singularity/', [self.hello_dir, self.other_dir]), | 102 ('singularity/', [self.hello_dir, self.other_dir]), |
103 ] | 103 ] |
104 server_address = ('http://%s:%u/' % | 104 server_address = ('http://%s:%u/' % |
105 http_server.start_http_server(mappings)) | 105 http_server.start_http_server(mappings, 0)) |
106 | 106 |
107 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) | 107 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) |
108 hello_response = urllib2.urlopen(server_address + 'singularity/' + | 108 hello_response = urllib2.urlopen(server_address + 'singularity/' + |
109 hello_relpath) | 109 hello_relpath) |
110 self.assertEquals(200, hello_response.getcode()) | 110 self.assertEquals(200, hello_response.getcode()) |
111 | 111 |
112 other_relpath = os.path.relpath(self.other_file.name, self.other_dir) | 112 other_relpath = os.path.relpath(self.other_file.name, self.other_dir) |
113 other_response = urllib2.urlopen(server_address + 'singularity/' + | 113 other_response = urllib2.urlopen(server_address + 'singularity/' + |
114 other_relpath) | 114 other_relpath) |
115 self.assertEquals(200, other_response.getcode()) | 115 self.assertEquals(200, other_response.getcode()) |
116 | 116 |
117 # Verify that a request for a file not present under any of the mapped | 117 # Verify that a request for a file not present under any of the mapped |
118 # directories results in 404. | 118 # directories results in 404. |
119 error_code = None | 119 error_code = None |
120 try: | 120 try: |
121 urllib2.urlopen(server_address + 'singularity/unavailable') | 121 urllib2.urlopen(server_address + 'singularity/unavailable') |
122 except urllib2.HTTPError as error: | 122 except urllib2.HTTPError as error: |
123 error_code = error.code | 123 error_code = error.code |
124 self.assertEquals(404, error_code) | 124 self.assertEquals(404, error_code) |
125 | 125 |
126 def test_gzip(self): | 126 def test_gzip(self): |
127 """Verifies the gzip content encoding of the files being served.""" | 127 """Verifies the gzip content encoding of the files being served.""" |
128 mappings = [ | 128 mappings = [ |
129 ('hello/', [self.hello_dir]), | 129 ('hello/', [self.hello_dir]), |
130 ] | 130 ] |
131 server_address = ('http://%s:%u/' % | 131 server_address = ('http://%s:%u/' % |
132 http_server.start_http_server(mappings)) | 132 http_server.start_http_server(mappings, 0)) |
133 | 133 |
134 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) | 134 hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) |
135 hello_response = urllib2.urlopen(server_address + 'hello/' + | 135 hello_response = urllib2.urlopen(server_address + 'hello/' + |
136 hello_relpath) | 136 hello_relpath) |
137 self.assertEquals(200, hello_response.getcode()) | 137 self.assertEquals(200, hello_response.getcode()) |
138 self.assertTrue('Content-Encoding' in hello_response.info()) | 138 self.assertTrue('Content-Encoding' in hello_response.info()) |
139 self.assertEquals('gzip', hello_response.info().get('Content-Encoding')) | 139 self.assertEquals('gzip', hello_response.info().get('Content-Encoding')) |
140 | 140 |
141 content = gzip.GzipFile( | 141 content = gzip.GzipFile( |
142 fileobj=StringIO.StringIO(hello_response.read())).read() | 142 fileobj=StringIO.StringIO(hello_response.read())).read() |
143 self.assertEquals('hello', content) | 143 self.assertEquals('hello', content) |
144 | 144 |
145 def test_dart_mime_type(self): | 145 def test_dart_mime_type(self): |
146 """Verifies that files of '.dart' extension are served with MIME type | 146 """Verifies that files of '.dart' extension are served with MIME type |
147 'application/dart'. | 147 'application/dart'. |
148 """ | 148 """ |
149 mappings = [ | 149 mappings = [ |
150 ('', [self.apps_dir]), | 150 ('', [self.apps_dir]), |
151 ] | 151 ] |
152 server_address = ('http://%s:%u/' % | 152 server_address = ('http://%s:%u/' % |
153 http_server.start_http_server(mappings)) | 153 http_server.start_http_server(mappings, 0)) |
154 | 154 |
155 app_relpath = os.path.relpath(self.dart_app_path, self.apps_dir) | 155 app_relpath = os.path.relpath(self.dart_app_path, self.apps_dir) |
156 hello_response = urllib2.urlopen(server_address + app_relpath) | 156 hello_response = urllib2.urlopen(server_address + app_relpath) |
157 self.assertEquals(200, hello_response.getcode()) | 157 self.assertEquals(200, hello_response.getcode()) |
158 self.assertTrue('Content-Type' in hello_response.info()) | 158 self.assertTrue('Content-Type' in hello_response.info()) |
159 self.assertEquals('application/dart', | 159 self.assertEquals('application/dart', |
160 hello_response.info().get('Content-Type')) | 160 hello_response.info().get('Content-Type')) |
161 | 161 |
162 | 162 |
163 if __name__ == "__main__": | 163 if __name__ == "__main__": |
164 unittest.main() | 164 unittest.main() |
OLD | NEW |