1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import collections
import json
import os
import mozhttpd
import mozunit
import pytest
from six import ensure_binary, ensure_str
from six.moves.urllib.error import HTTPError
from six.moves.urllib.request import (
HTTPHandler,
ProxyHandler,
Request,
build_opener,
install_opener,
urlopen,
)
def httpd_url(httpd, path, querystr=None):
"""Return the URL to a started MozHttpd server for the given info."""
url = "http://127.0.0.1:{port}{path}".format(
port=httpd.httpd.server_port,
path=path,
)
if querystr is not None:
url = "{url}?{querystr}".format(
url=url,
querystr=querystr,
)
return url
@pytest.fixture(name="num_requests")
def fixture_num_requests():
"""Return a defaultdict to count requests to HTTP handlers."""
return collections.defaultdict(int)
@pytest.fixture(name="try_get")
def fixture_try_get(num_requests):
"""Return a function to try GET requests to the server."""
def try_get(httpd, querystr):
"""Try GET requests to the server."""
num_requests["get_handler"] = 0
f = urlopen(httpd_url(httpd, "/api/resource/1", querystr))
assert f.getcode() == 200
assert json.loads(f.read()) == {"called": 1, "id": "1", "query": querystr}
assert num_requests["get_handler"] == 1
return try_get
@pytest.fixture(name="try_post")
def fixture_try_post(num_requests):
"""Return a function to try POST calls to the server."""
def try_post(httpd, querystr):
"""Try POST calls to the server."""
num_requests["post_handler"] = 0
postdata = {"hamburgers": "1234"}
f = urlopen(
httpd_url(httpd, "/api/resource/", querystr),
data=ensure_binary(json.dumps(postdata)),
)
assert f.getcode() == 201
assert json.loads(f.read()) == {
"called": 1,
"data": postdata,
"query": querystr,
}
assert num_requests["post_handler"] == 1
return try_post
@pytest.fixture(name="try_del")
def fixture_try_del(num_requests):
"""Return a function to try DEL calls to the server."""
def try_del(httpd, querystr):
"""Try DEL calls to the server."""
num_requests["del_handler"] = 0
opener = build_opener(HTTPHandler)
request = Request(httpd_url(httpd, "/api/resource/1", querystr))
request.get_method = lambda: "DEL"
f = opener.open(request)
assert f.getcode() == 200
assert json.loads(f.read()) == {"called": 1, "id": "1", "query": querystr}
assert num_requests["del_handler"] == 1
return try_del
@pytest.fixture(name="httpd_no_urlhandlers")
def fixture_httpd_no_urlhandlers():
"""Yields a started MozHttpd server with no URL handlers."""
httpd = mozhttpd.MozHttpd(port=0)
httpd.start(block=False)
yield httpd
httpd.stop()
@pytest.fixture(name="httpd_with_docroot")
def fixture_httpd_with_docroot(num_requests):
"""Yields a started MozHttpd server with docroot set."""
@mozhttpd.handlers.json_response
def get_handler(request, objid):
"""Handler for HTTP GET requests."""
num_requests["get_handler"] += 1
return (
200,
{
"called": num_requests["get_handler"],
"id": objid,
"query": request.query,
},
)
httpd = mozhttpd.MozHttpd(
port=0,
docroot=os.path.dirname(os.path.abspath(__file__)),
urlhandlers=[
{
"method": "GET",
"path": "/api/resource/([^/]+)/?",
"function": get_handler,
}
],
)
httpd.start(block=False)
yield httpd
httpd.stop()
@pytest.fixture(name="httpd")
def fixture_httpd(num_requests):
"""Yield a started MozHttpd server."""
@mozhttpd.handlers.json_response
def get_handler(request, objid):
"""Handler for HTTP GET requests."""
num_requests["get_handler"] += 1
return (
200,
{
"called": num_requests["get_handler"],
"id": objid,
"query": request.query,
},
)
@mozhttpd.handlers.json_response
def post_handler(request):
"""Handler for HTTP POST requests."""
num_requests["post_handler"] += 1
return (
201,
{
"called": num_requests["post_handler"],
"data": json.loads(request.body),
"query": request.query,
},
)
@mozhttpd.handlers.json_response
def del_handler(request, objid):
"""Handler for HTTP DEL requests."""
num_requests["del_handler"] += 1
return (
200,
{
"called": num_requests["del_handler"],
"id": objid,
"query": request.query,
},
)
httpd = mozhttpd.MozHttpd(
port=0,
urlhandlers=[
{
"method": "GET",
"path": "/api/resource/([^/]+)/?",
"function": get_handler,
},
{
"method": "POST",
"path": "/api/resource/?",
"function": post_handler,
},
{
"method": "DEL",
"path": "/api/resource/([^/]+)/?",
"function": del_handler,
},
],
)
httpd.start(block=False)
yield httpd
httpd.stop()
def test_api(httpd, try_get, try_post, try_del):
# GET requests
try_get(httpd, "")
try_get(httpd, "?foo=bar")
# POST requests
try_post(httpd, "")
try_post(httpd, "?foo=bar")
# DEL requests
try_del(httpd, "")
try_del(httpd, "?foo=bar")
# GET: By default we don't serve any files if we just define an API
with pytest.raises(HTTPError) as exc_info:
urlopen(httpd_url(httpd, "/"))
assert exc_info.value.code == 404
def test_nonexistent_resources(httpd_no_urlhandlers):
# GET: Return 404 for non-existent endpoint
with pytest.raises(HTTPError) as excinfo:
urlopen(httpd_url(httpd_no_urlhandlers, "/api/resource/"))
assert excinfo.value.code == 404
# POST: POST should also return 404
with pytest.raises(HTTPError) as excinfo:
urlopen(
httpd_url(httpd_no_urlhandlers, "/api/resource/"),
data=ensure_binary(json.dumps({})),
)
assert excinfo.value.code == 404
# DEL: DEL should also return 404
opener = build_opener(HTTPHandler)
request = Request(httpd_url(httpd_no_urlhandlers, "/api/resource/"))
request.get_method = lambda: "DEL"
with pytest.raises(HTTPError) as excinfo:
opener.open(request)
assert excinfo.value.code == 404
def test_api_with_docroot(httpd_with_docroot, try_get):
f = urlopen(httpd_url(httpd_with_docroot, "/"))
assert f.getcode() == 200
assert "Directory listing for" in ensure_str(f.read())
# Make sure API methods still work
try_get(httpd_with_docroot, "")
try_get(httpd_with_docroot, "?foo=bar")
def index_contents(host):
"""Return the expected index contents for the given host."""
return "{host} index".format(host=host)
@pytest.fixture(name="hosts")
def fixture_hosts():
"""Returns a tuple of hosts."""
return ("mozilla.com", "mozilla.org")
@pytest.fixture(name="docroot")
def fixture_docroot(tmpdir):
"""Returns a path object to a temporary docroot directory."""
docroot = tmpdir.mkdir("docroot")
index_file = docroot.join("index.html")
index_file.write(index_contents("*"))
yield docroot
docroot.remove()
@pytest.fixture(name="httpd_with_proxy_handler")
def fixture_httpd_with_proxy_handler(docroot):
"""Yields a started MozHttpd server for the proxy test."""
httpd = mozhttpd.MozHttpd(port=0, docroot=str(docroot))
httpd.start(block=False)
port = httpd.httpd.server_port
proxy_support = ProxyHandler(
{
"http": "http://127.0.0.1:{port:d}".format(port=port),
}
)
install_opener(build_opener(proxy_support))
yield httpd
httpd.stop()
# Reset proxy opener in case it changed
install_opener(None)
def test_proxy(httpd_with_proxy_handler, hosts):
for host in hosts:
f = urlopen("http://{host}/".format(host=host))
assert f.getcode() == 200
assert f.read() == ensure_binary(index_contents("*"))
@pytest.fixture(name="httpd_with_proxy_host_dirs")
def fixture_httpd_with_proxy_host_dirs(docroot, hosts):
for host in hosts:
index_file = docroot.mkdir(host).join("index.html")
index_file.write(index_contents(host))
httpd = mozhttpd.MozHttpd(port=0, docroot=str(docroot), proxy_host_dirs=True)
httpd.start(block=False)
port = httpd.httpd.server_port
proxy_support = ProxyHandler(
{"http": "http://127.0.0.1:{port:d}".format(port=port)}
)
install_opener(build_opener(proxy_support))
yield httpd
httpd.stop()
# Reset proxy opener in case it changed
install_opener(None)
def test_proxy_separate_directories(httpd_with_proxy_host_dirs, hosts):
for host in hosts:
f = urlopen("http://{host}/".format(host=host))
assert f.getcode() == 200
assert f.read() == ensure_binary(index_contents(host))
unproxied_host = "notmozilla.org"
with pytest.raises(HTTPError) as excinfo:
urlopen("http://{host}/".format(host=unproxied_host))
assert excinfo.value.code == 404
if __name__ == "__main__":
mozunit.main()
|