mirror of
https://github.com/kennethreitz/httpbin.git
synced 2026-06-05 23:00:18 +00:00
Fix bug that was causing POSTs missing a content-type header to 500
Closes #141
This commit is contained in:
+3
-3
@@ -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
@@ -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'), '*')
|
||||
|
||||
Reference in New Issue
Block a user