Skip to content

Commit

Permalink
Handle application/json headers with and without charset specified
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinmit committed Jul 22, 2020
1 parent 3c2f009 commit 583f58c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion shinysdr/i/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def render_GET(self, request):
return json.dumps(self.__record).encode('utf-8')

def render_POST(self, request):
assert request.getHeader(b'Content-Type') == b'application/json; charset=utf-8'
assert request.getHeader(b'Content-Type') in (b'application/json', b'application/json; charset=utf-8')
if not self.__database.writable:
request.setResponseCode(http.FORBIDDEN)
request.setHeader(b'Content-Type', b'text/plain')
Expand Down
4 changes: 2 additions & 2 deletions shinysdr/i/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def test_index_common(self):
@defer.inlineCallbacks
def test_index_response(self):
response, data = yield http_get(reactor, self.__url('/'))
self.assertEqual(response.headers.getRawHeaders('Content-Type'), ['application/json'])
self.assertEqual(response.headers.getRawHeaders('Content-Type'), ['application/json; charset=utf-8'])
j = json.loads(data)
self.assertEqual(j, self.response_json)

Expand All @@ -300,7 +300,7 @@ def test_record_common(self):
@defer.inlineCallbacks
def test_record_response(self):
response, data = yield http_get(reactor, self.__url('/1'))
self.assertEqual(response.headers.getRawHeaders('Content-Type'), ['application/json'])
self.assertEqual(response.headers.getRawHeaders('Content-Type'), ['application/json; charset=utf-8'])
j = json.loads(data)
self.assertEqual(j, self.test_records[1])

Expand Down
2 changes: 1 addition & 1 deletion shinysdr/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def _assert_http_response_properties(test_case, response, data):

if data is not None: # not a HEAD request
content_type = response.headers.getRawHeaders(b'Content-Type')[0]
if content_type == 'application/json':
if content_type in ('application/json', 'application/json; charset=utf-8'):
json.loads(data) # raises error if it doesn't parse
elif content_type.startswith('text/html'):
test_case.assertRegex(content_type, r'(?i)text/html;\s*charset=utf-8')
Expand Down

0 comments on commit 583f58c

Please sign in to comment.