Fix bug that was causing POSTs missing a content-type header to 500

Closes #141
This commit is contained in:
Kevin McCarthy
2014-06-05 17:56:34 -10:00
parent a109bbbe29
commit 32c842f06f
2 changed files with 17 additions and 5 deletions
+3 -3
View File
@@ -75,13 +75,12 @@ def json_safe(string, content_type='application/octet-stream'):
suitable for binary data, some additional encoding was necessary; "data"
URL scheme was chosen for its simplicity.
"""
try:
_encoded = json.dumps(string)
return string
except (ValueError, TypeError):
return b''.join([
b'data:',
b'data:',
content_type.encode('utf-8'),
b';base64,',
base64.b64encode(string)
@@ -94,7 +93,8 @@ def get_files():
files = dict()
for k, v in request.files.items():
val = json_safe(v.read(), request.files[k].content_type)
content_type = request.files[k].content_type or 'application/octet-stream'
val = json_safe(v.read(), content_type)
if files.get(k):
if not isinstance(files[k], list):
files[k] = [files[k]]
+14 -2
View File
@@ -4,6 +4,7 @@ import base64
import unittest
from werkzeug.http import parse_dict_header
from hashlib import md5
from six import BytesIO
import httpbin
@@ -34,16 +35,27 @@ class HttpbinTestCase(unittest.TestCase):
content_type='application/octet-stream')
self.assertEqual(response.status_code, 200)
def test_post_file_text(self):
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_file_binary(self):
def test_post_body_binary(self):
with open('httpbin/core.pyc','rb') as f:
response = self.app.post('/post', data={"file": f.read()})
self.assertEqual(response.status_code, 200)
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.
response = self.app.post(
'/post',
content_type='multipart/form-data; boundary=bound',
data = '--bound\r\nContent-Disposition: form-data; name="media"; filename="test.bin"\r\n\r\n\xa5\xc6\n--bound--\r\n'
)
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'), '*')