Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: mojo/devtools/common/devtoolslib/http_server_unittest.py

Issue 1269863004: Support mapping multiple local paths under the same url prefix. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Fix typos in tests. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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()
OLDNEW
« no previous file with comments | « mojo/devtools/common/devtoolslib/http_server.py ('k') | mojo/devtools/common/devtoolslib/linux_shell.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698