-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
202 lines (172 loc) · 7.71 KB
/
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
#!/usr/bin/env python3
#
# TODO(karl): Add the rest of the tests!
#
# Test script for the Bookmark Server.
#
# The server should be listening on port 8000, answer a GET request to /
# with an HTML document, answer a POST request by testing the provided URI
# and responding accordingly, and answer a GET request to /name depending
# on whether the name was previously stored or not.
import requests, random, socket
def test_CheckURI_bad():
'''The CheckURI code should return False for a bad URI.'''
print("Testing CheckURI function for a bad URI.")
from BookmarkServer import CheckURI
try:
bad = CheckURI("this is a bad uri")
except NotImplementedError:
return ("CheckURI raised NotImplementedError.\n"
"Do step 1, and make sure to remove the 'raise' line.")
if bad is not False:
return ("CheckURI returned {} on a bad URI instead of False.".format(
bad))
else:
print("CheckURI correctly tested a bad URI.")
return None
def test_CheckURI_good():
'''The CheckURI code should return True for a good URI.'''
print("Testing CheckURI function for a good URI.")
from BookmarkServer import CheckURI
try:
good = CheckURI("https://www.google.com/")
except NotImplementedError:
return ("CheckURI raised NotImplementedError.\n"
"Do step 1, and make sure to remove the 'raise' line.")
if good is not True:
return ("CheckURI returned {} on a good URI instead of True.\n"
"(Or maybe Google is down.)".format(good))
else:
print("CheckURI correctly tested a good URI.")
return None
def test_connect():
'''Try connecting to the server.'''
print("Testing connecting to the server.")
try:
with socket.socket() as s:
s.connect(("localhost", 8000))
print("Connection attempt succeeded.")
return None
except socket.error:
return "Server didn't answer on localhost port 8000. Is it running?"
def test_GET_root():
'''The server should accept a GET and return the form.'''
print("Testing GET request.")
uri = "http://localhost:8000/"
try:
r = requests.get(uri)
except requests.RequestException as e:
return ("Couldn't communicate with the server. ({})\n"
"If it's running, take a look at its output.").format(e)
if r.status_code == 501:
return ("The server returned status code 501 Not Implemented.\n"
"This means it doesn't know how to handle a GET request.\n"
"(Is the correct server code running?)")
elif r.status_code != 200:
return ("The server returned status code {} instead of a 200 OK."
).format(r.status_code)
elif not r.headers['content-type'].startswith('text/html'):
return ("The server didn't return Content-type: text/html.")
elif '<title>Bookmark Server</title>' not in r.text:
return ("The server didn't return the form text I expected.")
else:
print("GET request succeeded!")
return None
def test_POST_nodata():
'''The server should accept a POST and return 400 error on empty form.'''
print("Testing POST request with empty form.")
uri = "http://localhost:8000/"
data = {}
try:
r = requests.post(uri, data=data, allow_redirects=False)
except requests.ConnectionError as e:
return ("Server dropped the connection. Step 1 or 5 isn't done yet?")
if r.status_code == 501:
return ("The server returned status code 501 Not Implemented.\n"
"This means it doesn't know how to handle a POST request.\n"
"(Is the correct server code running?)")
elif r.status_code != 400:
return ("Server returned status code {} instead of 400 when I gave\n"
"it an empty form in a POST request.".format(r.status_code))
else:
print("POST request with bad URI correctly got a 400.")
return None
def test_POST_bad():
'''The server should accept a POST and return 404 error on bad URI.'''
print("Testing POST request with bad URI.")
uri = "http://localhost:8000/"
data = {'shortname': 'bad', 'longuri': 'this is fake'}
try:
r = requests.post(uri, data=data, allow_redirects=False)
except requests.ConnectionError as e:
return ("Server dropped the connection. Step 1 or 5 isn't done yet?")
if r.status_code == 501:
return ("The server returned status code 501 Not Implemented.\n"
"This means it doesn't know how to handle a POST request.\n"
"(Is the correct server code running?)")
elif r.status_code != 404:
return ("Server returned status code {} instead of 404 when I gave\n"
"it a bad URI in a POST request.".format(r.status_code))
else:
print("POST request with bad URI correctly got a 404.")
return None
def test_POST_good():
'''The server should accept a POST with a good URI and redirect to root.'''
print("Testing POST request with good URI.")
uri = "http://localhost:8000/"
data = {'shortname': 'google', 'longuri': 'http://www.google.com/'}
try:
r = requests.post(uri, data=data, allow_redirects=False)
except requests.ConnectionError as e:
return ("Server dropped the connection. Step 1 or 5 isn't done yet?")
if r.status_code == 501:
return ("The server returned status code 501 Not Implemented.\n"
"This means it doesn't know how to handle a POST request.\n"
"(Is the correct server code running?)")
elif r.status_code != 303:
return ("Server returned status code {} instead of 303 when I gave\n"
"it a good URI in a POST request.".format(r.status_code))
elif 'location' not in r.headers:
return ("Server returned a 303 redirect with no Location header.")
elif r.headers['location'] != '/':
return ("Server returned redirect to {} instead of to /."
.format(r.headers['location']))
else:
print("POST request with bad URI correctly got a 303 to /.")
return None
def test_GET_path():
'''The server should redirect on a GET to a recorded URI.'''
uri = "http://localhost:8000/google"
orig = "http://www.google.com/"
print("Testing GET request to {}.".format(uri))
try:
r = requests.get(uri, allow_redirects=False)
except requests.ConnectionError as e:
return ("Server dropped the connection. Step 1 or 5 isn't done yet?")
if r.status_code == 501:
return ("The server returned status code 501 Not Implemented.\n"
"This means it doesn't know how to handle a GET request.\n"
"(Is the correct server code running?)")
elif r.status_code != 303:
return ("Server returned status code {} instead of 303 when I asked\n"
"for it to follow a short URI.".format(r.status_code))
elif 'location' not in r.headers:
return ("Server returned a 303 with no Location header.")
elif r.headers['location'] != 'http://www.google.com/':
return ("Server returned a 303, but with a Location header of {}\n"
"when I expected it to be http://www.google.com/."
.format(r.headers('location')))
else:
print("GET request to {} returned 303 to {} successfully"
.format(uri, orig))
if __name__ == '__main__':
tests = [test_CheckURI_bad, test_CheckURI_good, test_connect,
test_GET_root, test_POST_nodata, test_POST_bad, test_POST_good,
test_GET_path]
for test in tests:
problem = test()
if problem is not None:
print(problem)
break
if not problem:
print("All tests succeeded!")