def wrapper_decorator(*args, **kwargs):
        response_headers = ResponseHeaders.merge(
            ResponseHeaders.allow_all_cors_headers(),
            ResponseHeaders.json_headers()
        )

        try:
            return func(*args, **kwargs)
        except HttpException as ex:
            logger.exception('HTTP exception encountered.')

            data = ex.data()

            return Response.json(
                http_status=ex.http_code(),
                headers=response_headers,
                body=data
            )
        except Exception as ex:
            logger.exception('Unexpected exception encountered.')

            error = InternalError(str(ex))

            return Response.json(
                http_status=error.http_code(),
                headers=response_headers,
                body=error.data()
            )
def test_FUNC_merge_WITH_multiple_headers_EXPECT_headers_merged() -> None:
    """
    Test that the function can merge headers.

    :return: No return.
    """
    headers = ResponseHeaders.merge(ResponseHeaders({'key1': 'value'}),
                                    ResponseHeaders({'key2': 'value'}),
                                    ResponseHeaders({'key3': 'value'}))

    assert headers.headers_dict == {
        'key1': 'value',
        'key2': 'value',
        'key3': 'value'
    }
def test_FUNC_mpeg_headers_WITH_nothing_EXPECT_headers_returned() -> None:
    """
    Test that the function returns appropriate headers.

    :return: No return.
    """
    headers = ResponseHeaders.mpeg_headers().headers_dict

    assert headers == {'Content-Type': 'audio/mpeg'}
def test_FUNC_headers_dict_WITH_dummy_headers_EXPECT_same_dummy_headers(
) -> None:
    """
    Test that the function returns same headers.

    :return: No return.
    """
    headers = ResponseHeaders({'key': 'value'})
    assert headers.headers_dict == {'key': 'value'}
def test_FUNC_allow_all_cors_headers_WITH_nothing_EXPECT_headers_returned(
) -> None:
    """
    Test that the function returns appropriate headers.

    :return: No return.
    """
    headers = ResponseHeaders.allow_all_cors_headers().headers_dict

    assert headers == {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Credentials': True
    }
Exemplo n.º 6
0
def test_FUNC_media_WITH_encoded_file_EXPECT_dictionary_returned() -> None:
    """
    Test that the function will return a byte64 encoded file.

    :return: No return.
    """

    test_value = base64.b64encode(b'test_value')

    response = Response.media(200, ResponseHeaders.wav_headers(), test_value)
    assert response == {
        'body': test_value,
        'isBase64Encoded': True,
        'statusCode': 200,
        'headers': {
            'Content-Type': 'audio/wav'
        }
    }
Exemplo n.º 7
0
    test_value = base64.b64encode(b'test_value')

    response = Response.media(200, ResponseHeaders.wav_headers(), test_value)
    assert response == {
        'body': test_value,
        'isBase64Encoded': True,
        'statusCode': 200,
        'headers': {
            'Content-Type': 'audio/wav'
        }
    }


@mark.parametrize("data,headers,encoded", [
    ('a string', ResponseHeaders.text_headers(), False),
    ('a string', ResponseHeaders.text_headers(), True),
    ('<p>a html paragraph</p>', ResponseHeaders.html_headers(), False),
    ('<p>a html paragraph</p>', ResponseHeaders.html_headers(), True),
])
def test_FUNC_any_WITH_any_data_EXPECT_dictionary_returned(
        data: Any, headers: ResponseHeaders, encoded: bool) -> None:
    """
    Test that the function will return any data with according headers.

    :return: No return.
    """
    response = Response.any(200, headers, data, encoded)
    assert response == {
        'body': data,
        'isBase64Encoded': encoded,