forked from ctb/cse491-serverz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.py
executable file
·130 lines (92 loc) · 3.98 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
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):
if len(self.sent) < 10000: # For huge resolution images
self.sent += s
def close(self):
self.is_closed = True
def test_handle_connection():
conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")
expected_return = 'HTTP/1.0 200 OK'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_handle_content_connection():
conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")
expected_return = 'HTTP/1.0 200 OK'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_handle_file_connection():
conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
expected_return = 'HTTP/1.0 200 OK'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_handle_image_connection():
conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")
expected_return = 'HTTP/1.0 200 OK'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_form_connection():
conn = FakeConnection('GET /submit?firstname=Robert&lastname=Paulson' + \
' HTTP/1.0\r\n\r\n')
expected_return = 'HTTP/1.0 200 OK'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_app_post_connection():
conn = FakeConnection('POST / HTTP/1.0\r\n' + \
'From: test@testserver\r\n' + \
'Content-Type: application/x-www-form-urlencoded\r\n'
'Content-Length: 33\r\n' + \
'\r\n'
'firstname=Robert&lastname=Paulson')
expected_return = 'HTTP/1.0 200 OK'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_multi_post_connection():
conn = FakeConnection('POST /submit HTTP/1.1\r\n' + \
'Host: arctic.cse.msu.edu:9684\r\n' + \
'Connection: keep-alive\r\n' + \
'Content-Length: 248\r\n' + \
'Content-Type: multipart/form-data; boundary=----' + \
'WebKitFormBoundaryeDbkALVq8cSTv4RB\r\n\r\n' + \
'------WebKitFormBoundaryeDbkALVq8cSTv4RB\r\n' + \
'Content-Disposition: form-data; name="firstname"\r\n\r\n' +
'Robert\r\n------WebKitFormBoundaryeDbkALVq8cSTv4RB\r\n' +
'Content-Disposition: form-data; name="lastname"\r\n\r\n' +
'Paulson\r\n------WebKitFormBoundaryeDbkALVq8cSTv4RB--\r\n')
expected_return = 'HTTP/1.0 200 OK'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_bad_connection():
conn = FakeConnection('GET /autobiography HTTP/1.0\r\n\r\n')
expected_return = 'HTTP/1.0 404 Not Found'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_put_connection():
conn = FakeConnection('PUT / HTTP/1.0\r\n\r\n')
expected_return = 'HTTP/1.0 404 Not Found'
server.handle_connection(conn)
splitconn = conn.sent.split('\r\n')[0]
assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)