Repository navigation
Expand file tree
/
Copy pathtest_client.py
More file actions
executable file
·152 lines (134 loc) · 4.65 KB
/
Copy pathtest_client.py
File metadata and controls
executable file
·152 lines (134 loc) · 4.65 KB
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
#!/usr/bin/python
import sys
import time
import urllib2
import os
from urllib2 import urlopen,HTTPError
TIMEOUT = 5
NORMAL_FILES = ['/index.html', '/foo/bar.html', '/test.pdf', '/test.txt', '/test.png', '/test.jpg', '/tmp.txt']
REDIRECTS = [['/cats', 'http://en.wikipedia.org/wiki/Cat']]
NOTFOUNDS = ['/redirect.defs', '/not/a/real/url.html', '/']
# prevents us from following redirects, so we can get the real response code
class DontFollowHttpRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_307(self, req, fp, code, msg, headers):
raise urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)
http_error_302 = http_error_303 = http_error_307 #= http_error_301
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
test_fail = FAIL + 'FAIL' + ENDC
test_pass = OKGREEN + 'PASS' + ENDC
def build_url(host, port, filename):
return 'http://' + host + ':' + port + filename
# returns request, response, code
# response is None for 4xx or 5xx response
def maybe_fetch(host, port, filename, op='GET'):
request = urllib2.Request(build_url(host, port, filename))
request.get_method = lambda : op
opener = urllib2.build_opener(DontFollowHttpRedirectHandler)
response = None
try:
response = opener.open(request)
except HTTPError as e:
return request, None, e.code
return request, response, response.code
def test_POST(host, port):
print 'test POST (unsupported)'
for filename in NORMAL_FILES:
request, response, code = maybe_fetch(host, port, filename, op='POST')
if code != 403:
return bcolors.test_fail
return bcolors.test_pass
def test_INVALID(host, port):
print 'test INVALID'
for filename in NORMAL_FILES:
request, response, code = maybe_fetch(host, port, filename, op='FOOBAR')
if code != 403 and code < 500:
return bcolors.test_fail
return bcolors.test_pass
def test_200(host, port, opcode='GET'):
print 'test 200s ' + opcode
for filename in NORMAL_FILES:
print '\t%s' % (filename)
request, response, code = maybe_fetch(host, port, filename, op=opcode)
if code != 200 or response is None:
return bcolors.test_fail
if opcode=='HEAD' and len(response.readlines()) != 0:
return bcolors.test_fail
return bcolors.test_pass
def test_404(host, port):
print 'test 404s'
for filename in NOTFOUNDS:
print '\t%s' % (filename)
request, response, code = maybe_fetch(host, port, filename)
if code != 404 or response != None:
return bcolors.test_fail
return bcolors.test_pass
def test_301(host, port, opcode='GET'):
print 'test 301s ' + opcode
for filename, redirect in REDIRECTS:
print '\t%s--->%s' % (filename, redirect)
request, response, code = maybe_fetch(host, port, filename, op=opcode)
# because we followed the redirect, the code should actually be 200,
# and there should be a response.
if code != 200 or response == None:
return bcolors.test_fail
if response.url != redirect:
return bcolors.test_fail
return bcolors.test_pass
def expected_response_type(fileExtension):
if fileExtension == '.html':
return 'text/html'
elif fileExtension == '.txt':
return 'text/plain'
elif fileExtension == '.pdf':
return 'application/pdf'
elif fileExtension == '.png':
return 'image/png'
elif fileExtension in {'.jpeg', '.jpg'}:
return 'image/jpeg'
else:
return ''
def test_response_type(host, port, opcode='GET'):
print 'test Valid Response Type ' + opcode
isPass = True
for filename in NORMAL_FILES:
request, response, code = maybe_fetch(host, port, filename, op=opcode)
if response.headers.type != expected_response_type(os.path.splitext(filename)[1]):
print '\t' + filename + ' ' + response.headers.type + ' ' + bcolors.test_fail
isPass = False
else:
print '\t' + filename + ' ' + response.headers.type + ' ' + bcolors.test_pass
if isPass:
return bcolors.test_pass
else:
return bcolors.test_fail
def parse_flags(argv):
arg_map = {}
if len(argv) <= 1: return {}
for arg in argv[1:]:
bits = arg.split('=')
if len(bits) == 2:
arg_map[bits[0]] = bits[1]
return arg_map
if __name__ == '__main__':
arg_map = parse_flags(sys.argv)
if ('--host' not in arg_map) or ('--port' not in arg_map):
print 'usage: project1_testclient.py --host=linux2 --port=12345'
sys.exit(-1)
host = arg_map['--host']
port = arg_map['--port']
print test_200(host, port) + '\n'
print test_404(host, port) + '\n'
print test_301(host, port) + '\n'
print test_200(host, port, opcode='HEAD') + '\n'
print test_301(host, port, opcode='HEAD') + '\n'
print test_POST(host, port) + '\n'
print test_INVALID(host, port) + '\n'
print test_response_type(host, port) + '\n'