コード例 #1
0
def test_handler_api_gateway_base_path(mock_wsgi_app_file, mock_app, event,
                                       wsgi_handler):
    event["headers"]["Host"] = "custom.domain.com"
    event["path"] = "/prod/some/path"
    try:
        os.environ["API_GATEWAY_BASE_PATH"] = "prod"
        wsgi_handler.handler(event, {})
    finally:
        del os.environ["API_GATEWAY_BASE_PATH"]

    assert wsgi_handler.wsgi_app.last_environ == {
        "CONTENT_LENGTH": "0",
        "CONTENT_TYPE": "",
        "HTTP_ACCEPT": "*/*",
        "HTTP_ACCEPT_ENCODING": "gzip, deflate",
        "HTTP_CACHE_CONTROL": "no-cache",
        "HTTP_CLOUDFRONT_FORWARDED_PROTO": "https",
        "HTTP_CLOUDFRONT_IS_DESKTOP_VIEWER": "true",
        "HTTP_CLOUDFRONT_IS_MOBILE_VIEWER": "false",
        "HTTP_CLOUDFRONT_IS_SMARTTV_VIEWER": "false",
        "HTTP_CLOUDFRONT_IS_TABLET_VIEWER": "false",
        "HTTP_CLOUDFRONT_VIEWER_COUNTRY": "DK",
        "HTTP_COOKIE":
        "CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001",
        "HTTP_HOST": "custom.domain.com",
        "HTTP_POSTMAN_TOKEN": "778a706e-d6b0-48d5-94dd-9e98c22f12fe",
        "HTTP_USER_AGENT": "PostmanRuntime/3.0.11-hotfix.2",
        "HTTP_VIA": "1.1 b8fa.cloudfront.net (CloudFront)",
        "HTTP_X_AMZN_TRACE_ID": "Root=1-58d534a5-1e7cffe644b086304dce7a1e",
        "HTTP_X_AMZ_CF_ID": "jx0Bvz9rm--Mz3wAj4i46FdOQQK3RHF4H0moJjBsQ==",
        "HTTP_X_FORWARDED_FOR": "76.20.166.147, 205.251.218.72",
        "HTTP_X_FORWARDED_PORT": "443",
        "HTTP_X_FORWARDED_PROTO": "https",
        "PATH_INFO": "/some/path",
        "QUERY_STRING": url_encode(event["queryStringParameters"]),
        "REMOTE_ADDR": "76.20.166.147",
        "REMOTE_USER": "******",
        "REQUEST_METHOD": "GET",
        "SCRIPT_NAME": "/prod",
        "SERVER_NAME": "custom.domain.com",
        "SERVER_PORT": "443",
        "SERVER_PROTOCOL": "HTTP/1.1",
        "wsgi.errors": wsgi_handler.wsgi_app.last_environ["wsgi.errors"],
        "wsgi.input": wsgi_handler.wsgi_app.last_environ["wsgi.input"],
        "wsgi.multiprocess": False,
        "wsgi.multithread": False,
        "wsgi.run_once": False,
        "wsgi.url_scheme": "https",
        "wsgi.version": (1, 0),
        "API_GATEWAY_AUTHORIZER": {
            "principalId": "wile_e_coyote"
        },
        "context": {},
        "event": event,
        "serverless.authorizer": {
            "principalId": "wile_e_coyote"
        },
        "serverless.context": {},
        "serverless.event": event,
    }
