mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-22 03:00:10 +00:00
Check response invalid content type
This commit is contained in:
parent
08fdf7c9aa
commit
3541793ff3
3 changed files with 20 additions and 1 deletions
|
@ -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):
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue