-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathtests.py
205 lines (145 loc) · 5.9 KB
/
tests.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
import unittest
import subprocess
import http.client
import os
class WebTestCase(unittest.TestCase):
"""tests for the echo server and client"""
def setUp(self):
self.server_process = subprocess.Popen(
[
"python",
"http_server.py"
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
def tearDown(self):
self.server_process.kill()
self.server_process.communicate()
def get_response(self, url):
"""
Helper function to get a response from a given url, using http.client
"""
conn = http.client.HTTPConnection('localhost:10000')
conn.request('GET', url)
response = conn.getresponse()
conn.close()
return response
def test_post_yields_method_not_allowed(self):
"""
Sending a POST request should yield a 405 Method Not Allowed response
"""
conn = http.client.HTTPConnection('localhost:10000')
conn.request('POST', '/')
response = conn.getresponse()
conn.close()
self.assertEqual(response.getcode(), 405)
def test_get_sample_text_content(self):
"""
A call to /sample.txt returns the correct body
"""
file = 'sample.txt'
local_path = os.path.join('webroot', *file.split('/'))
web_path = '/' + file
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
self.assertEqual(response.getcode(), 200, error_comment)
with open(local_path, 'rb') as f:
self.assertEqual(f.read(), response.read(), error_comment)
def test_get_sample_text_mime_type(self):
"""
A call to /sample.txt returns the correct mimetype
"""
file = 'sample.txt'
web_path = '/' + file
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
self.assertEqual(response.getcode(), 200, error_comment)
self.assertEqual(response.getheader('Content-Type'), 'text/plain', error_comment)
def test_get_sample_scene_balls_jpeg(self):
"""
A call to /images/Sample_Scene_Balls.jpg returns the correct body
"""
file = 'images/Sample_Scene_Balls.jpg'
local_path = os.path.join('webroot', *file.split('/'))
web_path = '/' + file
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
self.assertEqual(response.getcode(), 200, error_comment)
with open(local_path, 'rb') as f:
self.assertEqual(f.read(), response.read(), error_comment)
def test_get_sample_scene_balls_jpeg_mime_type(self):
"""
A call to /images/Sample_Scene_Balls.jpg returns the correct mimetype
"""
file = 'images/Sample_Scene_Balls.jpg'
web_path = '/' + file
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
self.assertEqual(response.getcode(), 200, error_comment)
self.assertEqual(response.getheader('Content-Type'), 'image/jpeg', error_comment)
def test_get_sample_1_png(self):
"""
A call to /images/sample_1.png returns the correct body
"""
file = 'images/sample_1.png'
local_path = os.path.join('webroot', *file.split('/'))
web_path = '/' + file
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
self.assertEqual(response.getcode(), 200, error_comment)
with open(local_path, 'rb') as f:
self.assertEqual(f.read(), response.read(), error_comment)
def test_get_sample_1_png_mime_type(self):
"""
A call to /images/sample_1.png returns the correct mimetype
"""
file = 'images/sample_1.png'
web_path = '/' + file
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
self.assertEqual(response.getcode(), 200, error_comment)
self.assertEqual(response.getheader('Content-Type'), 'image/png', error_comment)
def test_get_404(self):
"""
A call to /asdf.txt (a file which does not exist in webroot) yields a 404 error
"""
file = 'asdf.txt'
web_path = '/' + file
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
self.assertEqual(response.getcode(), 404, error_comment)
def test_images_index(self):
"""
A call to /images/ yields a list of files in the images directory
"""
directory = 'images'
local_path = os.path.join('webroot', directory)
web_path = '/' + directory
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
body = response.read().decode()
for path in os.listdir(local_path):
self.assertIn(path, body, error_comment)
def test_root_index(self):
"""
A call to / yields a list of files in the images directory
"""
directory = ''
local_path = os.path.join('webroot', directory)
web_path = '/' + directory
error_comment = "Error encountered while visiting " + web_path
response = self.get_response(web_path)
body = response.read().decode()
for path in os.listdir(local_path):
self.assertIn(path, body, error_comment)
def test_ok_response_at_root_index(self):
"""
A call to / at least yields a 200 OK response
"""
directory = ''
web_path = '/' + directory
response = self.get_response(web_path)
self.assertEqual(response.getcode(), 200)
if __name__ == '__main__':
unittest.main()