コード例 #2
0
def test_handler_binary_request_body(
    mock_wsgi_app_file, mock_app, event_v1, wsgi_handler
):
    event_v1["body"] = (
        "LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5VTRDZE5CRWVLQWxIaGRRcQ0KQ29udGVu"
        "dC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJ3YXQiDQoNCmhleW9vb3Bw"
        "cHBwDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlVNENkTkJFZUtBbEhoZFFxDQpD"
        "b250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImZpbGVUb1VwbG9h"
        "ZCI7IGZpbGVuYW1lPSJGRjREMDAtMC44LnBuZyINCkNvbnRlbnQtVHlwZTogaW1h"
        "Z2UvcG5nDQoNColQTkcNChoKAAAADUlIRFIAAAABAAAAAQEDAAAAJdtWygAAAANQ"
        "TFRF/00AXDU4fwAAAAF0Uk5TzNI0Vv0AAAAKSURBVHicY2IAAAAGAAM2N3yoAAAA"
        "AElFTkSuQmCCDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlVNENkTkJFZUtBbEho"
        "ZFFxDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9InN1Ym1p"
        "dCINCg0KVXBsb2FkIEltYWdlDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlVNENk"
        "TkJFZUtBbEhoZFFxLS0NCg=="
    )
    event_v1["headers"][
        "Content-Type"
    ] = "multipart/form-data; boundary=----WebKitFormBoundaryU4CdNBEeKAlHhdQq"
    event_v1["isBase64Encoded"] = True
    event_v1["httpMethod"] = "POST"

    wsgi_handler.handler(event_v1, {})

    environ = wsgi_handler.wsgi_app.last_environ

    assert environ["CONTENT_LENGTH"] == "496"
    assert Request(environ).form["submit"] == "Upload Image"
コード例 #3
0
def test_handler_strip_stage_path(mock_wsgi_app_file, mock_app, event_v1, wsgi_handler):
    try:
        os.environ["STRIP_STAGE_PATH"] = "True"
        wsgi_handler.handler(event_v1, {})
    finally:
        del os.environ["STRIP_STAGE_PATH"]

    assert wsgi_handler.wsgi_app.last_environ["SCRIPT_NAME"] == ""
コード例 #4
0
def test_command_exec(mock_wsgi_app_file, mock_app, wsgi_handler):
    response = wsgi_handler.handler(
        {"_serverless-wsgi": {"command": "exec", "data": "print(1+4)"}}, {}
    )

    assert response == "5\n"

    response = wsgi_handler.handler(
        {"_serverless-wsgi": {"command": "exec", "data": "invalid code"}}, {}
    )

    assert "Traceback (most recent call last):" in response
    assert "SyntaxError: invalid syntax" in response
コード例 #5
0
def test_handler_base64_request(mock_wsgi_app_file, mock_app, event_v1, wsgi_handler):
    event_v1["body"] = "SGVsbG8gd29ybGQ="
    event_v1["headers"]["Content-Type"] = "text/plain"
    event_v1["isBase64Encoded"] = True
    event_v1["httpMethod"] = "PUT"

    wsgi_handler.handler(event_v1, {})

    environ = wsgi_handler.wsgi_app.last_environ

    assert environ["CONTENT_TYPE"] == "text/plain"
    assert environ["CONTENT_LENGTH"] == "11"
    assert environ["REQUEST_METHOD"] == "PUT"
    assert environ["wsgi.input"].getvalue().decode() == "Hello world"
コード例 #6
0
def test_handler_request_body_undecodable_with_latin1(mock_wsgi_app_file,
                                                      mock_app, event,
                                                      wsgi_handler):
    event["body"] = (u"------WebKitFormBoundary3vA72kRLuq9D3NdL\r\n"
                     u'Content-Disposition: form-data; name="text"\r\n\r\n'
                     u"テスト 테스트 测试\r\n"
                     u"------WebKitFormBoundary3vA72kRLuq9D3NdL--")
    event["headers"][
        "Content-Type"] = "multipart/form-data; boundary=----WebKitFormBoundary3vA72kRLuq9D3NdL"
    event["httpMethod"] = "POST"

    wsgi_handler.handler(event, {})

    environ = wsgi_handler.wsgi_app.last_environ
    assert Request(environ).form["text"] == u"テスト 테스트 测试"
コード例 #7
0
def test_command_unknown(mock_wsgi_app_file, mock_app, wsgi_handler):
    response = wsgi_handler.handler(
        {"_serverless-wsgi": {"command": "unknown", "data": 'echo "hello world"'}}, {}
    )

    assert "Traceback (most recent call last):" in response
    assert "Exception: Unknown command: unknown" in response
