forked from postmanlabs/httpbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_httpbin.py
executable file
·458 lines (393 loc) · 16.8 KB
/
test_httpbin.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import base64
import unittest
import contextlib
import six
import json
from werkzeug.http import parse_dict_header
from hashlib import md5
from six import BytesIO
import httpbin
@contextlib.contextmanager
def _setenv(key, value):
"""Context manager to set an environment variable temporarily."""
old_value = os.environ.get(key, None)
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
yield
if old_value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
def _string_to_base64(string):
"""Encodes string to utf-8 and then base64"""
utf8_encoded = string.encode('utf-8')
return base64.urlsafe_b64encode(utf8_encoded)
class HttpbinTestCase(unittest.TestCase):
"""Httpbin tests"""
def setUp(self):
httpbin.app.debug = True
self.app = httpbin.app.test_client()
def test_response_headers_simple(self):
response = self.app.get('/response-headers?animal=dog')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get_all('animal'), ['dog'])
assert json.loads(response.data.decode('utf-8'))['animal'] == 'dog'
def test_response_headers_multi(self):
response = self.app.get('/response-headers?animal=dog&animal=cat')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get_all('animal'), ['dog', 'cat'])
assert json.loads(response.data.decode('utf-8'))['animal'] == ['dog', 'cat']
def test_get(self):
response = self.app.get('/get', headers={'User-Agent': 'test'})
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['args'], {})
self.assertEqual(data['headers']['Host'], 'localhost')
self.assertEqual(data['headers']['Content-Type'], '')
self.assertEqual(data['headers']['Content-Length'], '0')
self.assertEqual(data['headers']['User-Agent'], 'test')
self.assertEqual(data['origin'], None)
self.assertEqual(data['url'], 'http://localhost/get')
self.assertTrue(response.data.endswith(b'\n'))
def test_base64(self):
greeting = u'Здравствуй, мир!'
b64_encoded = _string_to_base64(greeting)
response = self.app.get(b'/base64/' + b64_encoded)
content = response.data.decode('utf-8')
self.assertEqual(greeting, content)
def test_post_binary(self):
response = self.app.post('/post',
data=b'\x01\x02\x03\x81\x82\x83',
content_type='application/octet-stream')
self.assertEqual(response.status_code, 200)
def test_post_body_text(self):
with open('httpbin/core.py') as f:
response = self.app.post('/post', data={"file": f.read()})
self.assertEqual(response.status_code, 200)
def test_post_body_binary(self):
response = self.app.post(
'/post',
data={"file": b'\x01\x02\x03\x81\x82\x83'})
self.assertEqual(response.status_code, 200)
def test_post_body_unicode(self):
response = self.app.post('/post', data=u'оживлённым'.encode('utf-8'))
self.assertEqual(json.loads(response.data.decode('utf-8'))['data'], u'оживлённым')
def test_post_file_with_missing_content_type_header(self):
# I built up the form data manually here because I couldn't find a way
# to convince the werkzeug test client to send files without the
# content-type of the file set.
data = '--bound\r\nContent-Disposition: form-data; name="media"; '
data += 'filename="test.bin"\r\n\r\n\xa5\xc6\n--bound--\r\n'
response = self.app.post(
'/post',
content_type='multipart/form-data; boundary=bound',
data=data,
)
self.assertEqual(response.status_code, 200)
def test_set_cors_headers_after_request(self):
response = self.app.get('/get')
self.assertEqual(
response.headers.get('Access-Control-Allow-Origin'), '*'
)
def test_set_cors_credentials_headers_after_auth_request(self):
response = self.app.get('/basic-auth/foo/bar')
self.assertEqual(
response.headers.get('Access-Control-Allow-Credentials'), 'true'
)
def test_set_cors_headers_after_request_with_request_origin(self):
response = self.app.get('/get', headers={'Origin': 'origin'})
self.assertEqual(
response.headers.get('Access-Control-Allow-Origin'), 'origin'
)
def test_set_cors_headers_with_options_verb(self):
response = self.app.open('/get', method='OPTIONS')
self.assertEqual(
response.headers.get('Access-Control-Allow-Origin'), '*'
)
self.assertEqual(
response.headers.get('Access-Control-Allow-Credentials'), 'true'
)
self.assertEqual(
response.headers.get('Access-Control-Allow-Methods'),
'GET, POST, PUT, DELETE, PATCH, OPTIONS'
)
self.assertEqual(
response.headers.get('Access-Control-Max-Age'), '3600'
)
# FIXME should we add any extra headers?
self.assertNotIn(
'Access-Control-Allow-Headers', response.headers
)
def test_set_cors_allow_headers(self):
response = self.app.open('/get', method='OPTIONS', headers={'Access-Control-Request-Headers': 'X-Test-Header'})
self.assertEqual(
response.headers.get('Access-Control-Allow-Headers'), 'X-Test-Header'
)
def test_user_agent(self):
response = self.app.get(
'/user-agent', headers={'User-Agent': 'test'}
)
self.assertIn('test', response.data.decode('utf-8'))
self.assertEqual(response.status_code, 200)
def test_gzip(self):
response = self.app.get('/gzip')
self.assertEqual(response.status_code, 200)
def test_digest_auth_with_wrong_password(self):
auth_header = 'Digest username="user",realm="wrong",nonce="wrong",uri="/digest-auth/user/passwd",response="wrong",opaque="wrong"'
response = self.app.get(
'/digest-auth/auth/user/passwd',
environ_base={
# httpbin's digest auth implementation uses the remote addr to
# build the nonce
'REMOTE_ADDR': '127.0.0.1',
},
headers={
'Authorization': auth_header,
}
)
assert 'Digest' in response.headers.get('WWW-Authenticate')
def test_digest_auth(self):
# make first request
unauthorized_response = self.app.get(
'/digest-auth/auth/user/passwd',
environ_base={
# digest auth uses the remote addr to build the nonce
'REMOTE_ADDR': '127.0.0.1',
}
)
# make sure it returns a 401
self.assertEqual(unauthorized_response.status_code, 401)
header = unauthorized_response.headers.get('WWW-Authenticate')
auth_type, auth_info = header.split(None, 1)
# Begin crappy digest-auth implementation
d = parse_dict_header(auth_info)
a1 = b'user:' + d['realm'].encode('utf-8') + b':passwd'
ha1 = md5(a1).hexdigest().encode('utf-8')
a2 = b'GET:/digest-auth/auth/user/passwd'
ha2 = md5(a2).hexdigest().encode('utf-8')
a3 = ha1 + b':' + d['nonce'].encode('utf-8') + b':' + ha2
auth_response = md5(a3).hexdigest()
auth_header = 'Digest username="user",realm="' + \
d['realm'] + \
'",nonce="' + \
d['nonce'] + \
'",uri="/digest-auth/auth/user/passwd",response="' + \
auth_response + \
'",opaque="' + \
d['opaque'] + '"'
# make second request
authorized_response = self.app.get(
'/digest-auth/auth/user/passwd',
environ_base={
# httpbin's digest auth implementation uses the remote addr to
# build the nonce
'REMOTE_ADDR': '127.0.0.1',
},
headers={
'Authorization': auth_header,
}
)
# done!
self.assertEqual(authorized_response.status_code, 200)
def test_drip(self):
response = self.app.get('/drip?numbytes=400&duration=2&delay=1')
self.assertEqual(response.content_length, 400)
self.assertEqual(len(response.get_data()), 400)
self.assertEqual(response.status_code, 200)
def test_drip_with_custom_code(self):
response = self.app.get('/drip?numbytes=400&duration=2&code=500')
self.assertEqual(response.content_length, 400)
self.assertEqual(len(response.get_data()), 400)
self.assertEqual(response.status_code, 500)
def test_get_bytes(self):
response = self.app.get('/bytes/1024')
self.assertEqual(len(response.get_data()), 1024)
self.assertEqual(response.status_code, 200)
def test_bytes_with_seed(self):
response = self.app.get('/bytes/10?seed=0')
# The RNG changed in python3, so even though we are
# setting the seed, we can't expect the value to be the
# same across both interpreters.
if six.PY3:
self.assertEqual(
response.data, b'\xc5\xd7\x14\x84\xf8\xcf\x9b\xf4\xb7o'
)
else:
self.assertEqual(
response.data, b'\xd8\xc2kB\x82g\xc8Mz\x95'
)
def test_stream_bytes(self):
response = self.app.get('/stream-bytes/1024')
self.assertEqual(len(response.get_data()), 1024)
self.assertEqual(response.status_code, 200)
def test_stream_bytes_with_seed(self):
response = self.app.get('/stream-bytes/10?seed=0')
# The RNG changed in python3, so even though we are
# setting the seed, we can't expect the value to be the
# same across both interpreters.
if six.PY3:
self.assertEqual(
response.data, b'\xc5\xd7\x14\x84\xf8\xcf\x9b\xf4\xb7o'
)
else:
self.assertEqual(
response.data, b'\xd8\xc2kB\x82g\xc8Mz\x95'
)
def test_delete_endpoint_returns_body(self):
response = self.app.delete(
'/delete',
data={'name': 'kevin'},
content_type='application/x-www-form-urlencoded'
)
form_data = json.loads(response.data.decode('utf-8'))['form']
self.assertEqual(form_data, {'name': 'kevin'})
def test_methods__to_status_endpoint(self):
methods = [
'GET',
'HEAD',
'POST',
'PUT',
'DELETE',
'PATCH',
'TRACE',
]
for m in methods:
response = self.app.open(path='/status/418', method=m)
self.assertEqual(response.status_code, 418)
def test_xml_endpoint(self):
response = self.app.get(path='/xml')
self.assertEqual(
response.headers.get('Content-Type'), 'application/xml'
)
def test_x_forwarded_proto(self):
response = self.app.get(path='/get', headers={
'X-Forwarded-Proto':'https'
})
assert json.loads(response.data.decode('utf-8'))['url'].startswith('https://')
def test_redirect_n_higher_than_1(self):
response = self.app.get('/redirect/5')
self.assertEqual(
response.headers.get('Location'), '/relative-redirect/4'
)
def test_redirect_absolute_param_n_higher_than_1(self):
response = self.app.get('/redirect/5?absolute=true')
self.assertEqual(
response.headers.get('Location'), 'http://localhost/absolute-redirect/4'
)
def test_redirect_n_equals_to_1(self):
response = self.app.get('/redirect/1')
self.assertEqual(response.status_code, 302)
self.assertEqual(
response.headers.get('Location'), '/get'
)
def test_relative_redirect_n_equals_to_1(self):
response = self.app.get('/relative-redirect/1')
self.assertEqual(
response.headers.get('Location'), '/get'
)
def test_relative_redirect_n_higher_than_1(self):
response = self.app.get('/relative-redirect/7')
self.assertEqual(response.status_code, 302)
self.assertEqual(
response.headers.get('Location'), '/relative-redirect/6'
)
def test_absolute_redirect_n_higher_than_1(self):
response = self.app.get('/absolute-redirect/5')
self.assertEqual(
response.headers.get('Location'), 'http://localhost/absolute-redirect/4'
)
def test_absolute_redirect_n_equals_to_1(self):
response = self.app.get('/absolute-redirect/1')
self.assertEqual(response.status_code, 302)
self.assertEqual(
response.headers.get('Location'), 'http://localhost/get'
)
def test_request_range(self):
response1 = self.app.get('/range/1234')
self.assertEqual(response1.status_code, 200)
self.assertEqual(response1.headers.get('ETag'), 'range1234')
self.assertEqual(response1.headers.get('Content-range'), 'bytes 0-1233/1234')
self.assertEqual(response1.headers.get('Accept-ranges'), 'bytes')
self.assertEqual(len(response1.get_data()), 1234)
response2 = self.app.get('/range/1234')
self.assertEqual(response2.status_code, 200)
self.assertEqual(response2.headers.get('ETag'), 'range1234')
self.assertEqual(response1.get_data(), response2.get_data())
def test_request_range_with_parameters(self):
response = self.app.get(
'/range/100?duration=1.5&chunk_size=5',
headers={ 'Range': 'bytes=10-24' }
)
self.assertEqual(response.status_code, 206)
self.assertEqual(response.headers.get('ETag'), 'range100')
self.assertEqual(response.headers.get('Content-range'), 'bytes 10-24/100')
self.assertEqual(response.headers.get('Accept-ranges'), 'bytes')
self.assertEqual(response.get_data(), 'klmnopqrstuvwxy'.encode('utf8'))
def test_request_range_first_15_bytes(self):
response = self.app.get(
'/range/1000',
headers={ 'Range': 'bytes=0-15' }
)
self.assertEqual(response.status_code, 206)
self.assertEqual(response.headers.get('ETag'), 'range1000')
self.assertEqual(response.get_data(), 'abcdefghijklmnop'.encode('utf8'))
self.assertEqual(response.headers.get('Content-range'), 'bytes 0-15/1000')
def test_request_range_open_ended_last_6_bytes(self):
response = self.app.get(
'/range/26',
headers={ 'Range': 'bytes=20-' }
)
self.assertEqual(response.status_code, 206)
self.assertEqual(response.headers.get('ETag'), 'range26')
self.assertEqual(response.get_data(), 'uvwxyz'.encode('utf8'))
self.assertEqual(response.headers.get('Content-range'), 'bytes 20-25/26')
def test_request_range_suffix(self):
response = self.app.get(
'/range/26',
headers={ 'Range': 'bytes=-5' }
)
self.assertEqual(response.status_code, 206)
self.assertEqual(response.headers.get('ETag'), 'range26')
self.assertEqual(response.get_data(), 'vwxyz'.encode('utf8'))
self.assertEqual(response.headers.get('Content-range'), 'bytes 21-25/26')
def test_request_out_of_bounds(self):
response = self.app.get(
'/range/26',
headers={ 'Range': 'bytes=10-5',
}
)
self.assertEqual(response.status_code, 416)
self.assertEqual(response.headers.get('ETag'), 'range26')
self.assertEqual(len(response.get_data()), 0)
self.assertEqual(response.headers.get('Content-range'), 'bytes */26')
response = self.app.get(
'/range/26',
headers={ 'Range': 'bytes=32-40',
}
)
self.assertEqual(response.status_code, 416)
response = self.app.get(
'/range/26',
headers={ 'Range': 'bytes=0-40',
}
)
self.assertEqual(response.status_code, 416)
def test_tracking_disabled(self):
with _setenv('HTTPBIN_TRACKING', None):
response = self.app.get('/')
data = response.data.decode('utf-8')
self.assertNotIn('google-analytics', data)
self.assertNotIn('perfectaudience', data)
def test_tracking_enabled(self):
with _setenv('HTTPBIN_TRACKING', '1'):
response = self.app.get('/')
data = response.data.decode('utf-8')
self.assertIn('google-analytics', data)
self.assertIn('perfectaudience', data)
if __name__ == '__main__':
unittest.main()