forked from ctb/cse491-serverz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.py
182 lines (113 loc) · 5.08 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
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 a basic GET call.
def test_handle_connection():
conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert 'HTTP/1.0 200' in conn.sent and 'form' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_connection_content():
conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert 'HTTP/1.0 200' in conn.sent and 'content' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_connenction_file():
conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert 'HTTP/1.0 200' in conn.sent and 'file' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_connection_image():
conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")
expected_return = 'HTTP/1.0 200 OK\r\n' + \
'Content-type: text/html\r\n\r\n' + \
'<h1><marquee>Much image.</marquee></h1>'
server.handle_connection(conn)
print conn.sent
assert 'HTTP/1.0 200' in conn.sent and 'image' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_submit():
conn = FakeConnection("GET /submit?firstName=Ari&lastName=Polavarapu" + \
" HTTP/1.1\r\n\r\n")
server.handle_connection(conn)
assert 'html' in conn.sent and 'Ari' in conn.sent \
and 'Polavarapu' in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_handle_submit_no_first_name():
conn = FakeConnection("GET /submit?firstName=&lastname=Polavarapu" + \
" HTTP/1.1\r\n\r\n")
server.handle_connection(conn)
assert 'html' in conn.sent and "Polavarapu" in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_submit_no_last_name():
conn = FakeConnection("GET /submit?firstname=Ari&lastname=" + \
" HTTP/1.1\r\n\r\n")
server.handle_connection(conn)
assert 'html' in conn.sent and "Ari" in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_not_found():
conn = FakeConnection("GET /nada HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_connection_post():
conn = FakeConnection("POST / HTTP/1.0\r\n" + \
"Content-length: 0\r\n\r\n")
server.handle_connection(conn)
assert 'HTTP/1.0 200' in conn.sent and 'form' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_submit_post():
conn = FakeConnection("POST /submit HTTP/1.1\r\n" + \
"Content-Length: 31\r\n\r\n" + \
"firstname=Ari&lastname=Polavarapu")
server.handle_connection(conn)
assert 'HTTP/1.0 200' 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------" + \
"WebKitFormBoundaryAaa127xQakxMcNYm\r\n" + \
'Content-Disposition: form-data; name="firstname"\r\n\r\nAri' + \
'\r\n------WebKitFormBoundaryAaa127xQakxMcNYm\r\n' + \
'Content-Disposition: form-data; name="lastname"\r\n\r\nPolavarapu' + \
'\r\n------WebKitFormBoundaryAaa127xQakxMcNYm--")')
server.handle_connection(conn)
assert 'HTTP/1.0 200' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_not_found_post():
conn = FakeConnection("POST /nada HTTP/1.1\r\n" + \
"Content-Length: 31\r\n\r\n" + \
"firstname=Ari&lastname=Polavarapu")
server.handle_connection(conn)
assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_long_request():
firstname = lastname = "VeryLongMuchLongMuchMuchLong" * 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)
assert 'HTTP/1.0 200' in conn.sent, \
'Got: %s' % (repr(conn.sent),)
def test_handle_empty_request():
conn = FakeConnection("\r\n\r\n")
server.handle_connection(conn)
assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
'Got: %s' % (repr(conn.sent),)