コード例 #8
0
def test_command_flask(mock_wsgi_app_file, mock_app, wsgi_handler):
    class MockObject:
        def __init__(self, **kwargs):
            for k, v in kwargs.items():
                self.__dict__[k] = v

    class MockFlaskGroup:
        def __init__(self, create_app):
            assert create_app() == mock_app

        def main(ctx, args, standalone_mode):
            assert not standalone_mode
            print("Called with: {}".format(", ".join(args)))

    sys.modules["flask"] = MockObject()
    sys.modules["flask.cli"] = MockObject(FlaskGroup=MockFlaskGroup)

    response = wsgi_handler.handler(
        {"_serverless-wsgi": {
            "command": "flask",
            "data": "custom command"
        }}, {})

    assert response[0] == 0
    assert response[1] == "Called with: custom, command\n"
コード例 #9
0
def test_alb_multi_query_params(mock_wsgi_app_file, mock_app, wsgi_handler,
                                elb_event):
    del (elb_event["queryStringParameters"])
    elb_event["multiValueQueryStringParameters"] = {
        "%E6%B8%AC%E8%A9%A6": ["%E3%83%86%E3%82%B9%E3%83%88", "test"],
        "test": "test%20test"
    }
    response = wsgi_handler.handler(elb_event, {})
    query_string = wsgi_handler.wsgi_app.last_environ["QUERY_STRING"]
    assert query_string == url_encode({
        "測試": ["テスト", "test"],
        "test": "test test"
    })

    assert response == {
        "body": u"Hello World ☃!",
        "headers": {
            "set-cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "text/plain; charset=utf-8",
            "sEt-cookie": "LOT_NUMBER=42; Path=/",
            "Set-cookie": "PART_NUMBER=ROCKET_LAUNCHER_0002; Path=/",
        },
        "statusDescription": "200 OK",
        "statusCode": 200,
        "isBase64Encoded": False,
    }
コード例 #10
0
def test_handler_warmup_plugin(mock_wsgi_app_file, mock_app, event,
                               wsgi_handler, capsys):
    event = {"source": "serverless-plugin-warmup"}
    response = wsgi_handler.handler(event, {})
    assert response == {}

    out, err = capsys.readouterr()
    assert out == "Lambda warming event received, skipping handler\n"
    assert err == ""
コード例 #11
0
def test_command_command(mock_wsgi_app_file, mock_app, wsgi_handler):
    response = wsgi_handler.handler(
        {
            "_serverless-wsgi": {
                "command": "command",
                "data": 'echo "hello world"'
            }
        }, {})

    assert response == "hello world\n"
コード例 #12
0
def test_command_command(mock_wsgi_app_file, mock_app, wsgi_handler):
    response = wsgi_handler.handler(
        {"_serverless-wsgi": {"command": "command", "data": 'echo "hello world"'}}, {}
    )

    assert response[0] == 0
    assert response[1] == "hello world\n"

    response = wsgi_handler.handler(
        {
            "_serverless-wsgi": {
                "command": "command",
                "data": "ls non-existing-filename",
            }
        },
        {},
    )

    assert response[0] > 0
    assert "No such file or directory" in response[1]
コード例 #13
0
def test_handler_no_cookie(mock_wsgi_app_file, mock_app, event_v1, wsgi_handler):
    wsgi_handler.wsgi_app.cookie_count = 0
    response = wsgi_handler.handler(event_v1, {})

    assert response == {
        "body": "Hello World ☃!",
        "headers": {
            "Content-Length": "16",
            "Content-Type": "text/plain; charset=utf-8",
        },
        "statusCode": 200,
        "isBase64Encoded": False,
    }
コード例 #14
0
def test_handler_single_cookie(mock_wsgi_app_file, mock_app, event_v1, wsgi_handler):
    wsgi_handler.wsgi_app.cookie_count = 1
    response = wsgi_handler.handler(event_v1, {})

    assert response == {
        "body": "Hello World ☃!",
        "headers": {
            "Set-Cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "text/plain; charset=utf-8",
        },
        "statusCode": 200,
        "isBase64Encoded": False,
    }
