def test_rule_factory(server_rule_prototype): """Check that custom rule factory is used when specified. 1. Create rule factory. 2. Register a new rule type. 3. Configure application with the created rule factory. 4. Make a POST-request to create a method rule for PUT requests. 5. Check that response is successful and contains rule ID. """ rule_factory = RuleFactory() application = configure_application(rule_factory=rule_factory) rule_type = "".join( random.choice(string.ascii_uppercase) for _ in range(10)) rule = server_rule_prototype.create_new(rule_type=rule_type) rule_factory.register_rule( rule_type=rule_type, parser=lambda *args, **kwargs: rule, serializer=lambda *args, **kwargs: {}, ) serialized_rule = rule_factory.serialize_rule(rule=rule) http_response = application.test_client().post( urljoin(DEFAULT_CONFIGURATION_ENDPOINT, "rules"), json=serialized_rule, ) assert http_response.status_code == 200, "Can't create a rule" assert http_response.json["data"][ "rule_id"] is not None, "Response does not contain rule ID"
def test_default_response_factory( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, ): """Check that default response factory is used if custom one is not specified. 1. Configure application without specifying response factory. 2. Create a flask client. 3. Create a GET-rule and set a response for it. 4. Make a request to the base url. 5. Check the response. """ application = configure_application() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule_id = client.create_rule(client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get(DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
def test_default_factories(): """Check that default factories are used if None was specified. 1. Create default application. 2. Create a flask client without specifying rule factory. 3. Create a method rule with the client. 4. Set a fixed response with the client. 5. Check the response. """ application = configure_application() application_client = application.test_client() client = FlaskClient( configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule = client.create_rule(rule=MethodRule(method="GET")) assert rule.rule_id is not None, "Rule was not created" client.set_response(rule_id=rule.rule_id, response=FixedResponse(status=200)) assert application_client.get( DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
def test_default_configuration_endpoint( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, ): """Test the default configuration endpoint of the application. 1. Configure application without specifying configuration endpoint. 2. Create a flask client with default configuration endpoint. 3. Create a rule with the client (RulesManager resource). 4. Get the rule with the client (Rule resource). 5. Set a response for the rule with the client (Response resource). 6. Make a request to trigger the configured rule and response. 7. Check the response. """ application = configure_application() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule_id = client.create_rule( client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get( DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
def test_default_routes( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, ): """Check that rules are applied for all nested routes of the default base endpoint. 1. Configure application without specifying base endpoint. 2. Create a flask client. 3. Create a GET-rule and set a response for it. 4. Make a GET-request to the default endpoint. 5. Check the response. """ application = configure_application() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule_id = client.create_rule( client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get( DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
def _run(commandline_arguments=None): """Entrypoint to run a loose server.""" if __name__ == "__main__": parser = create_parser() arguments = parser.parse_args(commandline_arguments) application = configure_application( base_endpoint=arguments.base_endpoint, configuration_endpoint=arguments.configuration_endpoint, ) application.run(host=arguments.host, port=arguments.port)
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 test_default_rule_factory( client_rule_factory, client_response_factory, client_method_rule_class, ): """Check that default rule factory is used if custom one is not specified. 1. Configure application without specifying rule factory. 2. Create a flask client. 3. Create a method rule for PUT requests with clients. 4. Check that rule is created and contains rule ID. """ application = configure_application() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule = client.create_rule(client_method_rule_class(method="GET")) assert rule.rule_id is not None, "Rule was not created"
def test_routes( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, specified_endpoint, expected_endpoint, ): # pylint: disable=too-many-arguments """Check that rules are applied for all nested routes of the specified base endpoint. 1. Configure application with specifying base endpoint. 2. Create a flask client. 3. Create a GET-rule and set a response for it. 4. Make a GET-request to the default endpoint. 5. Check the response. """ server_rule_factory = create_rule_factory(base_url=expected_endpoint) application = configure_application( base_endpoint=specified_endpoint, rule_factory=server_rule_factory, ) client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule_id = client.create_rule( client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get( expected_endpoint).status_code == 200, "Wrong status"
def test_configuration_endpoint( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, specified_endpoint, expected_endpoint, ): # pylint: disable=too-many-arguments """Test that specified configuration endpoint is used. 1. Configure application with specifying configuration endpoint. 2. Create a flask client with expected configuration endpoint. 3. Create a rule with the client (RulesManager resource). 4. Get the rule with the client (Rule resource). 5. Set a response for the rule with the client (Response resource). 6. Make a request to trigger the configured rule and response. 7. Check the response. """ application = configure_application( configuration_endpoint=specified_endpoint) client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=expected_endpoint, application_client=application.test_client(), ) rule_id = client.create_rule( client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get( DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
def default_factories_application(base_endpoint, configuration_endpoint): """Configured application with default factories.""" return configure_application( base_endpoint=base_endpoint, configuration_endpoint=configuration_endpoint, )
def test_custom_factories( server_rule_factory, server_response_factory, client_rule_factory, client_response_factory, server_rule_prototype, server_response_prototype, ): # pylint: disable=too-many-arguments """Check that custom factories are used if specified. 1. Register new rule in the server and client factories. 2. Register new response in the server and client factories. 3. Create an application with server factories. 4. Create a flask client with server factories. 5. Create a rule with the client. 6. Set a response with the client. 7. Check the response. """ serializer = lambda *args, **kwargs: None rule_type = "".join( random.choice(string.ascii_uppercase) for _ in range(5)) server_rule = server_rule_prototype.create_new(rule_type=rule_type, match_implementation=True) server_rule_factory.register_rule( rule_type=rule_type, parser=lambda *args, **kwargs: server_rule, serializer=serializer, ) client_rule_factory.register_rule( rule_type=rule_type, parser=lambda *args, **kwargs: ClientRule(rule_type=rule_type), serializer=serializer, ) response_type = "".join( random.choice(string.ascii_uppercase) for _ in range(5)) server_response = server_response_prototype.create_new( response_type=response_type, builder_implementation="", ) server_response_factory.register_response( response_type=response_type, parser=lambda *args, **kwargs: server_response, serializer=serializer, ) client_response_factory.register_response( response_type=response_type, parser=lambda *args, **kwargs: ClientResponse(response_type= response_type), serializer=serializer, ) application = configure_application( rule_factory=server_rule_factory, response_factory=server_response_factory, ) application_client = application.test_client() client = FlaskClient( configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), rule_factory=client_rule_factory, response_factory=client_response_factory, ) rule = client.create_rule(rule=ClientRule(rule_type=rule_type)) assert rule.rule_id is not None, "Rule was not created" client.set_response(rule_id=rule.rule_id, response=ClientResponse(response_type=response_type)) assert application_client.get( DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"