forked from ctb/cse491-serverz
/
test_server.py
148 lines (112 loc) · 4.75 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
import server
from app import make_app
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
def test_handle_connection_slash():
conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")
er = 'HTTP/1.0 200 OK\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_handle_connection_content():
conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")
er = 'HTTP/1.0 200 OK\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_handle_connection_file():
conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
er = 'HTTP/1.0 200 OK\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_handle_connection_image():
conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")
er = 'HTTP/1.0 200 OK\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_get_form():
conn = FakeConnection("GET /form HTTP/1.0\r\n\r\n")
er = 'HTTP/1.0 200 OK\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_404():
conn = FakeConnection("GET /asdf HTTP/1.0\r\n\r\n")
er = 'HTTP/1.0 404 Not Found\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_submit_get():
fname = "Ben"
lname = "Taylor"
conn = FakeConnection("GET /submit?firstname={0}&lastname={1} \
HTTP/1.0\r\n\r\n".format(fname, lname))
er = 'HTTP/1.0 200 OK\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_submit_post_urlencoded():
fname = "Ben"
lname = "Taylor"
conn = FakeConnection("POST /submit HTTP/1.0\r\n" + \
"Content-Length: 29\r\n" + \
"Content-Type: application/x-www-form-urlencoded\r\n\r\n" + \
"firstname={0}&lastname={1}\r\n".format(fname, lname))
er = 'HTTP/1.0 200 OK\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_submit_post_multipart():
conn = FakeConnection("POST /submit HTTP/1.0\r\n" + \
"Content-Length: 374\r\n" + \
"Content-Type: multipart/form-data; " + \
"boundary=32452685f36942178a5f36fd94e34b63\r\n\r\n" + \
"--32452685f36942178a5f36fd94e34b63\r\n" + \
"Content-Disposition: form-data; name=\"lastname\";" + \
" filename=\"lastname\"\r\n\r\n" + \
"taylor\r\n" + \
"--32452685f36942178a5f36fd94e34b63\r\n" + \
"Content-Disposition: form-data; name=\"firstname\";" + \
" filename=\"firstname\"\r\n\r\n" + \
"ben\r\n" + \
"--32452685f36942178a5f36fd94e34b63\r\n" + \
"Content-Disposition: form-data; name=\"key\";" + \
" filename=\"key\"\r\n\r\n" + \
"value\r\n" + \
"--32452685f36942178a5f36fd94e34b63--\r\n"
)
fname = 'ben'
lname = 'taylor'
er = 'HTTP/1.0 200 OK\r\n'
app = make_app()
server.handle_connection(conn, 80, app)
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_submit_post_404():
conn = FakeConnection("POST /asdf HTTP/1.0\r\n" + \
"Content-Length: 0\r\n" + \
"Content-Type: application/x-www-form-urlencoded\r\n\r\n"
)
app = make_app()
server.handle_connection(conn, 80, app)
er = 'HTTP/1.0 404 Not Found\r\n'
assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)