コード例 #15
0
def test_handler_base64(mock_wsgi_app_file, mock_app, event_v1, wsgi_handler):
    wsgi_handler.wsgi_app.cookie_count = 1
    wsgi_handler.wsgi_app.response_mimetype = "image/jpeg"
    response = wsgi_handler.handler(event_v1, {})

    assert response == {
        "body": "SGVsbG8gV29ybGQg4piDIQ==",
        "headers": {
            "Set-Cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "image/jpeg",
        },
        "statusCode": 200,
        "isBase64Encoded": True,
    }
コード例 #16
0
def test_handler_custom_text_mime_types(mock_text_mime_wsgi_app_file, mock_app,
                                        event, wsgi_handler):
    wsgi_handler.wsgi_app.cookie_count = 1
    wsgi_handler.wsgi_app.response_mimetype = "application/custom+json"
    response = wsgi_handler.handler(event, {})

    assert response == {
        "body": u"Hello World ☃!",
        "headers": {
            "Set-Cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "application/custom+json",
        },
        "statusCode": 200,
        "isBase64Encoded": False,
    }
コード例 #17
0
def test_handler_alb(mock_wsgi_app_file, mock_app, wsgi_handler, elb_event):
    response = wsgi_handler.handler(elb_event, {})

    assert response == {
        "body": "Hello World ☃!",
        "headers": {
            "set-cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "text/plain; charset=utf-8",
            "sEt-cookie": "LOT_NUMBER=42; Path=/",
            "Set-cookie": "PART_NUMBER=ROCKET_LAUNCHER_0002; Path=/",
        },
        "statusDescription": "200 OK",
        "statusCode": 200,
        "isBase64Encoded": False,
    }
コード例 #18
0
def test_handler_alb(mock_wsgi_app_file, mock_app, wsgi_handler):
    response = wsgi_handler.handler(
        {
            "requestContext": {
                "elb": {
                    "targetGroupArn":
                    "arn:aws:elasticloadbalancing:us-east-1:12345:targetgroup/xxxx/5e43816d76759862"
                }
            },
            "httpMethod": "GET",
            "path": "/cats",
            "queryStringParameters": {},
            "headers": {
                "accept":
                "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
                "accept-encoding": "gzip, deflate",
                "accept-language": "en-US,en;q=0.9,da;q=0.8",
                "cache-control": "max-age=0",
                "connection": "keep-alive",
                "host": "xxxx-203391234.us-east-1.elb.amazonaws.com",
                "upgrade-insecure-requests": "1",
                "user-agent":
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
                "x-amzn-trace-id": "Root=1-5f05949b-77e2b0f9434e2acbf5ad8ce8",
                "x-forwarded-for": "95.181.37.218",
                "x-forwarded-port": "80",
                "x-forwarded-proto": "http",
            },
            "body": "",
            "isBase64Encoded": False,
        },
        {},
    )

    assert response == {
        "body": u"Hello World ☃!",
        "headers": {
            "set-cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "text/plain; charset=utf-8",
            "sEt-cookie": "LOT_NUMBER=42; Path=/",
            "Set-cookie": "PART_NUMBER=ROCKET_LAUNCHER_0002; Path=/",
        },
        "statusDescription": "200 OK",
        "statusCode": 200,
        "isBase64Encoded": False,
    }
コード例 #19
0
def test_command_manage(mock_wsgi_app_file, mock_app, wsgi_handler):
    class MockObject:
        pass

    class MockDjango:
        def call_command(*args):
            print("Called with: {}".format(", ".join(args[1:])))

    sys.modules["django"] = MockObject()
    sys.modules["django.core"] = MockObject()
    sys.modules["django.core"].management = MockDjango()

    response = wsgi_handler.handler(
        {"_serverless-wsgi": {"command": "manage", "data": "check --list-tags"}}, {}
    )

    assert response == "Called with: check, --list-tags\n"
