Check response invalid content type

This commit is contained in:
Artur Maciag 2017-11-06 13:53:49 +00:00
parent 08fdf7c9aa
commit 3541793ff3
3 changed files with 20 additions and 1 deletions

View file

@ -3,6 +3,7 @@ from functools import lru_cache
from six import iteritems
from openapi_core.exceptions import InvalidContentType
from openapi_core.media_types import MediaTypeGenerator
from openapi_core.parameters import ParametersGenerator
@ -18,6 +19,13 @@ class Response(object):
self.content = content and dict(content) or {}
self.links = links and dict(links) or {}
def __getitem__(self, mimetype):
try:
return self.content[mimetype]
except KeyError:
raise InvalidContentType(
"Invalid mime type `{0}`".format(mimetype))
class ResponsesGenerator(object):

View file

@ -167,7 +167,7 @@ class ResponseValidator(object):
if operation_response.content:
try:
media_type = operation_response.content[response.mimetype]
media_type = operation_response[response.mimetype]
except OpenAPIMappingError as exc:
errors.append(exc)
else:

View file

@ -206,6 +206,17 @@ class TestResponseValidator(object):
assert result.body is None
assert result.headers == {}
def test_invalid_content_type(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets')
response = MockResponse('Not Found', mimetype='text/csv')
result = validator.validate(request, response)
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidContentType
assert result.body is None
assert result.headers == {}
def test_missing_body(self, validator):
request = MockRequest(self.host_url, 'get', '/v1/pets')
response = MockResponse(None)