-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd_test.py
516 lines (408 loc) · 15.7 KB
/
httpd_test.py
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/python
"""
Test under 'test_root'. Some test cases could create 'tests/XXXX' directory.
"""
import fs
import httpd
import json
import os
import params
import random
import SimpleHTTPServer, BaseHTTPServer, httplib
import string
import subprocess
import tempfile
import time
import threading
import unittest
import urllib
DEBUG = 4
"""
Easy hack to temporarily overwrite the root path for testing.
"""
params.HTTP_ROOT = "test_root"
class StoppableHandler(httpd.HttpHandler):
def do_QUIT(self):
"""
Kudos http://code.activestate.com/recipes/336012-stoppable-http-server/
"""
self.server.stop = True
self.send_response(200)
self.end_headers()
self.wfile.write("Get QUIT from client!")
def log_message(self, format, *args):
"""
Kudos http://stackoverflow.com/questions/10651052/how-to-quiet-simplehttpserver
"""
if (DEBUG >= 5):
super(StoppableHandler, self).log_message(format, *args)
pass
class StoppableHttpHandler(StoppableHandler):
pass
class StoppableHttpsHandler(httpd.SecureHTTPRequestHandler, StoppableHandler):
pass
class StoppableServer():
"""
Kudos http://code.activestate.com/recipes/336012-stoppable-http-server/
"""
def serve_forever(self):
self.stop = False
self.started = True
while not self.stop:
self.handle_request()
self.socket.close()
class StoppableHttpServer(StoppableServer, httpd.ThreadingHTTP):
pass
class StoppableHttpsServer(StoppableServer, httpd.ThreadingHTTPS):
pass
class Catcher():
"""
Used by the verifier of curl() to catch interested data.
To dump all headers:
for h in catcher.headers.keys():
print "%s: %s" % (h, catcher.headers[h])
"""
def parse_header(self, out):
"""
Given the curl output, parse to self.headers dictionary.
"""
self.headers = {}
for h in out.split("\r\n\r\n", 1)[0].split("\r\n"):
x = h.split(":")
self.headers[x[0]] = ":".join(x[1:]).lstrip()
return True
class BaseCases():
"""
Each test creates a standalone HTTP server and creates a standalone directory.
The /tests/XXXXXXXX/ directory is deleted after the test.
For class inheriting this, please implement:
1. curl_protocol for curl. "http" or "https".
2. curl_args = [].
3. start_daemon().
"""
def start_daemon(self):
assert False == "Please implement this function."
def setUp(self):
random.seed()
self.server_port = random.randint(1024, 65535)
self.server_url = "%s://localhost:%s" % (self.curl_protocol,
str(self.server_port))
chars = string.ascii_uppercase + string.digits
self.server_path = ''.join(random.choice(chars) for _ in range(8))
self.server_root = "%s/tests/%s/" % (self.server_url, self.server_path)
# Must init before new thread starts
self.server = None
# start thread
t = threading.Thread(target = self.start_daemon, args=())
t.daemon = True # Program exits and leaves threads.
t.start()
# wait for server start to accept connection
while self.server == None:
pass
while not hasattr(self.server, 'started'):
pass
while self.server.started == False:
pass
self.curl("PUT", self.server_root, expect_status = 201)
"""
Arguments:
expect_output: a string. Assert it matches the output content (exclude headers)
verifier: a callback function. Assert verifier(out) returns True.
"""
def curl(self, method, url, params = {}, accept = "application/json",
userpass = None,
expect_status = 200, expect_output = None, verifier = None,
extra_headers = {}):
# TODO: use curl.py
# curl -i -H "Accept: application/json" -X PUT -d "phone=1-800-999-9999"
# http://localhost:8888/test
DEVNULL = open(os.devnull, 'w')
p = ["%s=%s" % (urllib.quote(k), urllib.quote(v))
for (k, v) in params.iteritems()]
p = "&".join(p)
a = "Accept: %s" % accept # TODO: unused?
up = ["--digest", "--user", userpass] if userpass else []
headers = []
for (h, v) in extra_headers:
headers.append("-H")
headers.append("%s: %s" % (h, v))
cmd = ['curl', '-i', '-X', method, "-d", p, url] + up + \
self.curl_args + headers
if DEBUG >= 5: print(cmd)
pipe = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = DEVNULL)
(out, _) = pipe.communicate()
if DEBUG >= 5: print "OUT: " + out
# parse headers
out_lines = out.split("\r\n")
if DEBUG >= 5: print "out_lines: ", out_lines
status = int(out_lines[0].split(" ")[1]) # HTTP/1.0 200 OK
# If 401, curl will send another request
if status == 401:
i = 1
while i < len(out_lines) - 1:
if len(out_lines[i]) == 0:
next_line = out_lines[i + 1]
if DEBUG >= 5: print "Second header: [%s]" % next_line
if next_line[:5] == "HTTP/":
status = int(next_line.split(" ")[1]) # HTTP/1.0 200 OK
i = i + 1
self.assertEqual(status, expect_status)
if expect_output:
output = out.split("\r\n\r\n", 1)[1]
self.assertEqual(output, expect_output)
if verifier:
self.assertTrue(verifier(out))
def test_get_not_exist(self):
not_exist = "%s%s" % (self.server_root, "no_exist")
self.curl("GET", not_exist, expect_status = 404)
def test_put_new_file(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "Line 1 \n Line 2"
content2 = "Line 1 \n Line 2\nLine 3\n"
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
self.curl("GET", new_file, expect_output = content)
# Upload again
# Expect: 201 Created
self.curl("GET", new_file, {"action": "PUT", "content": content},
expect_status = 201)
self.curl("GET", new_file, expect_output = content)
# Try to change the content
# Expect: 201 created
self.curl("PUT", new_file, {"content": content2}, expect_status = 201)
self.curl("GET", new_file, expect_output = content2)
# clean up
self.curl("DELETE", new_file)
def test_create_file_but_a_directory_exists(self):
new_dir = "%s%s/" % (self.server_root, "new_file")
self.curl("PUT", new_dir, expect_status = 201)
new_file = "%s%s" % (self.server_root, "new_file")
self.curl("PUT", new_file, expect_status = 403)
# clean up
self.curl("DELETE", new_dir)
def test_create_directory_but_a_file_exists(self):
new_file = "%s%s" % (self.server_root, "new_file")
self.curl("PUT", new_file, expect_status = 201)
new_dir = "%s%s/" % (self.server_root, "new_file")
self.curl("PUT", new_dir, expect_status = 403)
# clean up
self.curl("DELETE", new_file)
def test_delete_nonexist(self):
filename = "%s%s" % (self.server_root, "new_file")
self.curl("DELETE", filename, expect_status = 404)
def test_update_nonexist(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "Line 1 \n Line 2"
self.curl("UPDATE", new_file, {"content": content}, expect_status = 403)
def test_update_directory(self):
new_dir = "%s%s/" % (self.server_root, "new_file")
new_file = "%s%s" % (self.server_root, "new_file")
content = "Line 1 \n Line 2"
self.curl("PUT", new_dir, expect_status = 201)
self.curl("UPDATE", new_file, {"content": content}, expect_status = 403)
# clean up
self.curl("DELETE", new_dir)
def test_update_file(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "Line 1 \n Line 2"
content2 = "Line 1 \n Line 2\nLine 3\n"
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
self.curl("GET", new_file, expect_output = content)
# Upload again
# Expect: 200 OK
self.curl("UPDATE", new_file, {"content": content2})
self.curl("GET", new_file, expect_output = content2)
# Upload again (with action=UPDATE)
# Expect: 200 OK
self.curl("GET", new_file, {"action": "UPDATE", "content": content})
self.curl("GET", new_file, expect_output = content)
# clean up
self.curl("DELETE", new_file)
def test_content_special(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "Line\0 1 \n Line 2\r\n% < & >"
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
self.curl("GET", new_file, expect_output = content)
# clean up
self.curl("DELETE", new_file)
def test_ls_dir_in_json(self):
new_file = "%s%s" % (self.server_root, "new_file")
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, expect_status = 201)
self.curl("GET", new_file)
# List the dir
content = '[{"type": 2, "name": "new_file"}]'
path = "%s?output_format=json" % self.server_root
self.curl("GET", path, expect_output = content)
# List the dir
content = '[{"type": 2, "name": "new_file"}]'
self.curl("GET", self.server_root, {'output_format': 'application/json'},
expect_output = content)
# clean up
self.curl("DELETE", new_file)
def test_get_file_in_json(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "content=xxx" # recursive description
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
#
# See if "content=xxx" is encoded in the JSON["content"].
def content_verifier(out):
output = out.split("\r\n\r\n", 1)[1]
return json.loads(output)["content"] == content
self.curl("GET", new_file, {"output": "json"}, verifier = content_verifier)
#
# See if Last-Modified in the returned HTTP response headers
def LM_verifier(out):
headers = out.split("\r\n\r\n", 1)[0].split("\r\n")
for h in headers:
if h.split(":")[0] == "Last-Modified":
return True
return False
self.curl("GET", new_file, {"output": "json"}, verifier = LM_verifier)
# clean up
self.curl("DELETE", new_file)
def test_last_modified(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "v0"
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
self.curl("GET", new_file, {}, expect_output = content)
#
# See if Last-Modified in the returned HTTP response headers
def LM_verifier(out):
headers = out.split("\r\n\r\n", 1)[0].split("\r\n")
for h in headers:
if h.split(":")[0] == "Last-Modified":
return True
return False
self.curl("GET", new_file, {}, verifier = LM_verifier)
#
# Specify an older if-modified-since, expect full content.
self.curl("GET", new_file, {}, expect_output = content, extra_headers = [
("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT"),
])
#
# Specify an newer if-modified-since, expect 304
self.curl("GET", new_file, {}, expect_output = "", expect_status = 304,
extra_headers = [
("If-Modified-Since", "Thu, 1 Jan 2038 00:00:00 GMT"),
])
# clean up
self.curl("DELETE", new_file)
def test_longpoll_no_modify(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "v0"
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
catcher = Catcher()
self.curl("GET", new_file, {}, expect_output = content,
verifier = lambda out: catcher.parse_header(out))
last_modified = catcher.headers["Last-Modified"]
self.curl("GET", new_file, {"longpoll": "3"},
expect_output = "", expect_status = 304,
extra_headers = [
("If-Modified-Since", last_modified),
])
# clean up
self.curl("DELETE", new_file)
def test_longpoll_non_exist(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "v1"
def delayed_curl():
time.sleep(3)
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
t = threading.Thread(target = delayed_curl, args=())
t.daemon = True # Program exits and leaves threads.
t.start()
self.curl("GET", new_file, {"longpoll": "6"},
expect_output = content, expect_status = 200)
# clean up
self.curl("DELETE", new_file)
def test_longpoll_modify(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "v1"
content2 = "v2"
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
catcher = Catcher()
self.curl("GET", new_file, {}, expect_output = content,
verifier = lambda out: catcher.parse_header(out))
last_modified = catcher.headers["Last-Modified"]
def delayed_curl():
time.sleep(3)
self.curl("PUT", new_file, {"content": content2}, expect_status = 201)
t = threading.Thread(target = delayed_curl, args=())
t.daemon = True # Program exits and leaves threads.
t.start()
self.curl("GET", new_file, {"longpoll": "6"},
expect_output = content2, expect_status = 200,
extra_headers = [
("If-Modified-Since", last_modified),
], verifier = lambda out: catcher.parse_header(out))
last_modified2 = catcher.headers["Last-Modified"]
self.assertNotEqual(last_modified, last_modified2)
# clean up
self.curl("DELETE", new_file)
def test_longpoll_modify_no_if_modified_since_as_chrome(self):
new_file = "%s%s" % (self.server_root, "new_file")
content = "v1"
content2 = "v2"
# Upload a new file
# Expect: 200 OK
self.curl("PUT", new_file, {"content": content}, expect_status = 201)
def delayed_curl():
time.sleep(3)
self.curl("PUT", new_file, {"content": content2}, expect_status = 201)
t = threading.Thread(target = delayed_curl, args=())
t.daemon = True # Program exits and leaves threads.
t.start()
self.curl("GET", new_file, {"longpoll": "6"},
expect_output = content2, expect_status = 200,
extra_headers = [])
# clean up
self.curl("DELETE", new_file)
def test_http_auth(self):
self.curl("GET", self.server_url + "/uid/5566", expect_output = "I am 5566\n")
self.curl("GET", self.server_url + "/noauth")
self.curl("GET", self.server_url + "/noauth/file")
self.curl("GET", self.server_url + "/noauth/noauth")
self.curl("GET", self.server_url + "/noauth/noauth/file")
self.curl("GET", self.server_url + "/auth", expect_status = 401)
self.curl("GET", self.server_url + "/auth", userpass = "/auth/.:auth1234")
self.curl("GET", self.server_url + "/auth/aFirst", expect_status = 401)
self.curl("GET", self.server_url + "/auth/aFirst", userpass = "/auth/aFirst:a234")
self.curl("GET", self.server_url + "/auth/auth", expect_status = 401)
self.curl("GET", self.server_url + "/auth/auth/file", expect_status = 401)
self.curl("GET", self.server_url + "/auth/noauth", expect_status = 401)
self.curl("GET", self.server_url + "/auth/noauth/file", expect_status = 401)
self.curl("GET", self.server_url + "/auth/auth/noauth", expect_status = 401)
self.curl("GET", self.server_url + "/auth/auth/noauth/file", expect_status = 401)
self.curl("GET", self.server_url + "/auth/public")
def tearDown(self):
self.curl("DELETE", self.server_root)
self.curl("QUIT", self.server_url)
class TestHttpd(BaseCases, unittest.TestCase):
curl_protocol = "http"
curl_args = []
def start_daemon(self):
httpd.run_server(False, self.server_port,
StoppableHttpServer, StoppableHttpHandler, self)
class TestHttpsd(BaseCases, unittest.TestCase):
curl_protocol = "https"
curl_args = ["--insecure"]
def start_daemon(self):
httpd.run_server(True, self.server_port,
StoppableHttpsServer, StoppableHttpsHandler, self)
if __name__ == '__main__':
unittest.main()