コード例 #20
0
def test_alb_query_params(mock_wsgi_app_file, mock_app, wsgi_handler, elb_event):
    elb_event["queryStringParameters"] = {"test": "test%20test"}
    response = wsgi_handler.handler(elb_event, {})
    query_string = wsgi_handler.wsgi_app.last_environ["QUERY_STRING"]
    assert query_string == "test=test+test"

    assert response == {
        "body": "Hello World ☃!",
        "headers": {
            "set-cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "text/plain; charset=utf-8",
            "sEt-cookie": "LOT_NUMBER=42; Path=/",
            "Set-cookie": "PART_NUMBER=ROCKET_LAUNCHER_0002; Path=/",
        },
        "statusDescription": "200 OK",
        "statusCode": 200,
        "isBase64Encoded": False,
    }
コード例 #21
0
def test_handler_multivalue(
    mock_wsgi_app_file, mock_app, event_v1, capsys, wsgi_handler
):
    event_v1["multiValueQueryStringParameters"] = {
        "param1": ["value1"],
        "param2": ["value2", "value3"],
    }

    # Convert regular headers request to multiValueHeaders
    multi_headers = {}
    for key, value in event_v1["headers"].items():
        if key not in multi_headers:
            multi_headers[key] = []
        multi_headers[key].append(value)

    event_v1["multiValueHeaders"] = multi_headers
    response = wsgi_handler.handler(event_v1, {"memory_limit_in_mb": "128"})
    query_string = wsgi_handler.wsgi_app.last_environ["QUERY_STRING"]

    assert query_string == url_encode(
        MultiDict(
            (i, k)
            for i, j in event_v1["multiValueQueryStringParameters"].items()
            for k in j
        )
    )

    assert response == {
        "body": "Hello World ☃!",
        "multiValueHeaders": {
            "Content-Length": ["16"],
            "Content-Type": ["text/plain; charset=utf-8"],
            "Set-Cookie": [
                "CUSTOMER=WILE_E_COYOTE; Path=/",
                "PART_NUMBER=ROCKET_LAUNCHER_0002; Path=/",
                "LOT_NUMBER=42; Path=/",
            ],
        },
        "statusCode": 200,
        "isBase64Encoded": False,
    }
コード例 #22
0
def test_handler_plain(mock_wsgi_app_file, mock_app, event_v1, wsgi_handler):
    wsgi_handler.wsgi_app.cookie_count = 1

    plain_mimetypes = [
        "application/vnd.api+json",
        "application/javascript; charset=utf-8",
        "image/svg+xml; charset=utf-8",
    ]

    for mimetype in plain_mimetypes:
        wsgi_handler.wsgi_app.response_mimetype = mimetype
        response = wsgi_handler.handler(event_v1, {})

        assert response == {
            "body": "Hello World ☃!",
            "headers": {
                "Set-Cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
                "Content-Length": "16",
                "Content-Type": mimetype,
            },
            "statusCode": 200,
            "isBase64Encoded": False,
        }
