Exemplo n.º 1
0
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"
Exemplo n.º 2
0
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"
Exemplo n.º 3
0
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"
Exemplo n.º 4
0
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"
Exemplo n.º 6
0
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"
Exemplo n.º 7
0
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"
        )