forked from ctb/cse491-serverz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.py
139 lines (100 loc) · 4.22 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
import server
import requests
import jinja2
TemplateDir = "./templates"
ldr = jinja2.FileSystemLoader(TemplateDir)
env = jinja2.Environment(loader=ldr)
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 getsockname(self):
return ("address", "port")
# Test a basic GET call.
def test_handle_connection():
conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert "Welcome" in conn.sent , 'Got: %s' % (repr(conn.sent),)
def test_default_path():
conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert "Content" in conn.sent and "File" in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_content_path():
conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert "content" in conn.sent , 'Got: %s' % (repr(conn.sent),)
def test_file_path():
conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert "text/plain" in conn.sent , 'Got: %s' % (repr(conn.sent),)
def test_image_path():
conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert "image/jpeg" in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_form_get():
conn = FakeConnection("GET /submit?firstname=hello&lastname=world HTTP/1.0\r\n\r\n")
server.handle_connection(conn)
assert "Mr. hello world" in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_form_post():
conn = FakeConnection('POST /submit HTTP/1.1\r\n' +\
'Host: arctic.cse.msu.edu:9176\r\n' +\
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131030 Firefox/17.0 Iceweasel/17.0.10\r\n' +\
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n' +\
'Accept-Language: en-US,en;q=0.5\r\n' +\
'Accept-Encoding: gzip, deflate\r\n' +\
'Connection: keep-alive\r\n' +\
'Referer: http://arctic.cse.msu.edu:9176/formPost\r\n' +\
'Content-Type: application/x-www-form-urlencoded\r\n' +\
'Content-Length: 50\r\n' +\
'\r\n' +\
'firstname=hello&lastname=world&submit=Submit+Query\r\n')
server.handle_connection(conn)
assert "Mr. hello world" in conn.sent , 'Got: %s' % (repr(conn.sent),)
def test_form_post__multi():
reqString = 'POST /submit HTTP/1.1\r\n' +\
'Host: arctic.cse.msu.edu:9853\r\n' +\
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131030 Firefox/17.0 Iceweasel/17.0.10\r\n' +\
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n' +\
'Accept-Language: en-US,en;q=0.5\r\n' +\
'Accept-Encoding: gzip, deflate\r\n' +\
'Connection: keep-alive\r\n' +\
'Referer: http://arctic.cse.msu.edu:9853/formPost\r\n'+\
'Content-Type: multipart/form-data; boundary=---------------------------10925359777073771901781915428\r\n' +\
'Content-Length: 422\r\n' +\
'\r\n' +\
'-----------------------------10925359777073771901781915428\r\n' +\
'Content-Disposition: form-data; name="firstname"\r\n' +\
'\r\n' +\
'hello\r\n' +\
'-----------------------------10925359777073771901781915428\r\n' +\
'Content-Disposition: form-data; name="lastname"\r\n' +\
'\r\n' +\
'world\r\n' +\
'-----------------------------10925359777073771901781915428\r\n' +\
'Content-Disposition: form-data; name="submit"\r\n' +\
'\r\n' +\
'Submit Query\r\n' +\
'-----------------------------10925359777073771901781915428--\r\n'
conn = FakeConnection(reqString)
server.handle_connection(conn)
assert "Mr. hello world" in conn.sent, 'Wrong page: %s' % (repr(conn.sent),)
def test_404():
conn = FakeConnection("GET /feiap HTTP/1.1\r\n\r\n")
server.handle_connection(conn)
assert "404 error" in conn.sent, "Got: %s" % (repr(conn.sent),)