コード例 #23
0
def test_handler_v2(mock_wsgi_app_file, mock_app, event_v2, capsys, wsgi_handler):
    response = wsgi_handler.handler(event_v2, {"memory_limit_in_mb": "128"})

    assert response == {
        "body": "Hello World ☃!",
        "headers": {
            "set-cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "text/plain; charset=utf-8",
            "sEt-cookie": "LOT_NUMBER=42; Path=/",
            "Set-cookie": "PART_NUMBER=ROCKET_LAUNCHER_0002; Path=/",
        },
        "statusCode": 200,
        "isBase64Encoded": False,
    }

    assert wsgi_handler.wsgi_app.last_environ == {
        "CONTENT_LENGTH": "0",
        "CONTENT_TYPE": "",
        "HTTP_ACCEPT": "*/*",
        "HTTP_ACCEPT_ENCODING": "gzip, deflate",
        "HTTP_CACHE_CONTROL": "no-cache",
        "HTTP_CLOUDFRONT_FORWARDED_PROTO": "https",
        "HTTP_CLOUDFRONT_IS_DESKTOP_VIEWER": "true",
        "HTTP_CLOUDFRONT_IS_MOBILE_VIEWER": "false",
        "HTTP_CLOUDFRONT_IS_SMARTTV_VIEWER": "false",
        "HTTP_CLOUDFRONT_IS_TABLET_VIEWER": "false",
        "HTTP_CLOUDFRONT_VIEWER_COUNTRY": "DK",
        "HTTP_COOKIE": "CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001",
        "HTTP_HOST": "3z6kd9fbb1.execute-api.us-east-1.amazonaws.com",
        "HTTP_POSTMAN_TOKEN": "778a706e-d6b0-48d5-94dd-9e98c22f12fe",
        "HTTP_USER_AGENT": "PostmanRuntime/3.0.11-hotfix.2",
        "HTTP_VIA": "1.1 b8fa.cloudfront.net (CloudFront)",
        "HTTP_X_AMZN_TRACE_ID": "Root=1-58d534a5-1e7cffe644b086304dce7a1e",
        "HTTP_X_AMZ_CF_ID": "jx0Bvz9rm--Mz3wAj4i46FdOQQK3RHF4H0moJjBsQ==",
        "HTTP_X_FORWARDED_FOR": "76.20.166.147, 205.251.218.72",
        "HTTP_X_FORWARDED_PORT": "443",
        "HTTP_X_FORWARDED_PROTO": "https",
        "PATH_INFO": "/some/path",
        "QUERY_STRING": "param1=value1&param2=value2&param2=value3",
        "REMOTE_ADDR": "76.20.166.147",
        "REMOTE_USER": "******",
        "REQUEST_METHOD": "GET",
        "SCRIPT_NAME": "/dev",
        "SERVER_NAME": "3z6kd9fbb1.execute-api.us-east-1.amazonaws.com",
        "SERVER_PORT": "443",
        "SERVER_PROTOCOL": "HTTP/1.1",
        "wsgi.errors": wsgi_handler.wsgi_app.last_environ["wsgi.errors"],
        "wsgi.input": wsgi_handler.wsgi_app.last_environ["wsgi.input"],
        "wsgi.multiprocess": False,
        "wsgi.multithread": False,
        "wsgi.run_once": False,
        "wsgi.url_scheme": "https",
        "wsgi.version": (1, 0),
        "serverless.authorizer": {"principalId": "wile_e_coyote"},
        "serverless.context": {"memory_limit_in_mb": "128"},
        "serverless.event": event_v2,
    }

    out, err = capsys.readouterr()
    assert out == ""
    assert err == "application debug #1\n"
コード例 #24
0
def test_handler_warmup_plugin(mock_wsgi_app_file, mock_app, event, wsgi_handler):
    event = {"source": "serverless-plugin-warmup"}
    response = wsgi_handler.handler(event, {})
    assert response == {}
コード例 #25
0
def test_handler_lambda_error(
    mock_wsgi_app_file, mock_app, event_lambda_integration, capsys, wsgi_handler
):
    mock_app.status_code = 400
    with pytest.raises(Exception, match='"statusCode": 400'):
        wsgi_handler.handler(event_lambda_integration, {"memory_limit_in_mb": "128"})
コード例 #26
0
def test_handler_with_encoded_characters_in_path_v2(
    mock_wsgi_app_file, mock_app, event_v2, capsys, wsgi_handler
):
    event_v2["rawPath"] = "/city/new%20york"
    wsgi_handler.handler(event_v2, {"memory_limit_in_mb": "128"})
    assert wsgi_handler.wsgi_app.last_environ["PATH_INFO"] == "/city/new york"
コード例 #27
0
def test_handler_schedule(mock_wsgi_app_file, mock_app, event_v1, wsgi_handler):
    event_v1 = {"source": "aws.events"}
    response = wsgi_handler.handler(event_v1, {})
    assert response == {}
