def test_serialize_response(): """Check that registered serializer is used by response factory. 1. Create response factory. 2. Register new response type. 3. Serialize a response. 4. Check that the response is successfully serialized. """ response_factory = ResponseFactory() def _serializer(response_type, response): return response_type, response response_type = "Test" response_factory.register_response( response_type=response_type, parser=None, serializer=_serializer, ) response = ResponseClass(response_type=response_type, parameters={}) expected_serialized_response = { "response_type": response_type, "parameters": ( response_type, response, ), } serialized_response = response_factory.serialize_response(response=response) assert serialized_response == expected_serialized_response, "Response has not been serialized"
def test_response_factory( server_rule_factory, registered_match_all_rule, server_response_prototype, ): """Check that custom response factory is used when specified. 1. Create a response factory. 2. Register a new response type. 3. Configure application with the created response factory. 4. Create a univeral rule to match every request. 5. Make a POST-request to set a response for the rule. 6. Check that response is successful. 7. Make a request to the base url. 8. Check the response. """ response_factory = ResponseFactory() application = configure_application( rule_factory=server_rule_factory, response_factory=response_factory, ) client = application.test_client() serialized_rule = server_rule_factory.serialize_rule( registered_match_all_rule) http_response = client.post( urljoin(DEFAULT_CONFIGURATION_ENDPOINT, "rules"), json=serialized_rule, ) rule_id = http_response.json["data"]["rule_id"] response_type = "".join( random.choice(string.ascii_uppercase) for _ in range(10)) response = server_response_prototype.create_new( response_type=response_type, builder_implementation=b"body", ) response_factory.register_response( response_type=response_type, parser=lambda *args, **kwargs: response, serializer=lambda *args, **kwargs: {}, ) serialized_response = response_factory.serialize_response( response=response) http_response = client.post( urljoin(DEFAULT_CONFIGURATION_ENDPOINT, "response/{0}".format(rule_id)), json=serialized_response, ) assert http_response.status_code == 200, "Can't set a response" assert client.put(DEFAULT_BASE_ENDPOINT).data == b"body"
def test_response_factory( client_rule_factory, client_response_factory, client_method_rule_class, server_response_prototype, ): """Check that custom response factory is used when specified. 1. Create a response factory. 2. Register a new response type. 3. Configure application with the created response factory. 4. Create a flask client. 5. Create a PUT-rule with the client. 6. Make a POST-request to set a response for the rule. 7. Check that response is successful. 8. Make a request to the base url. 9. Check the response. """ response_factory = ResponseFactory() application = configure_application(response_factory=response_factory) application_client = application.test_client() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application_client, ) rule_id = client.create_rule(client_method_rule_class(method="PUT")).rule_id response_type = "".join(random.choice(string.ascii_uppercase) for _ in range(10)) response = server_response_prototype.create_new( response_type=response_type, builder_implementation=b"body", ) response_factory.register_response( response_type=response_type, parser=lambda *args, **kwargs: response, serializer=lambda *args, **kwargs: {}, ) serialized_response = response_factory.serialize_response(response=response) http_response = application_client.post( urljoin(DEFAULT_CONFIGURATION_ENDPOINT, "response/{0}".format(rule_id)), json=serialized_response, ) assert http_response.status_code == 200, "Can't set a response" assert application_client.put(DEFAULT_BASE_ENDPOINT).data == b"body"
def create_response_factory(): """Create and prepare response factory. :returns: instance of :class:`ResponseFactory <looseserver.common.response.ResponseFactory>`. """ response_factory = ResponseFactory() server_factory_preparator = ResponseFactoryPreparator(response_factory=response_factory) server_factory_preparator.prepare_fixed_response(fixed_response_class=FixedResponse) return response_factory
def test_serialize_response_with_unknown_error(): """Check that ResponseError is raised if unhandled exception is raised by a serializer. 1. Create response factory. 2. Register a new response type with a serialier, raising an error different from ResponseSerializeError. 3. Try to serialize a response. 4. Check that ResponseError is raised. """ response_factory = ResponseFactory() error = ValueError("TestError") def _serializer(response_type, response): # pylint: disable=unused-argument raise error response_type = "Test" response_factory.register_response( response_type=response_type, parser=None, serializer=_serializer, ) response = ResponseClass(response_type=response_type, parameters={}) with pytest.raises(ResponseError) as exception_info: response_factory.serialize_response(response=response) actual_error = exception_info.value assert not isinstance(actual_error, ResponseSerializeError), "Wrong type of the error" assert actual_error.args[0] == "Failed to serialize a response", "Wrong error message" assert actual_error.__cause__ is error, "Wrong reason"
def test_serialize_response_with_error(): """Check that ResponseSerializeError is propagated from serializers. 1. Create response factory. 2. Register a new response type with a serializer, raising ResponseSerializeError 3. Try to serialize a response. 4. Check that ResponseSerializeError is raised. """ response_factory = ResponseFactory() error = ResponseSerializeError("TestError") def _serializer(response_type, response): # pylint: disable=unused-argument raise error response_type = "Test" response_factory.register_response( response_type=response_type, parser=None, serializer=_serializer, ) response = ResponseClass(response_type=response_type, parameters={}) with pytest.raises(ResponseSerializeError) as exception_info: response_factory.serialize_response(response=response) assert exception_info.value is error, "Wrong error is raised"
def test_parse_response_with_error(): """Check that ResponseParseError is propagated from parsers. 1. Create response factory. 2. Register a new response type with a parser, raising ResponseParseError. 3. Try to parse a response. 4. Check that ResponseParseError is raised. """ response_factory = ResponseFactory() error = ResponseParseError("TestError") def _parser(response_type, response_data): # pylint: disable=unused-argument raise error response_type = "Test" response_factory.register_response(response_type=response_type, parser=_parser, serializer=None) response_data = { "response_type": response_type, "parameters": {}, } with pytest.raises(ResponseParseError) as exception_info: response_factory.parse_response(data=response_data) assert exception_info.value is error, "Wrong error is raised"
def test_parse_response(): """Check that registered parser is used by response factory. 1. Create response factory. 2. Register new response type. 3. Parse a response. 4. Check that the response is successfully parsed. """ response_factory = ResponseFactory() parser = lambda response_type, response_data: (response_type, response_data) response_type = "Test" response_factory.register_response(response_type=response_type, parser=parser, serializer=None) response_parameters = { "key": "value", } response_data = { "response_type": response_type, "parameters": response_parameters, } parsed_response = response_factory.parse_response(data=response_data) assert parsed_response == (response_type, response_parameters), "Response has not been parsed"
def test_parse_wrong_data_type(): """Check that ResponseParseError is raised on attempt to parse wrong data type. 1. Create response factory. 2. Register new response type. 3. Try to parse a response from an object that without __getitem__. 4. Check that ResponseParseError is raised. 5. Check the message of the error. """ response_factory = ResponseFactory() parser = lambda *args, **kwargs: None response_factory.register_response(response_type="Test", parser=parser, serializer=None) with pytest.raises(ResponseParseError) as exception_info: response_factory.parse_response(data=None) assert exception_info.value.args[0] == "Failed to parse response type. Wrong data format", ( "Wrong error message" )
def test_parse_data_without_type(): # pylint: disable=line-too-long """Check that ResponseParseError is raised on attempt to parse dictionary without "response_type" key. 1. Create response factory. 2. Register new response type. 3. Try to parse a response from a dictionary without "response_type" key. 4. Check that ResponseParseError is raised. 5. Check the message of the error. """ # pylint: enable=line-too-long response_factory = ResponseFactory() parser = lambda *args, **kwargs: None response_factory.register_response(response_type="Test", parser=parser, serializer=None) with pytest.raises(ResponseParseError) as exception_info: response_factory.parse_response(data={"parameters": {"key": "value"}}) assert exception_info.value.args[0] == "Failed to parse response. Type is not specified", ( "Wrong error message" )
def test_serialize_unknown_type(): """Check that ResponseSerializeError is raised on attempt to serialize response of unknown type. 1. Create response factory. 2. Register new response type. 3. Try to serialize a response with unregistered type. 4. Check that ResponseSerializeError is raised. 5. Check the message of the error. """ response_factory = ResponseFactory() serializer = lambda *args, **kwargs: None response_factory.register_response(response_type="Test", parser=None, serializer=serializer) response = ResponseClass(response_type="NonExistentType", parameters={}) with pytest.raises(ResponseSerializeError) as exception_info: response_factory.serialize_response(response=response) expected_message = "Failed to serialize response {0}. Unknown type 'NonExistentType'".format( response, ) assert exception_info.value.args[0] == expected_message, "Wrong error message"
def test_serialize_wrong_object(): # pylint: disable=line-too-long """Check that ResponseSerializeError is raised on attempt to serialize object without response_type. 1. Create response factory. 2. Register new response type. 3. Try to serialize an object without response_type attribute. 4. Check that ResponseSerializeError is raised. 5. Check the message of the error. """ # pylint: enable=line-too-long response_factory = ResponseFactory() serializer = lambda *args, **kwargs: None response_factory.register_response(response_type="Test", parser=None, serializer=serializer) with pytest.raises(ResponseSerializeError) as exception_info: response_factory.serialize_response(response=None) assert exception_info.value.args[0] == "Failed to obtain type of the response", ( "Wrong error message" )
def test_parse_unknown_type(): """Check that ResponseParseError is raised on attempt to parse response of unknown type. 1. Create response factory. 2. Register new response type. 3. Try to parse a response with unregistered type. 4. Check that ResponseParseError is raised. 5. Check the message of the error. """ response_factory = ResponseFactory() parser = lambda *args, **kwargs: None response_factory.register_response(response_type="Test", parser=parser, serializer=None) response_data = { "response_type": "NonExistentType", "parameters": {}, } with pytest.raises(ResponseParseError) as exception_info: response_factory.parse_response(data=response_data) error_message = "Failed to parse response. Unknown type 'NonExistentType'" assert exception_info.value.args[0] == error_message, "Wrong error message"
def client_response_factory(): """Custom response factory for client.""" return ResponseFactory()
def server_response_factory(): """Custom response factory for server.""" return ResponseFactory()