Merge pull request #15 from noone234/fix-content-type

Fix content type
This commit is contained in:
Andrew Rabert 2019-01-09 16:20:05 -05:00 committed by GitHub
commit b3b06e5f99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View file

@ -278,6 +278,8 @@ class HTTPClientMixin(object):
"""
if not response.body:
return None
if 'Content-Type' not in response.headers:
return response.body
try:
content_type = algorithms.select_content_type(
[headers.parse_content_type(response.headers['Content-Type'])],

View file

@ -1,6 +1,9 @@
import io
import json
import logging
import os
import sys
import unittest
import uuid
from tornado import httpclient, httputil, testing, web
@ -413,3 +416,18 @@ class MixinTestCase(testing.AsyncHTTPTestCase):
client = httpclient.AsyncHTTPClient()
self.assertEqual(client.max_clients, 25)
@testing.gen_test()
@unittest.skipUnless(sys.version_info >= (3, ),
'StringIO requires Python 3')
def test_missing_content_type(self):
# Craft a response that lacks a Content-Type header.
request = httpclient.HTTPRequest(
self.get_url('/test?foo=bar&status_code=200'))
response = httpclient.HTTPResponse(
request, code=200, headers={},
buffer=io.StringIO('Do not try to deserialize me.'))
# Try to deserialize that response. It should not raise an exception.
try:
response_body = self.mixin._http_resp_deserialize(response)
except KeyError:
self.fail('http_fetch raised KeyError!')