10000 refactor API GW tests, assert correct JSON content-type · localstack/localstack@67f9dce · GitHub
[go: up one dir, main page]

Skip to content

Commit 67f9dce

Browse files
committed
refactor API GW tests, assert correct JSON content-type
1 parent f4d39cf commit 67f9dce

15 files changed

+905
-817
lines changed

localstack/services/apigateway/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def __init__(
9393
self.path_params = {}
9494
self.route = None
9595
self.ws_route = None
96+
self.response = None
9697

9798
@property
9899
def resource_id(self) -> Optional[str]:

localstack/services/apigateway/integration.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,14 @@ def invoke(self, invocation_context: ApiInvocationContext):
449449

450450
# apply response templates
451451
response_content = json.dumps(remove_attributes(response, ["ResponseMetadata"]))
452-
response_obj = requests_response(content=response_content)
452+
invocation_context.response = response_obj = requests_response(content=response_content)
453453
response = self.response_templates.render(invocation_context, response=response_obj)
454454

455455
# construct final response
456-
response = requests_response(response)
457-
invocation_context.response = response
456+
# TODO: set response header based on response templates
457+
headers = {HEADER_CONTENT_TYPE: APPLICATION_JSON}
458+
response = requests_response(response, headers=headers)
459+
458460
return response
459461

460462

localstack/services/apigateway/templates.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ def render(self, api_context: ApiInvocationContext, **kwargs) -> Union[bytes, st
264264

265265
# we only support JSON templates for now - if there is no template we return
266266
# the response as is
267+
# TODO - support other content types, besides application/json!
267268
template = response_templates.get(APPLICATION_JSON, {})
268269
if not template:
269270
return response._content

tests/integration/apigateway_fixtures.py renamed to tests/integration/apigateway/apigateway_fixtures.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from localstack.testing.aws.util import is_aws_cloud
1010
from localstack.utils.aws import aws_stack
1111

12+
# TODO convert the test util functions in this file to pytest fixtures
13+
1214

1315
def _client(service, region_name=None, aws_access_key_id=None):
1416
if os.environ.get("TEST_TARGET") == "AWS_CLOUD":

tests/integration/apigateway/conftest.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from localstack.constants import APPLICATION_JSON
44
from localstack.utils.strings import short_uid
5-
from tests.integration.apigateway_fixtures import (
5+
from tests.integration.apigateway.apigateway_fixtures import (
66
create_rest_api_deployment,
77
create_rest_api_integration,
88
create_rest_api_integration_response,
@@ -11,15 +11,50 @@
1111
create_rest_resource,
1212
create_rest_resource_method,
1313
)
14-
from tests.integration.test_apigateway import (
15-
APIGATEWAY_ASSUME_ROLE_POLICY,
16-
APIGATEWAY_DYNAMODB_POLICY,
17-
APIGATEWAY_KINESIS_POLICY,
18-
)
1914

2015
# default name used for created REST API stages
2116
DEFAULT_STAGE_NAME = "dev"
2217

18+
STEPFUNCTIONS_ASSUME_ROLE_POLICY = {
19+
"Version": "2012-10-17",
20+
"Statement": [
21+
{
22+
"Effect": "Allow",
23+
"Principal": {"Service": "states.amazonaws.com"},
24+
"Action": "sts:AssumeRole",
25+
}
26+
],
27+
}
28+
29+
APIGATEWAY_STEPFUNCTIONS_POLICY = {
30+
"Version": "2012-10-17",
31+
"Statement": [{"Effect": "Allow", "Action": "states:*", "Resource": "*"}],
32+
}
33+
34+
APIGATEWAY_KINESIS_POLICY = {
35+
"Version": "2012-10-17",
36+
"Statement": [{"Effect": "Allow", "Action": "kinesis:*", "Resource": "*"}],
37+
}
38+
39+
APIGATEWAY_LAMBDA_POLICY = {
40+
"Version": "2012-10-17",
41+
"Statement": [{"Effect": "Allow", "Action": "lambda:*", "Resource": "*"}],
42+
}
43+
44+
APIGATEWAY_DYNAMODB_POLICY = {
45+
"Version": "2012-10-17",
46+
"Statement": [{"Effect": "Allow", "Action": "dynamodb:*", "Resource": "*"}],
47+
}
48+
49+
APIGATEWAY_ASSUME_ROLE_POLICY = {
50+
"Statement": {
51+
"Sid": "",
52+
"Effect": "Allow",
53+
"Principal": {"Service": "apigateway.amazonaws.com"},
54+
"Action": "sts:AssumeRole",
55+
}
56+
}
57+
2358

2459
@pytest.fixture
2560
def create_rest_api_with_integration(

0 commit comments

Comments
 (0)
0