##// END OF EJS Templates
tests: add test coverage for parsing WSGI requests...
Gregory Szorc -
r36912:b2a3308d default
parent child Browse files
Show More
@@ -0,0 +1,255 b''
1 from __future__ import absolute_import, print_function
2
3 import unittest
4
5 from mercurial.hgweb import (
6 request as requestmod,
7 )
8
9 DEFAULT_ENV = {
10 r'REQUEST_METHOD': r'GET',
11 r'SERVER_NAME': r'testserver',
12 r'SERVER_PORT': r'80',
13 r'SERVER_PROTOCOL': r'http',
14 r'wsgi.version': (1, 0),
15 r'wsgi.url_scheme': r'http',
16 r'wsgi.input': None,
17 r'wsgi.errors': None,
18 r'wsgi.multithread': False,
19 r'wsgi.multiprocess': True,
20 r'wsgi.run_once': False,
21 }
22
23 def parse(env, bodyfh=None, extra=None):
24 env = dict(env)
25 env.update(extra or {})
26
27 return requestmod.parserequestfromenv(env, bodyfh)
28
29 class ParseRequestTests(unittest.TestCase):
30 def testdefault(self):
31 r = parse(DEFAULT_ENV)
32 self.assertEqual(r.url, b'http://testserver')
33 self.assertEqual(r.baseurl, b'http://testserver')
34 self.assertEqual(r.advertisedurl, b'http://testserver')
35 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
36 self.assertEqual(r.urlscheme, b'http')
37 self.assertEqual(r.method, b'GET')
38 self.assertIsNone(r.remoteuser)
39 self.assertIsNone(r.remotehost)
40 self.assertEqual(r.apppath, b'')
41 self.assertEqual(r.dispatchparts, [])
42 self.assertEqual(r.dispatchpath, b'')
43 self.assertFalse(r.havepathinfo)
44 self.assertIsNone(r.reponame)
45 self.assertEqual(r.querystring, b'')
46 self.assertEqual(len(r.qsparams), 0)
47 self.assertEqual(len(r.headers), 0)
48
49 def testcustomport(self):
50 r = parse(DEFAULT_ENV, extra={
51 r'SERVER_PORT': r'8000',
52 })
53
54 self.assertEqual(r.url, b'http://testserver:8000')
55 self.assertEqual(r.baseurl, b'http://testserver:8000')
56 self.assertEqual(r.advertisedurl, b'http://testserver:8000')
57 self.assertEqual(r.advertisedbaseurl, b'http://testserver:8000')
58
59 r = parse(DEFAULT_ENV, extra={
60 r'SERVER_PORT': r'4000',
61 r'wsgi.url_scheme': r'https',
62 })
63
64 self.assertEqual(r.url, b'https://testserver:4000')
65 self.assertEqual(r.baseurl, b'https://testserver:4000')
66 self.assertEqual(r.advertisedurl, b'https://testserver:4000')
67 self.assertEqual(r.advertisedbaseurl, b'https://testserver:4000')
68
69 def testhttphost(self):
70 r = parse(DEFAULT_ENV, extra={
71 r'HTTP_HOST': r'altserver',
72 })
73
74 self.assertEqual(r.url, b'http://altserver')
75 self.assertEqual(r.baseurl, b'http://altserver')
76 self.assertEqual(r.advertisedurl, b'http://testserver')
77 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
78
79 def testscriptname(self):
80 r = parse(DEFAULT_ENV, extra={
81 r'SCRIPT_NAME': r'',
82 })
83
84 self.assertEqual(r.url, b'http://testserver')
85 self.assertEqual(r.baseurl, b'http://testserver')
86 self.assertEqual(r.advertisedurl, b'http://testserver')
87 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
88 self.assertEqual(r.apppath, b'')
89 self.assertEqual(r.dispatchparts, [])
90 self.assertEqual(r.dispatchpath, b'')
91 self.assertFalse(r.havepathinfo)
92
93 r = parse(DEFAULT_ENV, extra={
94 r'SCRIPT_NAME': r'/script',
95 })
96
97 self.assertEqual(r.url, b'http://testserver/script')
98 self.assertEqual(r.baseurl, b'http://testserver')
99 self.assertEqual(r.advertisedurl, b'http://testserver/script')
100 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
101 self.assertEqual(r.apppath, b'/script')
102 self.assertEqual(r.dispatchparts, [])
103 self.assertEqual(r.dispatchpath, b'')
104 self.assertFalse(r.havepathinfo)
105
106 r = parse(DEFAULT_ENV, extra={
107 r'SCRIPT_NAME': r'/multiple words',
108 })
109
110 self.assertEqual(r.url, b'http://testserver/multiple%20words')
111 self.assertEqual(r.baseurl, b'http://testserver')
112 self.assertEqual(r.advertisedurl, b'http://testserver/multiple%20words')
113 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
114 self.assertEqual(r.apppath, b'/multiple words')
115 self.assertEqual(r.dispatchparts, [])
116 self.assertEqual(r.dispatchpath, b'')
117 self.assertFalse(r.havepathinfo)
118
119 def testpathinfo(self):
120 r = parse(DEFAULT_ENV, extra={
121 r'PATH_INFO': r'',
122 })
123
124 self.assertEqual(r.url, b'http://testserver')
125 self.assertEqual(r.baseurl, b'http://testserver')
126 self.assertEqual(r.advertisedurl, b'http://testserver')
127 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
128 self.assertEqual(r.apppath, b'')
129 self.assertEqual(r.dispatchparts, [])
130 self.assertEqual(r.dispatchpath, b'')
131 self.assertTrue(r.havepathinfo)
132
133 r = parse(DEFAULT_ENV, extra={
134 r'PATH_INFO': r'/pathinfo',
135 })
136
137 self.assertEqual(r.url, b'http://testserver/pathinfo')
138 self.assertEqual(r.baseurl, b'http://testserver')
139 self.assertEqual(r.advertisedurl, b'http://testserver/pathinfo')
140 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
141 self.assertEqual(r.apppath, b'')
142 self.assertEqual(r.dispatchparts, [b'pathinfo'])
143 self.assertEqual(r.dispatchpath, b'pathinfo')
144 self.assertTrue(r.havepathinfo)
145
146 r = parse(DEFAULT_ENV, extra={
147 r'PATH_INFO': r'/one/two/',
148 })
149
150 self.assertEqual(r.url, b'http://testserver/one/two/')
151 self.assertEqual(r.baseurl, b'http://testserver')
152 self.assertEqual(r.advertisedurl, b'http://testserver/one/two/')
153 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
154 self.assertEqual(r.apppath, b'')
155 self.assertEqual(r.dispatchparts, [b'one', b'two'])
156 self.assertEqual(r.dispatchpath, b'one/two')
157 self.assertTrue(r.havepathinfo)
158
159 def testscriptandpathinfo(self):
160 r = parse(DEFAULT_ENV, extra={
161 r'SCRIPT_NAME': r'/script',
162 r'PATH_INFO': r'/pathinfo',
163 })
164
165 self.assertEqual(r.url, b'http://testserver/script/pathinfo')
166 self.assertEqual(r.baseurl, b'http://testserver')
167 self.assertEqual(r.advertisedurl, b'http://testserver/script/pathinfo')
168 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
169 self.assertEqual(r.apppath, b'/script')
170 self.assertEqual(r.dispatchparts, [b'pathinfo'])
171 self.assertEqual(r.dispatchpath, b'pathinfo')
172 self.assertTrue(r.havepathinfo)
173
174 r = parse(DEFAULT_ENV, extra={
175 r'SCRIPT_NAME': r'/script1/script2',
176 r'PATH_INFO': r'/path1/path2',
177 })
178
179 self.assertEqual(r.url,
180 b'http://testserver/script1/script2/path1/path2')
181 self.assertEqual(r.baseurl, b'http://testserver')
182 self.assertEqual(r.advertisedurl,
183 b'http://testserver/script1/script2/path1/path2')
184 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
185 self.assertEqual(r.apppath, b'/script1/script2')
186 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
187 self.assertEqual(r.dispatchpath, b'path1/path2')
188 self.assertTrue(r.havepathinfo)
189
190 r = parse(DEFAULT_ENV, extra={
191 r'HTTP_HOST': r'hostserver',
192 r'SCRIPT_NAME': r'/script',
193 r'PATH_INFO': r'/pathinfo',
194 })
195
196 self.assertEqual(r.url, b'http://hostserver/script/pathinfo')
197 self.assertEqual(r.baseurl, b'http://hostserver')
198 self.assertEqual(r.advertisedurl, b'http://testserver/script/pathinfo')
199 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
200 self.assertEqual(r.apppath, b'/script')
201 self.assertEqual(r.dispatchparts, [b'pathinfo'])
202 self.assertEqual(r.dispatchpath, b'pathinfo')
203 self.assertTrue(r.havepathinfo)
204
205 def testreponame(self):
206 """REPO_NAME path components get stripped from URL."""
207 r = parse(DEFAULT_ENV, extra={
208 r'REPO_NAME': r'repo',
209 r'PATH_INFO': r'/path1/path2'
210 })
211
212 self.assertEqual(r.url, b'http://testserver/path1/path2')
213 self.assertEqual(r.baseurl, b'http://testserver')
214 self.assertEqual(r.advertisedurl, b'http://testserver/path1/path2')
215 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
216 self.assertEqual(r.apppath, b'/repo')
217 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
218 self.assertEqual(r.dispatchpath, b'path1/path2')
219 self.assertTrue(r.havepathinfo)
220 self.assertEqual(r.reponame, b'repo')
221
222 r = parse(DEFAULT_ENV, extra={
223 r'REPO_NAME': r'repo',
224 r'PATH_INFO': r'/repo/path1/path2',
225 })
226
227 self.assertEqual(r.url, b'http://testserver/repo/path1/path2')
228 self.assertEqual(r.baseurl, b'http://testserver')
229 self.assertEqual(r.advertisedurl, b'http://testserver/repo/path1/path2')
230 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
231 self.assertEqual(r.apppath, b'/repo')
232 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
233 self.assertEqual(r.dispatchpath, b'path1/path2')
234 self.assertTrue(r.havepathinfo)
235 self.assertEqual(r.reponame, b'repo')
236
237 r = parse(DEFAULT_ENV, extra={
238 r'REPO_NAME': r'prefix/repo',
239 r'PATH_INFO': r'/prefix/repo/path1/path2',
240 })
241
242 self.assertEqual(r.url, b'http://testserver/prefix/repo/path1/path2')
243 self.assertEqual(r.baseurl, b'http://testserver')
244 self.assertEqual(r.advertisedurl,
245 b'http://testserver/prefix/repo/path1/path2')
246 self.assertEqual(r.advertisedbaseurl, b'http://testserver')
247 self.assertEqual(r.apppath, b'/prefix/repo')
248 self.assertEqual(r.dispatchparts, [b'path1', b'path2'])
249 self.assertEqual(r.dispatchpath, b'path1/path2')
250 self.assertTrue(r.havepathinfo)
251 self.assertEqual(r.reponame, b'prefix/repo')
252
253 if __name__ == '__main__':
254 import silenttestrunner
255 silenttestrunner.main(__name__)
@@ -222,7 +222,7 b' def parserequestfromenv(env, bodyfh):'
222 # root. We also exclude its path components from PATH_INFO when resolving
222 # root. We also exclude its path components from PATH_INFO when resolving
223 # the dispatch path.
223 # the dispatch path.
224
224
225 apppath = env['SCRIPT_NAME']
225 apppath = env.get('SCRIPT_NAME', '')
226
226
227 if env.get('REPO_NAME'):
227 if env.get('REPO_NAME'):
228 if not apppath.endswith('/'):
228 if not apppath.endswith('/'):
General Comments 0
You need to be logged in to leave comments. Login now