コード例 #28
0
def test_handler_lambda(
    mock_wsgi_app_file, mock_app, event_lambda_integration, capsys, wsgi_handler
):
    response = wsgi_handler.handler(
        event_lambda_integration, {"memory_limit_in_mb": "128"}
    )

    assert response == {
        "body": "Hello World ☃!",
        "headers": {
            "set-cookie": "CUSTOMER=WILE_E_COYOTE; Path=/",
            "Content-Length": "16",
            "Content-Type": "text/plain; charset=utf-8",
            "sEt-cookie": "LOT_NUMBER=42; Path=/",
            "Set-cookie": "PART_NUMBER=ROCKET_LAUNCHER_0002; Path=/",
        },
        "statusCode": 200,
        "isBase64Encoded": False,
    }

    assert wsgi_handler.wsgi_app.last_environ == {
        "CONTENT_LENGTH": "0",
        "CONTENT_TYPE": "",
        "HTTP_ACCEPT": "*/*",
        "HTTP_AUTHORIZATION": "Bearer f14a720d62e1d1295d9",
        "HTTP_CLOUDFRONT_FORWARDED_PROTO": "https",
        "HTTP_CLOUDFRONT_IS_DESKTOP_VIEWER": "true",
        "HTTP_CLOUDFRONT_IS_MOBILE_VIEWER": "false",
        "HTTP_CLOUDFRONT_IS_SMARTTV_VIEWER": "false",
        "HTTP_CLOUDFRONT_IS_TABLET_VIEWER": "false",
        "HTTP_CLOUDFRONT_VIEWER_COUNTRY": "FI",
        "HTTP_HOST": "k3k8rkx1mf.execute-api.us-east-1.amazonaws.com",
        "HTTP_USER_AGENT": "curl/7.68.0",
        "HTTP_VIA": "2.0 3bf180720d62e0d1295d99086d103efb.cloudfront.net (CloudFront)",
        "HTTP_X_AMZN_TRACE_ID": "Root=1-5055b7d3-751afb497f81bab2759b6e7b",
        "HTTP_X_AMZ_CF_ID": "9Z6K736EDx_vlsij1PA-ZVxIPPi-vAIMaLNOvJ2FrbpvGMisAISY8Q==",
        "HTTP_X_FORWARDED_FOR": "83.23.10.243, 130.166.149.164",
        "HTTP_X_FORWARDED_PORT": "443",
        "HTTP_X_FORWARDED_PROTO": "https",
        "PATH_INFO": "/some/path2",
        "QUERY_STRING": "q=test",
        "REMOTE_ADDR": "83.23.100.243",
        "REMOTE_USER": "******",
        "REQUEST_METHOD": "GET",
        "SCRIPT_NAME": "/dev",
        "SERVER_NAME": "k3k8rkx1mf.execute-api.us-east-1.amazonaws.com",
        "SERVER_PORT": "443",
        "SERVER_PROTOCOL": "HTTP/1.1",
        "wsgi.errors": wsgi_handler.wsgi_app.last_environ["wsgi.errors"],
        "wsgi.input": wsgi_handler.wsgi_app.last_environ["wsgi.input"],
        "wsgi.multiprocess": False,
        "wsgi.multithread": False,
        "wsgi.run_once": False,
        "wsgi.url_scheme": "https",
        "wsgi.version": (1, 0),
        "serverless.authorizer": {
            "principalId": "testuser",
            "integrationLatency": "1031",
            "contextTest": "123",
        },
        "serverless.context": {"memory_limit_in_mb": "128"},
        "serverless.event": event_lambda_integration,
    }

    out, err = capsys.readouterr()
    assert out == ""
    assert err == "application debug #1\n"
コード例 #29
0
def test_handler_china(mock_wsgi_app_file, mock_app, event_v1, capsys, wsgi_handler):
    event_v1["headers"]["Host"] = "x.amazonaws.com.cn"
    wsgi_handler.handler(event_v1, {"memory_limit_in_mb": "128"})

    assert wsgi_handler.wsgi_app.last_environ["SCRIPT_NAME"] == "/dev"