Response data renamed

This commit is contained in:
Artur Maciag 2017-11-06 14:05:06 +00:00
parent 3541793ff3
commit 128971185a
4 changed files with 27 additions and 26 deletions

View file

@ -45,9 +45,9 @@ class RequestValidationResult(BaseValidationResult):
class ResponseValidationResult(BaseValidationResult):
def __init__(self, errors, body=None, headers=None):
def __init__(self, errors, data=None, headers=None):
super(ResponseValidationResult, self).__init__(errors)
self.body = body
self.data = data
self.headers = headers
@ -137,7 +137,7 @@ class ResponseValidator(object):
def validate(self, request, response):
errors = []
body = None
data = None
headers = {}
try:
@ -145,7 +145,7 @@ class ResponseValidator(object):
# don't process if server errors
except OpenAPIMappingError as exc:
errors.append(exc)
return ResponseValidationResult(errors, body, headers)
return ResponseValidationResult(errors, data, headers)
operation_pattern = request.full_url_pattern.replace(
server.default_url, '')
@ -156,14 +156,14 @@ class ResponseValidator(object):
# don't process if operation errors
except OpenAPIMappingError as exc:
errors.append(exc)
return ResponseValidationResult(errors, body, headers)
return ResponseValidationResult(errors, data, headers)
try:
operation_response = operation.get_response(str(response.status))
# don't process if invalid response status code
except InvalidResponse as exc:
errors.append(exc)
return ResponseValidationResult(errors, body, headers)
return ResponseValidationResult(errors, data, headers)
if operation_response.content:
try:
@ -172,19 +172,19 @@ class ResponseValidator(object):
errors.append(exc)
else:
try:
raw_body = self._get_raw_body(response)
raw_data = self._get_raw_data(response)
except MissingBody as exc:
errors.append(exc)
else:
try:
body = media_type.unmarshal(raw_body)
data = media_type.unmarshal(raw_data)
except OpenAPIMappingError as exc:
errors.append(exc)
return ResponseValidationResult(errors, body, headers)
return ResponseValidationResult(errors, data, headers)
def _get_raw_body(self, response):
if not response.body:
raise MissingBody("Missing required response body")
def _get_raw_data(self, response):
if not response.data:
raise MissingBody("Missing required response data")
return response.body
return response.data

View file

@ -116,8 +116,8 @@ class BaseOpenAPIResponse(object):
class MockResponse(BaseOpenAPIRequest):
def __init__(self, body, status=200, mimetype='application/json'):
self.body = body
def __init__(self, data, status=200, mimetype='application/json'):
self.data = data
self.status = status
self.mimetype = mimetype
@ -129,8 +129,8 @@ class FlaskOpenAPIResponse(BaseOpenAPIResponse):
self.response = response
@property
def body(self):
return self.response.text
def data(self):
return self.response.data
@property
def status(self):

View file

@ -181,7 +181,7 @@ class TestResponseValidator(object):
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidServer
assert result.body is None
assert result.data is None
assert result.headers == {}
def test_invalid_operation(self, validator):
@ -192,7 +192,7 @@ class TestResponseValidator(object):
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidOperation
assert result.body is None
assert result.data is None
assert result.headers == {}
def test_invalid_response(self, validator):
@ -203,7 +203,7 @@ class TestResponseValidator(object):
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidResponse
assert result.body is None
assert result.data is None
assert result.headers == {}
def test_invalid_content_type(self, validator):
@ -214,7 +214,7 @@ class TestResponseValidator(object):
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidContentType
assert result.body is None
assert result.data is None
assert result.headers == {}
def test_missing_body(self, validator):
@ -225,7 +225,7 @@ class TestResponseValidator(object):
assert len(result.errors) == 1
assert type(result.errors[0]) == MissingBody
assert result.body is None
assert result.data is None
assert result.headers == {}
def test_invalid_media_type_value(self, validator):
@ -236,7 +236,7 @@ class TestResponseValidator(object):
assert len(result.errors) == 1
assert type(result.errors[0]) == InvalidMediaTypeValue
assert result.body is None
assert result.data is None
assert result.headers == {}
def test_get_pets(self, validator):
@ -254,5 +254,5 @@ class TestResponseValidator(object):
result = validator.validate(request, response)
assert result.errors == []
assert result.body == response_json
assert result.data == response_json
assert result.headers == {}

View file

@ -92,7 +92,7 @@ class TestFlaskOpenAPIRequest(object):
assert openapi_request.mimetype == request.mimetype
class TetsFlaskOpenAPIResponse(object):
class TestFlaskOpenAPIResponse(object):
@pytest.fixture
def response_factory(self):
@ -105,6 +105,7 @@ class TetsFlaskOpenAPIResponse(object):
openapi_response = FlaskOpenAPIResponse(response)
assert openapi_response.body == response.text
assert openapi_response.response == response
assert openapi_response.data == response.data
assert openapi_response.status == response.status
assert openapi_response.mimetype == response.mimetype