forked from ctb/cse491-serverz
/
test_server.py
172 lines (123 loc) · 5.72 KB
/
test_server.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
import server
class FakeConnection(object):
"""
A fake connection class that mimics a real TCP socket for the purpose
of testing socket I/O.
"""
def __init__(self, to_recv):
self.to_recv = to_recv
self.sent = ""
self.is_closed = False
def recv(self, n):
if n > len(self.to_recv):
r = self.to_recv
self.to_recv = ""
return r
r, self.to_recv = self.to_recv[:n], self.to_recv[n:]
return r
def send(self, s):
self.sent += s
def close(self):
self.is_closed = True
# Test basic GET calls.
# Test path = /
def test_handle_connection():
conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 200' in conn.sent and 'form' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Test path = /content
def test_handle_connection_content():
conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 200' in conn.sent and 'content' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Test path = /file
def test_handle_connection_file():
conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 200' in conn.sent and 'text/plain' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Test path = /content
def test_handle_connection_image():
conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 200' in conn.sent and 'image/jpeg' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Test path = /submit
def test_handle_submit():
conn = FakeConnection("GET /submit?firstname=Taylor&lastname=Swift" + \
" HTTP/1.1\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'html' in conn.sent and "Taylor" in conn.sent \
and 'Swift' in conn.sent, 'Got: %s' % (repr(conn.sent),)
# Test a submit with no first name
def test_handle_submit_no_first_name():
conn = FakeConnection("GET /submit?firstname=&lastname=Swift" + \
" HTTP/1.1\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'html' in conn.sent and "Swift" in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Tests a submit with no last name
def test_handle_submit_no_last_name():
conn = FakeConnection("GET /submit?firstname=Taylor&lastname=" + \
" HTTP/1.1\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'html' in conn.sent and "Taylor" in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# test 404
def test_handle_not_found():
conn = FakeConnection("GET /poop HTTP/1.0\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Test POST connections
# Test / requests
def test_handle_connection_post():
conn = FakeConnection("POST / HTTP/1.0\r\n" + \
"Content-length: 0\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 200' in conn.sent and 'form' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Test /submit requests (both types)
def test_handle_submit_post():
conn = FakeConnection("POST /submit HTTP/1.1\r\n" + \
"Content-Length: 31\r\n\r\n" + \
"firstname=Taylor&lastname=Swift")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 200' in conn.sent and "Hello Mrs." in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_submit_post_multipart_and_form_data():
conn = FakeConnection("POST /submit " + \
"HTTP/1.1\r\nContent-length: 246\r\n\r\n------" + \
"WebKitFormBoundaryAaal27xQakxMcNYm\r\n" + \
'Content-Disposition: form-data; name="firstname"\r\n\r\nTaylor' + \
'\r\n------WebKitFormBoundaryAaal27xQakxMcNYm\r\n' + \
'Content-Disposition: form-data; name="lastname"\r\n\r\nSwift' + \
'\r\n------WebKitFormBoundaryAaal27xQakxMcNYm--")')
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 200' in conn.sent and "Hello Mrs." in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# test 404
def test_handle_not_found_post():
conn = FakeConnection("POST /butts HTTP/1.1\r\n" + \
"Content-Length: 31\r\n\r\n" + \
"firstname=Taylor&lastname=Swift")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Handle large request
def test_handle_long_request():
firstname = lastname = "asdfasdfasdfasdfasdf" * 100
conn = FakeConnection("POST /submit HTTP/1.1\r\n" + \
"Content-Length: 4020\r\n\r\n" + \
"firstname=%s&lastname=%s" % (firstname, lastname))
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 200' in conn.sent and "Hello Mrs." in conn.sent, \
'Got: %s' % (repr(conn.sent),)
# Test an empty request
def test_handle_empty_request():
conn = FakeConnection("\r\n\r\n")
server.handle_connection(conn, "arctic.cse.msu.edu", "9943")
assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
'Got: %s' % (repr(conn.sent),)