Example #1
0
def test_headers(
    base_endpoint,
    default_factories_application,
    client_response_factory,
    configured_flask_client,
    existing_get_rule,
    headers,
):
    # pylint: disable=too-many-arguments
    """Check that response headers can be set.

    1. Prepare a fixed response in the response factory.
    2. Set a fixed response with specified headers for an existing rule.
    3. Check the headers of the response.
    """
    preparator = ResponseFactoryPreparator(client_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    fixed_response = FixedResponse(headers=headers)
    configured_flask_client.set_response(rule_id=existing_get_rule.rule_id,
                                         response=fixed_response)

    http_response = default_factories_application.test_client().get(
        base_endpoint)
    assert set(http_response.headers.items()).issuperset(
        headers.items()), ("Headers were not set")
Example #2
0
def test_status(
    base_endpoint,
    default_factories_application,
    client_response_factory,
    configured_flask_client,
    existing_get_rule,
    status,
):
    # pylint: disable=too-many-arguments
    """Check that response status can be set.

    1. Prepare a fixed response in the response factory.
    2. Set a fixed response with specified status for an existing rule.
    3. Check the status of the response.
    """
    preparator = ResponseFactoryPreparator(client_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    fixed_response = FixedResponse(status=status)
    configured_flask_client.set_response(rule_id=existing_get_rule.rule_id,
                                         response=fixed_response)

    http_response = default_factories_application.test_client().get(
        base_endpoint)
    assert http_response.status_code == status, "Wrong status code"
Example #3
0
def test_bytes_body(
    base_endpoint,
    default_factories_application,
    client_response_factory,
    configured_flask_client,
    existing_get_rule,
    unicode_body,
    encoding,
):
    # pylint: disable=too-many-arguments
    """Check that response body can be set as a bytes object.

    1. Prepare a fixed response in the response factory.
    2. Set a fixed response with specified bytes body for an existing rule.
    3. Check the body of the response.
    """
    preparator = ResponseFactoryPreparator(client_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    body = unicode_body.encode(encoding)
    headers = {"Content-Type": "text/plain; charset={0}".format(encoding)}

    fixed_response = FixedResponse(headers=headers, body=body)
    configured_flask_client.set_response(rule_id=existing_get_rule.rule_id,
                                         response=fixed_response)

    http_response = default_factories_application.test_client().get(
        base_endpoint)
    assert http_response.data.decode(encoding) == unicode_body, "Wrong body"
Example #4
0
def test_string_body(
    base_endpoint,
    default_factories_application,
    client_response_factory,
    configured_flask_client,
    existing_get_rule,
    body,
):
    # pylint: disable=too-many-arguments
    """Check that response body can be set as a string.

    1. Prepare a fixed response in the response factory.
    2. Set a fixed response with specified string body for an existing rule.
    3. Check the body of the response.
    """
    preparator = ResponseFactoryPreparator(client_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    fixed_response = FixedResponse(body=body)
    configured_flask_client.set_response(rule_id=existing_get_rule.rule_id,
                                         response=fixed_response)

    http_response = default_factories_application.test_client().get(
        base_endpoint)
    assert http_response.data == body.encode("utf8"), "Wrong body"
Example #5
0
def test_application(flask_run_parameters, base_endpoint, configuration_endpoint):
    """Check that application is configured with specified endpoints.

    1. Run application with specified base endpoint and configuration endpoint.
    2. Create a flask client with the specified configuration endpoint.
    3. Create a method rule and set a successful response for it.
    4. Make a GET-request for the specified based endpoint.
    5. Check the response.
    """
    commandline_arguments = [
        "--base-endpoint", base_endpoint,
        "--configuration-endpoint", configuration_endpoint,
        ]
    run_module._run(commandline_arguments)  # pylint: disable=protected-access

    application = flask_run_parameters["self"]
    application_client = application.test_client()

    client = FlaskClient(
        configuration_url=configuration_endpoint,
        application_client=application_client,
        )

    rule = client.create_rule(rule=MethodRule(method="GET"))
    client.set_response(rule_id=rule.rule_id, response=FixedResponse(status=200))

    assert application_client.get(base_endpoint).status_code == 200, "Wrong status"
Example #6
0
def test_create_response_factory(
    base_endpoint,
    configuration_endpoint,
    default_factories_application,
):
    """Check that default responses are registered in the default response factory.

    1. Configure application with default factories.
    2. Create default response factory for client.
    3. Create a method rule with the client.
    4. Set a fixed response with the client.
    4. Check that response is successful.
    """
    application_client = default_factories_application.test_client()

    client = FlaskClient(
        configuration_url=configuration_endpoint,
        rule_factory=create_rule_factory(),
        response_factory=create_response_factory(),
        application_client=application_client,
    )

    rule = client.create_rule(rule=MethodRule(method="GET"))

    fixed_response = FixedResponse(status=200)
    client.set_response(rule_id=rule.rule_id, response=fixed_response)

    assert application_client.get(
        base_endpoint).status_code == fixed_response.status, (
            "Response was not set")
Example #7
0
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"
Example #8
0
def test_default_response_type():
    """Check the default response type of the fixed response.

    1. Create a fixed response without specifying its type.
    2. Check the response type.
    """
    response = FixedResponse()
    assert response.response_type == ResponseType.FIXED.name, "Wrong response type"
Example #9
0
def test_default_parameters():
    """Check the default values of the response.

    1. Create a fixed response without specifying anything.
    2. Check status.
    3. Check headers.
    4. Check body.
    """
    response = FixedResponse()
    assert response.status == 200, "Wrong status"
    assert response.headers == {}, "Wrong headers"
    assert response.body == "", "Wrong body"
Example #10
0
def test_response_representation():
    """Check the representation of the fixed response.

    1. Create a fixed response.
    2. Check result of the repr function.
    """
    status = 200
    headers = {"key": "value"}
    body = "body"
    response = FixedResponse(
        response_type=ResponseType.FIXED.name,
        status=status,
        headers=headers,
        body=body,
    )
    assert repr(response) == "FixedResponse()", "Wrong representation"