def test_hostname_if_passed_overrides_origin_url(httprettified, swagger_dict): register_get("http://foo/test_http?", body='') swagger_dict['host'] = 'foo' register_spec(swagger_dict) resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP(test_param="foo").result() assert ["foo"] == httpretty.last_request().querystring['test_param']
def test_invalid_spec_raises_SwaggerValidationError(httprettified, swagger_dict): swagger_dict['paths']['/test_http']['get']['parameters'][0]['type'] = 'X' register_spec(swagger_dict) with pytest.raises(SwaggerValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL) assert 'is not valid' in str(excinfo.value)
def test_invalid_type_in_response_raises_ValidationError( httprettified, swagger_dict, sample_model): register_spec(swagger_dict) register_get("http://localhost/test_http", body='"NOT_COMPLEX_TYPE"') with pytest.raises(ValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL).api_test.testHTTP().result() assert "'NOT_COMPLEX_TYPE' is not of type" in str(excinfo.value)
def test_default_value_in_request(httprettified, swagger_dict): swagger_dict['paths']['/test_http']['get']['parameters'][0]['default'] = 'X' register_spec(swagger_dict) register_get("http://localhost/test_http?") resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP().result() assert ['X'] == httpretty.last_request().querystring['test_param']
async def test_invalid_spec_raises_SwaggerValidationError( swagger_dict, http_client): swagger_dict['paths']['/test_http']['get']['parameters'][0]['type'] = 'X' register_spec(swagger_dict) with pytest.raises(SwaggerValidationError) as excinfo: await swagger_client(http_client) assert 'is not valid' in str(excinfo.value)
async def test_correct_route_with_basePath_no_slash(swagger_dict, http_client): register_get("http://localhost/lame/test/test_http?test_param=foo", body=u'""') swagger_dict["basePath"] = "/lame/test" register_spec(swagger_dict) client = await swagger_client(http_client) assert await client.api_test.testHTTP(test_param="foo").result() is None
def test_basePath_works(httprettified, swagger_dict): swagger_dict["basePath"] = "/append" register_spec(swagger_dict) register_get("http://localhost/append/test_http?test_param=foo") resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP(test_param="foo").result() assert ["foo"] == httpretty.last_request().querystring['test_param']
def test_correct_route_with_basePath_no_slash(httprettified, swagger_dict): register_get("http://localhost/lame/test/test_http?test_param=foo", body=u'""') swagger_dict["basePath"] = "/lame/test" register_spec(swagger_dict) resource = SwaggerClient.from_url(API_DOCS_URL).api_test assert resource.testHTTP(test_param="foo").result() is None
async def test_basePath_works(swagger_dict, http_client): swagger_dict["basePath"] = "/append" register_spec(swagger_dict) register_get("http://localhost/append/test_http?test_param=foo") client = await swagger_client(http_client) await client.api_test.testHTTP(test_param="foo").result() assert ["foo"] == HTTPretty.last_request.querystring['test_param']
def test_invalid_spec_raises_SwaggerValidationError( httprettified, swagger_dict): swagger_dict['paths']['/test_http']['get']['parameters'][0]['type'] = 'X' register_spec(swagger_dict) with pytest.raises(SwaggerValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL) assert 'is not valid' in str(excinfo.value)
def test_error_on_missing_type_in_model(httprettified, swagger_dict, sample_model): register_spec(swagger_dict) sample_model["schools"][0] = {} # Omit 'name' register_get("http://localhost/test_http", body=json.dumps(sample_model)) with pytest.raises(ValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL).api_test.testHTTP().result() assert "'name' is a required property" in str(excinfo.value)
async def test_hostname_if_passed_overrides_origin_url(swagger_dict, http_client): register_get("http://foo/test_http?", body='') swagger_dict['host'] = 'foo' register_spec(swagger_dict) client = await swagger_client(http_client) await client.api_test.testHTTP(test_param="foo").result() assert ["foo"] == HTTPretty.last_request.querystring['test_param']
def test_model_missing_required_property_in_response_raises_ValidationError( httprettified, swagger_dict, sample_model): register_spec(swagger_dict) sample_model.pop("id") register_get("http://localhost/test_http", body=simplejson.dumps(sample_model)) with pytest.raises(ValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL).api_test.testHTTP().result() assert "'id' is a required property" in str(excinfo.value)
def test_default_value_in_request(httprettified, swagger_dict): swagger_dict['paths']['/test_http']['get']['parameters'][0][ 'default'] = 'X' register_spec(swagger_dict) register_get("http://localhost/test_http?") resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP().result() assert ['X'] == httpretty.last_request().querystring['test_param']
def test_error_on_wrong_type_inside_complex_type(httprettified, swagger_dict, sample_model): register_spec(swagger_dict) sample_model["id"] = "Not Integer" register_get("http://localhost/test_http", body=json.dumps(sample_model)) with pytest.raises(ValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL).api_test.testHTTP().result() assert "'Not Integer' is not of type" in str(excinfo.value)
def test_error_on_missing_type_in_model( httprettified, swagger_dict, sample_model): register_spec(swagger_dict) sample_model["schools"][0] = {} # Omit 'name' register_get("http://localhost/test_http", body=simplejson.dumps(sample_model)) with pytest.raises(ValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL).api_test.testHTTP().result() assert "'name' is a required property" in str(excinfo.value)
def test_model_missing_required_property_in_response_raises_ValidationError( httprettified, swagger_dict, sample_model): register_spec(swagger_dict) sample_model.pop("id") register_get("http://localhost/test_http", body=json.dumps(sample_model)) with pytest.raises(ValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL).api_test.testHTTP().result() assert "'id' is a required property" in str(excinfo.value)
def test_additionalProperty_in_model_in_response(httprettified, swagger_dict, sample_model): register_spec(swagger_dict) sample_model["extra"] = 42 register_get("http://localhost/test_http", body=json.dumps(sample_model)) resource = SwaggerClient.from_url(API_DOCS_URL).api_test result = resource.testHTTP().result() assert result.extra == 42
def test_error_on_wrong_type_inside_complex_type( httprettified, swagger_dict, sample_model): register_spec(swagger_dict) sample_model["id"] = "Not Integer" register_get("http://localhost/test_http", body=simplejson.dumps(sample_model)) with pytest.raises(ValidationError) as excinfo: SwaggerClient.from_url(API_DOCS_URL).api_test.testHTTP().result() assert "'Not Integer' is not of type" in str(excinfo.value)
def test_correct_route_with_basePath_no_slash(httprettified, swagger_dict): register_get( "http://localhost/lame/test/test_http?test_param=foo", body=u'""') swagger_dict["basePath"] = "/lame/test" register_spec(swagger_dict) resource = SwaggerClient.from_url(API_DOCS_URL).api_test assert resource.testHTTP(test_param="foo").result() is None
def test_additionalProperty_in_model_in_response( httprettified, swagger_dict, sample_model): register_spec(swagger_dict) sample_model["extra"] = 42 register_get("http://localhost/test_http", body=simplejson.dumps(sample_model)) resource = SwaggerClient.from_url(API_DOCS_URL).api_test result = resource.testHTTP().result() assert result.extra == 42
async def test_invalid_type_in_response_raises_ValidationError( swagger_dict, sample_model, http_client): register_spec(swagger_dict) register_get("http://localhost/test_http", body='"NOT_COMPLEX_TYPE"') client = await swagger_client(http_client) with pytest.raises(ValidationError) as excinfo: await client.api_test.testHTTP().result() assert "'NOT_COMPLEX_TYPE' is not of type" in str(excinfo.value)
async def test_error_on_missing_type_in_model(swagger_dict, sample_model, http_client): register_spec(swagger_dict) sample_model["schools"][0] = {} # Omit 'name' register_get("http://localhost/test_http", body=json.dumps(sample_model)) client = await swagger_client(http_client) with pytest.raises(ValidationError) as excinfo: await client.api_test.testHTTP().result() assert "'name' is a required property" in str(excinfo.value)
async def test_headers_sendable_with_api_doc_request(swagger_dict, http_client): register_spec(swagger_dict) await SwaggerClient.from_url( API_DOCS_URL, http_client=http_client, request_headers={'foot': 'bart'}, ) assert 'bart' == HTTPretty.last_request.headers.get('foot')
async def test_additionalProperty_in_model_in_response(swagger_dict, sample_model, http_client): register_spec(swagger_dict) sample_model["extra"] = 42 register_get("http://localhost/test_http", body=json.dumps(sample_model)) client = await swagger_client(http_client) result = await client.api_test.testHTTP().result() assert result.extra == 42
async def test_model_missing_required_property_in_response_raises_ValidationError( swagger_dict, sample_model, http_client): register_spec(swagger_dict) sample_model.pop("id") register_get("http://localhost/test_http", body=json.dumps(sample_model)) client = await swagger_client(http_client) with pytest.raises(ValidationError) as excinfo: await client.api_test.testHTTP().result() assert "'id' is a required property" in str(excinfo.value)
async def test_default_value_not_in_request(swagger_dict, http_client): # Default should be applied on the server side so no need to send it in # the request. swagger_dict['paths']['/test_http']['get']['parameters'][0]['default'] = 'X' register_spec(swagger_dict) register_get('http://localhost/test_http?') client = await swagger_client(http_client) await client.api_test.testHTTP().result() assert 'test_param' not in HTTPretty.last_request.querystring
def test_default_value_not_in_request(httprettified, swagger_dict): # Default should be applied on the server side so no need to send it in # the request. swagger_dict['paths']['/test_http']['get']['parameters'][0]['default'] = 'X' register_spec(swagger_dict) register_get("http://localhost/test_http?") resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP().result() assert 'test_param' not in httpretty.last_request().querystring
async def test_error_on_wrong_type_inside_complex_type(swagger_dict, sample_model, http_client): register_spec(swagger_dict) sample_model["id"] = "Not Integer" register_get("http://localhost/test_http", body=json.dumps(sample_model)) client = await swagger_client(http_client) with pytest.raises(ValidationError) as excinfo: await client.api_test.testHTTP().result() assert "'Not Integer' is not of type" in str(excinfo.value)
def test_parameter_in_path_of_request(httprettified, swagger_dict): path_param_spec = {"in": "path", "name": "param_id", "type": "string"} paths_spec = swagger_dict['paths'] paths_spec['/test_http/{param_id}'] = paths_spec.pop('/test_http') paths_spec['/test_http/{param_id}']['get']['parameters'].append( path_param_spec) register_spec(swagger_dict) register_get('http://localhost/test_http/42?test_param=foo') resource = SwaggerClient.from_url(API_DOCS_URL).api_test assert resource.testHTTP(test_param="foo", param_id="42").result() is None
def test_default_value_not_in_request(httprettified, swagger_dict): # Default should be applied on the server side so no need to send it in # the request. swagger_dict['paths']['/test_http']['get']['parameters'][0][ 'default'] = 'X' register_spec(swagger_dict) register_get('http://localhost/test_http?') resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP().result() assert 'test_param' not in httpretty.last_request().querystring
async def test_array_in_response(swagger_dict, http_client): response_spec = { 'type': 'array', 'items': { 'type': 'string', }, } register_spec(swagger_dict, response_spec) expected_array = ['inky', 'dinky', 'doo'] register_test_http(body=json.dumps(expected_array)) await assert_result(expected_array, http_client)
def test_array_in_response(httprettified, swagger_dict): response_spec = { 'type': 'array', 'items': { 'type': 'string', }, } register_spec(swagger_dict, response_spec) expected_array = ['inky', 'dinky', 'doo'] register_test_http(body=simplejson.dumps(expected_array)) assert_result(expected_array)
def test_array_in_response(httprettified, swagger_dict): response_spec = { 'type': 'array', 'items': { 'type': 'string', }, } register_spec(swagger_dict, response_spec) expected_array = ['inky', 'dinky', 'doo'] register_test_http(body=json.dumps(expected_array)) assert_result(expected_array)
def test_primitive_types_returned_in_response(httprettified, swagger_dict): rtypes = { 'string': '"test"', 'integer': 42, 'number': 3.4, 'boolean': True } for rtype, rvalue in rtypes.items(): register_spec(swagger_dict, {'type': rtype}) register_test_http(body=simplejson.dumps(rvalue)) assert_result(rvalue)
def test_primitive_types_returned_in_response(httprettified, swagger_dict): rtypes = { 'string': '"test"', 'integer': 42, 'number': 3.4, 'boolean': True } for rtype, rvalue in rtypes.iteritems(): register_spec(swagger_dict, {'type': rtype}) register_test_http(body=json.dumps(rvalue)) assert_result(rvalue)
def test_invalid_primitive_types_in_response_raises_ValidationError( httprettified, swagger_dict): rtypes = { 'string': 42, 'integer': 3.4, 'number': 'foo', 'boolean': '"NOT_BOOL"' } for rtype, rvalue in rtypes.iteritems(): register_spec(swagger_dict, {'type': rtype}) register_test_http(body=json.dumps(rvalue)) assert_raises_and_matches(ValidationError, 'is not of type')
def test_500_error_raises_HTTPError(httprettified, swagger_dict): register_spec(swagger_dict) register_get('http://localhost/test_http?test_param=foo', status=500) with pytest.raises(HTTPError) as excinfo: _resource().testHTTP(test_param='foo').result() match_str = '500 Internal Server Error' assert match_str in str(excinfo.value) response_text = '{"message": "HTTPretty :)"}' assert response_text in str(excinfo.value)
def test_invalid_primitive_types_in_response_raises_ValidationError( httprettified, swagger_dict): rtypes = { 'string': 42, 'integer': 3.4, 'number': 'foo', 'boolean': '"NOT_BOOL"' } for rtype, rvalue in rtypes.items(): register_spec(swagger_dict, {'type': rtype}) register_test_http(body=simplejson.dumps(rvalue)) assert_raises_and_matches(ValidationError, 'is not of type')
def test_model_in_response(httprettified, swagger_dict, sample_model): register_spec(swagger_dict) register_get("http://localhost/test_http", body=json.dumps(sample_model)) client = SwaggerClient.from_url(API_DOCS_URL) result = client.api_test.testHTTP().result() User = client.get_model('User') School = client.get_model('School') assert isinstance(result, User) for school in result.schools: assert isinstance(school, School) assert User(id=42, schools=[School(name="School1"), School(name="School2")]) == result
def test_form_params_in_request(httprettified, swagger_dict): param1_spec = {'in': 'formData', 'name': 'param_id', 'type': 'integer'} param2_spec = {'in': 'formData', 'name': 'param_name', 'type': 'string'} path_spec = swagger_dict['paths']['/test_http'] path_spec['post'] = path_spec.pop('get') path_spec['post']['parameters'] = [param1_spec, param2_spec] register_spec(swagger_dict) httpretty.register_uri(httpretty.POST, 'http://localhost/test_http?') resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP(param_id=42, param_name='foo').result() content_type = httpretty.last_request().headers['content-type'] assert 'application/x-www-form-urlencoded' == content_type body = urlparse.parse_qs(httpretty.last_request().body) assert {b'param_name': [b'foo'], b'param_id': [b'42']} == body
def test_parameter_in_path_of_request(httprettified, swagger_dict): path_param_spec = { "in": "path", "name": "param_id", "type": "string" } paths_spec = swagger_dict['paths'] paths_spec['/test_http/{param_id}'] = paths_spec.pop('/test_http') paths_spec['/test_http/{param_id}']['get']['parameters'].append( path_param_spec) register_spec(swagger_dict) register_get('http://localhost/test_http/42?test_param=foo') resource = SwaggerClient.from_url(API_DOCS_URL).api_test assert resource.testHTTP(test_param="foo", param_id="42").result() is None
def test_parameter_in_path_of_request(httprettified, swagger_dict): path_param_spec = { 'in': 'path', 'name': 'param_id', 'required': True, 'type': 'string', } paths_spec = swagger_dict['paths'] paths_spec['/test_http/{param_id}'] = paths_spec.pop('/test_http') paths_spec['/test_http/{param_id}']['get']['parameters'].append( path_param_spec) register_spec(swagger_dict) register_get('http://localhost/test_http/42?test_param=foo') resource = SwaggerClient.from_url(API_DOCS_URL).api_test assert resource.testHTTP(test_param='foo', param_id='42').result() is None
def test_model_in_response(httprettified, swagger_dict, sample_model): register_spec(swagger_dict) register_get("http://localhost/test_http", body=json.dumps(sample_model)) client = SwaggerClient.from_url(API_DOCS_URL) result = client.api_test.testHTTP().result() User = client.get_model('User') School = client.get_model('School') assert isinstance(result, User) for school in result.schools: assert isinstance(school, School) assert User( id=42, schools=[ School(name="School1"), School(name="School2") ]) == result
def test_model_in_body_of_request(httprettified, swagger_dict, sample_model): param_spec = { "in": "body", "name": "body", "schema": { "$ref": "#/definitions/User" } } swagger_dict["paths"]["/test_http"]['post']["parameters"] = [param_spec] register_spec(swagger_dict) httpretty.register_uri(httpretty.POST, "http://localhost/test_http") client = SwaggerClient.from_url(API_DOCS_URL) resource = client.api_test User = client.get_model('User') School = client.get_model('School') user = User(id=42, schools=[School(name='s1')]) resource.testHTTPPost(body=user).result() body = simplejson.loads(httpretty.last_request().body.decode('utf-8')) assert {'schools': [{'name': 's1'}], 'id': 42} == body
def test_array_with_collection_format_in_path_of_request( httprettified, swagger_dict): path_param_spec = { 'in': 'path', 'name': 'param_ids', 'type': 'array', 'items': { 'type': 'integer' }, 'collectionFormat': 'csv', } swagger_dict['paths']['/test_http/{param_ids}'] = \ swagger_dict['paths'].pop('/test_http') swagger_dict['paths']['/test_http/{param_ids}']['get']['parameters'] = \ [path_param_spec] register_spec(swagger_dict) register_get('http://localhost/test_http/40,41,42') resource = SwaggerClient.from_url(API_DOCS_URL).api_test assert resource.testHTTP(param_ids=[40, 41, 42]).result() is None
def test_form_params_in_request(httprettified, swagger_dict): param1_spec = { "in": "formData", "name": "param_id", "type": "integer" } param2_spec = { "in": "formData", "name": "param_name", "type": "string" } path_spec = swagger_dict['paths']['/test_http'] path_spec['post'] = path_spec.pop('get') path_spec['post']['parameters'] = [param1_spec, param2_spec] register_spec(swagger_dict) httpretty.register_uri(httpretty.POST, "http://localhost/test_http?") resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP(param_id=42, param_name='foo').result() content_type = httpretty.last_request().headers['content-type'] assert 'application/x-www-form-urlencoded' == content_type body = urlparse.parse_qs(httpretty.last_request().body) assert {b'param_name': [b'foo'], b'param_id': [b'42']} == body
def test_file_upload_in_request(httprettified, swagger_dict): param1_spec = { "in": "formData", "name": "param_id", "type": "integer" } param2_spec = { "in": "formData", "name": "file_name", "type": "file" } path_spec = swagger_dict['paths']['/test_http'] path_spec['post'] = path_spec.pop('get') path_spec['post']['parameters'] = [param1_spec, param2_spec] path_spec['post']['consumes'] = ['multipart/form-data'] register_spec(swagger_dict) httpretty.register_uri(httpretty.POST, "http://localhost/test_http?") resource = SwaggerClient.from_url(API_DOCS_URL).api_test resource.testHTTP(param_id=42, file_name=cStringIO('boo')).result() content_type = httpretty.last_request().headers['content-type'] assert content_type.startswith('multipart/form-data') assert b"42" in httpretty.last_request().body assert b"boo" in httpretty.last_request().body
def test_500_error_raises_HTTPError(httprettified, swagger_dict): register_spec(swagger_dict) register_get('http://localhost/test_http?test_param=foo', status=500) assert_raises_and_matches(HTTPError, '500 Server Error')
def test_headers_sendable_with_api_doc_request(httprettified, swagger_dict): register_spec(swagger_dict) SwaggerClient.from_url(API_DOCS_URL, request_headers={'foot': 'bart'}) assert 'bart' == httpretty.last_request().headers.get('foot')
def test_unstructured_json_in_response(httprettified, swagger_dict): response_spec = {'type': 'object', 'additionalProperties': True} register_spec(swagger_dict, response_spec) register_test_http(body='{"some_foo": "bar"}') assert_result({'some_foo': 'bar'})
def test_date_format_in_reponse(httprettified, swagger_dict): response_spec = {'type': 'string', 'format': 'date'} register_spec(swagger_dict, response_spec) register_test_http(body=json.dumps("2014-06-10")) assert_result(datetime.date(2014, 6, 10))
def test_resources_are_attrs_on_client(httprettified, swagger_dict): register_spec(swagger_dict) client = SwaggerClient.from_url(API_DOCS_URL) assert isinstance(client.api_test, ResourceDecorator)