|
| 1 | +import pytest |
| 2 | +from pydantic import ValidationError |
| 3 | + |
1 | 4 | from aws_lambda_powertools.utilities.parser import envelopes, event_parser
|
2 | 5 | from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
|
3 | 6 | from aws_lambda_powertools.utilities.typing import LambdaContext
|
@@ -100,3 +103,44 @@ def test_apigw_event():
|
100 | 103 | assert request_context.operationName is None
|
101 | 104 | assert identity.apiKey is None
|
102 | 105 | assert identity.apiKeyId is None
|
| 106 | + |
| 107 | + |
| 108 | +def test_apigw_event_with_invalid_websocket_request(): |
| 109 | + # GIVEN an event with an eventType != MESSAGE and has a messageId |
| 110 | + event = { |
| 111 | + "resource": "/", |
| 112 | + "path": "/", |
| 113 | + "httpMethod": "GET", |
| 114 | + "headers": {}, |
| 115 | + "multiValueHeaders": {}, |
| 116 | + "isBase64Encoded": False, |
| 117 | + "body": "Foo!", |
| 118 | + "requestContext": { |
| 119 | + "accountId": "1234", |
| 120 | + "apiId": "myApi", |
| 121 | + "httpMethod": "GET", |
| 122 | + "identity": { |
| 123 | + "sourceIp": "127.0.0.1", |
| 124 | + }, |
| 125 | + "path": "/", |
| 126 | + "protocol": "Https", |
| 127 | + "requestId": "1234", |
| 128 | + "requestTime": "2018-09-07T16:20:46Z", |
| 129 | + "requestTimeEpoch": 1536992496000, |
| 130 | + "resourcePath": "/", |
| 131 | + "stage": "test", |
| 132 | + "eventType": "DISCONNECT", |
| 133 | + "messageId": "messageId", |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + # WHEN calling event_parser with APIGatewayProxyEventModel |
| 138 | + with pytest.raises(ValidationError) as err: |
| 139 | + handle_apigw_event(event, LambdaContext()) |
| 140 | + |
| 141 | + # THEN raise TypeError for invalid event |
| 142 | + errors = err.value.errors() |
| 143 | + assert len(errors) == 1 |
| 144 | + expected_msg = "messageId is available only when the `eventType` is `MESSAGE`" |
| 145 | + assert errors[0]["msg"] == expected_msg |
| 146 | + assert expected_msg in str(err.value) |
0 commit comments