-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_async_wsgi_py.py
221 lines (188 loc) · 8.31 KB
/
test_async_wsgi_py.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import requests
import unittest
import socket
class TestAsyncHTTPServer(unittest.TestCase):
host = "http://localhost"
port = 9000
def setUp(self):
pass
def test_empty_request(self):
"""Send empty request"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", self.port))
s.sendall(b"\n")
s.close()
def test_server_header(self):
"""Server header exists"""
r = requests.get(f'{self.host}:{self.port}')
server = r.headers.get('Server')
self.assertIsNotNone(server)
def test_date_header(self):
"""Date header exists"""
r = requests.get(f'{self.host}:{self.port}')
date = r.headers.get('Date')
self.assertIsNotNone(date)
def test_directory_index(self):
"""Directory index file exists"""
r = requests.get(f'{self.host}:{self.port}')
data = r.content.decode()
length = r.headers.get('Content-Length')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 34)
self.assertEqual(len(data), 34)
self.assertEqual(data, "<html>Directory index file</html>\n")
def test_index_not_found(self):
"""Directory index file absent"""
r = requests.get(f'{self.host}:{self.port}/403/')
self.assertEqual(int(r.status_code), 403)
def test_file_not_found(self):
"""Absent file returns 404"""
r = requests.get(f'{self.host}:{self.port}/404/index.html')
self.assertEqual(int(r.status_code), 404)
def test_file_in_nested_folders(self):
"""File located in nested folders"""
r = requests.get(f'{self.host}:{self.port}/dir1/dir2/dir3/quote.txt')
data = r.content
length = r.headers.get('Content-Length')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 62)
self.assertEqual(len(data), 62)
self.assertEqual(data.decode(), 'Would you tell me, please, which way I ought to go from here?\n')
def test_file_with_query_string(self):
"""Slash after filename"""
r = requests.get(f'{self.host}:{self.port}/404/page.html/')
self.assertEqual(r.status_code, 404)
def test_file_with_query_string(self):
"""Query string after filename"""
r = requests.get(f'{self.host}:{self.port}/dir1/page.html?arg1=value&arg2=value')
data = r.content
length = r.headers.get('Content-Length')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 38)
self.assertEqual(len(data), 38)
self.assertEqual(data.decode(), '<html><body>Page sample</body></html>\n')
def test_file_with_spaces(self):
"""Filename with spaces"""
r = requests.get(f'{self.host}:{self.port}/dir1/space%20in%20name.txt')
data = r.content
length = r.headers.get('Content-Length')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 19)
self.assertEqual(len(data), 19)
self.assertEqual(data.decode(), 'letters and spaces\n')
def test_file_urlencoded(self):
"""Urlencoded filename"""
r = requests.get(f'{self.host}:{self.port}/dir1/%70%61%67%65%2e%68%74%6d%6c')
data = r.content
length = r.headers.get('Content-Length')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 38)
self.assertEqual(len(data), 38)
self.assertEqual(data.decode(), '<html><body>Page sample</body></html>\n')
def test_document_root_escaping(self):
"""Document root escaping forbidden"""
r = requests.get(f'{self.host}:{self.port}/dir1/../../../../../../../../../../../../../etc/passwd')
self.assertIn(r.status_code, (400, 403, 404))
def test_file_with_dot_in_name(self):
"""File with two dots in name"""
r = requests.get(f'{self.host}:{self.port}/dir1/text..txt')
data = r.content.decode()
length = r.headers.get('Content-Length')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 6)
self.assertEqual(len(data), 6)
self.assertIn('hello', data)
def test_post_method(self):
"""Post method forbidden"""
r = requests.post(f'{self.host}:{self.port}/dir1/page.html')
self.assertIn(int(r.status_code), (400, 405))
def test_head_method(self):
"""Head method support"""
r = requests.head(f'{self.host}:{self.port}/dir1/page.html')
data = r.content.decode()
length = r.headers.get('Content-Length')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 38)
self.assertEqual(len(data), 0)
def test_filetype_html(self):
"""Content-Type for .html"""
r = requests.get(f'{self.host}:{self.port}/dir1/page.html')
data = r.content
length = r.headers.get('Content-Length')
ctype = r.headers.get('Content-Type')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 38)
self.assertEqual(len(data), 38)
self.assertEqual(ctype, "text/html")
def test_filetype_css(self):
"""Content-Type for .css"""
r = requests.get(f'{self.host}:{self.port}/dir1/bootstrap.css')
data = r.content
length = r.headers.get('Content-Length')
ctype = r.headers.get('Content-Type')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 122849)
self.assertEqual(len(data), 122849)
self.assertEqual(ctype, "text/css")
def test_filetype_js(self):
"""Content-Type for .js"""
r = requests.get(f'{self.host}:{self.port}/dir1/bootstrap.js')
data = r.content
length = r.headers.get('Content-Length')
ctype = r.headers.get('Content-Type')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 58533)
self.assertEqual(len(data), 58533)
self.assertIn(ctype, ("application/x-javascript", "application/javascript", "text/javascript"))
def test_filetype_jpg(self):
"""Content-Type for .jpg"""
r = requests.get(f'{self.host}:{self.port}/dir1/cat.jpg')
data = r.content
length = r.headers.get('Content-Length')
ctype = r.headers.get('Content-Type')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 140391)
self.assertEqual(len(data), 140391)
self.assertEqual(ctype, "image/jpeg")
def test_filetype_jpeg(self):
"""Content-Type for .jpeg"""
r = requests.get(f'{self.host}:{self.port}/dir1/nature.jpeg')
data = r.content
length = r.headers.get('Content-Length')
ctype = r.headers.get('Content-Type')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 31706)
self.assertEqual(len(data), 31706)
self.assertEqual(ctype, "image/jpeg")
def test_filetype_png(self):
"""Content-Type for .png"""
r = requests.get(f'{self.host}:{self.port}/dir1/logo.png')
data = r.content
length = r.headers.get('Content-Length')
ctype = r.headers.get('Content-Type')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 19832)
self.assertEqual(len(data), 19832)
self.assertEqual(ctype, "image/png")
def test_filetype_gif(self):
"""Content-Type for .gif"""
r = requests.get(f'{self.host}:{self.port}/dir1/pokemon.gif')
data = r.content
length = r.headers.get('Content-Length')
ctype = r.headers.get('Content-Type')
self.assertEqual(r.status_code, 200)
self.assertEqual(int(length), 123602)
self.assertEqual(len(data), 123602)
self.assertEqual(ctype, "image/gif")
loader = unittest.TestLoader()
suite = unittest.TestSuite()
a = loader.loadTestsFromTestCase(TestAsyncHTTPServer)
suite.addTest(a)
class NewResult(unittest.TextTestResult):
def getDescription(self, test):
doc_first_line = test.shortDescription()
return doc_first_line or ""
class NewRunner(unittest.TextTestRunner):
resultclass = NewResult
runner = NewRunner(verbosity=2)
runner.run(suite)