def register_urls(self): httpretty.register_uri( httpretty.GET, "http://localhost/api-docs", body=json.dumps( {"swaggerVersion": "1.2", "apis": [{"path": "/api_test"}]})) httpretty.register_uri( httpretty.GET, "http://localhost/api-docs/api_test", body=json.dumps(self.response))
def test_error_if_response_is_null_and_allow_null_not_given(self): self.register_urls() httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(None)) resource = SwaggerClient.from_url(u'http://localhost/api-docs').api_test self.assertRaises(TypeError, resource.testHTTP().result)
def test_alllow_null_as_response_if_allow_null_is_given(self): self.register_urls() httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(None)) resource = SwaggerClient.from_url(u'http://localhost/api-docs').api_test resource.testHTTP().result(allow_null=True)
def add_param_to_req(param, value, request): """Populates request object with the request parameters :param param: swagger spec details of a param :type param: dict :param value: value for the param given in the API call :param request: request object to be populated """ pname = param['name'] type_ = swagger_type.get_swagger_type(param) param_req_type = param['paramType'] if param_req_type == u'path': request['url'] = request['url'].replace( u'{%s}' % pname, urllib.quote(unicode(value))) elif param_req_type == u'query': request['params'][pname] = value elif param_req_type == u'body': if not swagger_type.is_primitive(type_): # If not primitive, body has to be 'dict' # (or has already been converted to dict from model) request['headers']['content-type'] = APP_JSON request['data'] = json.dumps(value) else: request['data'] = stringify_body(value) elif param_req_type == 'form': handle_form_param(pname, value, type_, request) # TODO(#31): accept 'header', in paramType else: raise AssertionError( u"Unsupported Parameter type: %s" % param_req_type)
def test_error_on_missing_type_inside_nested_complex_type_1(self): self.register_urls() self.sample_model["schools"][0] = {} # Omit 'name' httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(self.sample_model)) self.assertRaises(AssertionError, SwaggerClient.from_url( u'http://localhost/api-docs').api_test.testHTTP().result)
def test_error_on_wrong_type_inside_complex_type(self): self.register_urls() self.sample_model["id"] = "Not Integer" httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(self.sample_model)) self.assertRaises(TypeError, SwaggerClient.from_url( u'http://localhost/api-docs').api_test.testHTTP().result)
def test_error_on_missing_required_type_instead_of_complex_type(self): self.register_urls() self.sample_model.pop("id") httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(self.sample_model)) self.assertRaises(AssertionError, SwaggerClient.from_url( u'http://localhost/api-docs').api_test.testHTTP().result)
def test_success_on_extra_field_in_complex_type(self): self.register_urls() self.sample_model["extra"] = 42 httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(self.sample_model)) result = SwaggerClient.from_url( u'http://localhost/api-docs').api_test.testHTTP().result() self.assertEqual(result._raw["extra"], 42)
def test_alllow_null_in_response_body_if_allow_null_is_given(self): self.register_urls() self.sample_model["schools"].append(None) httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(self.sample_model)) resource = SwaggerClient.from_url(u'http://localhost/api-docs').api_test resp = resource.testHTTP().result(allow_null=True) self.assertTrue(isinstance(resp, resource.testHTTP._models['User']))
def test_remove_null_values_when_convert_to_flat_dict(self): self.register_urls() httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(self.sample_model)) resource = SwaggerClient.from_url(u'http://localhost/api-docs').api_test models = resource.testHTTP._models User = models['User'] School = models['School'] user = User(schools=[School(name='a'), None]) self.assertEqual(None, user.schools[1]) self.assertEqual([{'name': 'a'}], user._flat_dict()['schools'])
def test_success_on_complex_operation_response_type(self): self.register_urls() httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(self.sample_model)) resource = SwaggerClient.from_url(u'http://localhost/api-docs').api_test resp = resource.testHTTP().result() models = resource.testHTTP._models User = models['User'] School = models['School'] self.assertTrue(isinstance(resp, User)) [self.assertTrue(isinstance(x, School)) for x in resp.schools] self.assertEqual(User(id=42, schools=[School( name="School1"), School(name="School2")]), resp)
def test_content_type_not_present_if_only_primitive_type_in_body(self): query_parameter = { "paramType": "body", "name": "body", "type": "integer", } self.response["apis"][1]["operations"][0]["parameters"] = [ query_parameter] self.register_urls() httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps({'id': 1})) client = SwaggerClient.from_url(u'http://localhost/api-docs').api_test client.testHTTPPost(body=42).result() self.assertFalse('content-type' in httpretty.last_request().headers)
def test_content_type_as_json_if_complex_type_in_body(self): query_parameter = { "paramType": "body", "name": "body", "type": "School", } school = {"name": "temp"} self.response["apis"][1]["operations"][0]["type"] = "School" self.response["apis"][1]["operations"][0]["parameters"] = [ query_parameter] self.register_urls() httpretty.register_uri( httpretty.GET, "http://localhost/test_http", body=json.dumps(school)) resource = SwaggerClient.from_url(u'http://localhost/api-docs').api_test resource.testHTTPPost(body=school).result() self.assertEqual("application/json", httpretty.last_request().headers[ 'content-type'])
def test_removal_of_none_attributes_from_param_body_model(self): query_parameter = { "paramType": "body", "name": "body", "type": "User", } self.response["apis"][1]["operations"][0]["parameters"] = [ query_parameter] self.response["models"]["User"]["properties"]["school"] = { "$ref": "School"} self.register_urls() resource = SwaggerClient.from_url(u'http://localhost/api-docs').api_test user = resource.testHTTP._models['User'](id=42) future = resource.testHTTPPost(body=user) # Removed the 'school': None - key, value pair from dict self.assertEqual( json.dumps({'id': 42, 'schools': []}), future._request.request.data, )
def test_success_model_in_param_body_converts_to_dict(self): query_parameter = { "paramType": "body", "name": "body", "type": "User", } self.response["apis"][1]["operations"][0]["parameters"] = [ query_parameter] self.register_urls() resource = SwaggerClient.from_url(u'http://localhost/api-docs').api_test models = resource.testHTTP._models User = models['User'] School = models['School'] # Also test all None items are removed from array list user = User(id=42, schools=[School(name='s1'), None]) future = resource.testHTTPPost(body=user) self.assertEqual( json.dumps({'id': 42, 'schools': [{'name': 's1'}]}), future._request.request.data, )
def register_urls(self): httpretty.register_uri(httpretty.GET, "http://localhost/api-docs", body=json.dumps(self.response))
def stringify_body(value): """Json dump the value to string if not already in string """ if not value or isinstance(value, basestring): return value return json.dumps(value)