def test_routes( configuration_endpoint, server_rule_factory, server_response_factory, registered_response_data, response_setter, relative_path, 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. Set universal response for all routes. 3. Make a GET-request to one of the nested endpoints. 4. Check the response. """ application = configure_application( base_endpoint=specified_endpoint, configuration_endpoint=configuration_endpoint, rule_factory=server_rule_factory, response_factory=server_response_factory, ) response_setter(application) http_response = application.test_client().get( urljoin(expected_endpoint, relative_path)) assert http_response.status_code == 200 assert http_response.data == registered_response_data, "Wrong response"
def test_route_methods( configuration_endpoint, server_rule_factory, server_response_factory, response_setter, registered_response_data, method, ): # pylint: disable=too-many-arguments """Check that all HTTP methods are allowed for the routes. 1. Configure application without specifying base endpoint. 2. Set universal response for all routes. 3. Make a request of the specified type to the base url. 4. Check the response. """ application = configure_application( configuration_endpoint=configuration_endpoint, rule_factory=server_rule_factory, response_factory=server_response_factory, ) response_setter(application) http_response = application.test_client().open(DEFAULT_BASE_ENDPOINT, method=method) assert http_response.status_code == 200 # Response for HEAD-request does not contain a body assert http_response.data == registered_response_data or method == "HEAD", "Wrong response"
def test_rule_factory(server_response_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, response_factory=server_response_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_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 _application_factory( base_endpoint=base_endpoint, configuration_endpoint=configuration_endpoint, rule_factory=server_rule_factory, response_factory=server_response_factory, ): return configure_application( base_endpoint=base_endpoint, configuration_endpoint=configuration_endpoint, rule_factory=rule_factory, response_factory=response_factory, )
def test_configuration_endpoint( server_rule_factory, server_response_factory, registered_match_all_rule, registered_response_data, registered_success_response, specified_endpoint, expected_endpoint, ): # pylint: disable=too-many-arguments """Test that specified configuration endpoint is used. 1. Configure application with specifying configuration endpoint. 2. Make a POST request to create a rule (RulesManager resource). 3. Check the status of the response. 4. Make a GET request to get the rule (Rule resource). 5. Check the response. 6. Make a POST request to set a response for the rule (Response resource). 7. Check the response. 8. Make a request to trigger the configured rule and response. 9. Check the response. """ application = configure_application( configuration_endpoint=specified_endpoint, rule_factory=server_rule_factory, response_factory=server_response_factory, ) client = application.test_client() serialized_rule = server_rule_factory.serialize_rule( rule=registered_match_all_rule) http_response = client.post(urljoin(expected_endpoint, "rules"), json=serialized_rule) assert http_response.status_code == 200, "Can't create a rule" rule_id = http_response.json["data"]["rule_id"] http_response = client.get( urljoin(expected_endpoint, "rule/{0}".format(rule_id))) assert http_response.status_code == 200, "Can't obtain the rule" assert http_response.json["data"]["rule_id"] == rule_id, "Wrong rule" serialized_response = server_response_factory.serialize_response( registered_success_response) http_response = client.post( urljoin(expected_endpoint, "response/{0}".format(rule_id)), json=serialized_response, ) assert http_response.status_code == 200, "Can't set the response" assert client.get(DEFAULT_BASE_ENDPOINT ).data == registered_response_data, "Wrong response"
def test_unmanaged_routes( configuration_endpoint, server_rule_factory, server_response_factory, response_setter, ): """Check that only paths nested to the default base endpoint are managed. 1. Configure application without specifying base endpoint. 2. Set universal response for all routes. 3. Make a request to the root. 4. Check that 404 status is returned. """ application = configure_application( configuration_endpoint=configuration_endpoint, rule_factory=server_rule_factory, response_factory=server_response_factory, ) response_setter(application) assert application.test_client().get( "/").status_code == 404, "Wrong status code"