def test_get_pet_not_found(self, spec, response_validator): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets/{petId}' view_args = { 'petId': '1', } request = MockRequest( host_url, 'GET', '/pets/1', path_pattern=path_pattern, view_args=view_args, ) parameters = request.get_parameters(spec) assert parameters == { 'path': { 'petId': 1, } } body = request.get_body(spec) assert body is None data_json = { 'code': 404, 'message': 'Not found', 'rootCause': 'Pet not found', } data = json.dumps(data_json) response = MockResponse(data, status_code=404) response_result = response_validator.validate(request, response) assert response_result.errors == [] assert response_result.data == data_json
def test_get_pets(self, spec, response_validator): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets' query_params = { 'limit': '20', } request = MockRequest( host_url, 'GET', '/pets', path_pattern=path_pattern, args=query_params, ) parameters = request.get_parameters(spec) body = request.get_body(spec) assert parameters == { 'query': { 'limit': 20, 'page': 1, 'search': '', } } assert body is None data_json = { 'data': [], } data = json.dumps(data_json) response = MockResponse(data) response_result = response_validator.validate(request, response) assert response_result.errors == [] assert response_result.data == data_json
def test_invalid_media_type_value(self, validator): request = MockRequest(self.host_url, 'get', '/v1/pets') response = MockResponse('\{\}') result = validator.validate(request, response) assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidMediaTypeValue assert result.data is None assert result.headers == {}
def test_missing_body(self, validator): request = MockRequest(self.host_url, 'get', '/v1/pets') response = MockResponse(None) result = validator.validate(request, response) assert len(result.errors) == 1 assert type(result.errors[0]) == MissingBody assert result.data 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.data is None assert result.headers == {}
def test_invalid_response(self, validator): request = MockRequest(self.host_url, 'get', '/v1/pets') response = MockResponse('Not Found', status_code=409) result = validator.validate(request, response) assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidResponse assert result.data is None assert result.headers == {}
def test_invalid_server(self, validator): request = MockRequest('http://petstore.invalid.net/v1', 'get', '/') response = MockResponse('Not Found', status_code=404) result = validator.validate(request, response) assert len(result.errors) == 1 assert type(result.errors[0]) == InvalidServer assert result.data is None assert result.headers == {}
def test_get_pets(self, validator): request = MockRequest(self.host_url, 'get', '/v1/pets') response_json = { 'data': [ { 'id': 1, }, ], } response_data = json.dumps(response_json) response = MockResponse(response_data) result = validator.validate(request, response) assert result.errors == [] assert result.data == response_json assert result.headers == {}
def test_get_pet(self, spec, response_validator): host_url = 'http://petstore.swagger.io/v1' path_pattern = '/v1/pets/{petId}' view_args = { 'petId': '1', } request = MockRequest( host_url, 'GET', '/pets/1', path_pattern=path_pattern, view_args=view_args, ) parameters = request.get_parameters(spec) assert parameters == { 'path': { 'petId': 1, } } body = request.get_body(spec) assert body is None data_json = { 'data': { 'id': 1, 'name': 'test', }, } data = json.dumps(data_json) response = MockResponse(data) response_result = response_validator.validate(request, response) assert response_result.errors == [] assert response_result.data == data_json