Skip to content

Commit 836bfdb

Browse files
committed
fix MOCK & fixture
1 parent b10d593 commit 836bfdb

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

localstack-core/localstack/services/apigateway/next_gen/execute_api/test_invoke.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_INVOKE_TEMPLATE = """Execution log for request {request_id}
2424
{formatted_date} : Starting execution for request: {request_id}
25-
{formatted_date} : HTTP Method: GET, Resource Path: {resource_path}
25+
{formatted_date} : HTTP Method: {request_method}, Resource Path: {resource_path}
2626
{formatted_date} : Method request path: {method_request_path_parameters}
2727
{formatted_date} : Method request query string: {method_request_query_string}
2828
{formatted_date} : Method request headers: {method_request_headers}
@@ -68,7 +68,8 @@ def log_template(invocation_context: RestApiInvocationContext, response_headers:
6868
return TEST_INVOKE_TEMPLATE.format(
6969
formatted_date=formatted_date,
7070
request_id=context_var["requestId"],
71-
resource_path=context_var["resourcePath"],
71+
resource_path=request["path"],
72+
request_method=request["http_method"],
7273
method_request_path_parameters=dict_to_string(request["path_parameters"]),
7374
method_request_query_string=dict_to_string(request["query_string_parameters"]),
7475
method_request_headers=_dump_headers(request.get("headers")),
@@ -176,11 +177,18 @@ def run_test_invocation(
176177
invocation_context = create_test_invocation_context(test_request, deployment)
177178

178179
test_chain = create_test_chain()
179-
# we manually add the trace-id, as it is normally added by handlers.response_enricher which adds to much data for
180-
# the TestInvoke
181-
test_response = Response(
182-
headers={"X-Amzn-Trace-Id": invocation_context.trace_id, "Content-Type": APPLICATION_JSON}
183-
)
180+
# header order is important
181+
if invocation_context.integration["type"] == "MOCK":
182+
base_headers = {"Content-Type": APPLICATION_JSON}
183+
else:
184+
# we manually add the trace-id, as it is normally added by handlers.response_enricher which adds to much data
185+
# for the TestInvoke. It needs to be first
186+
base_headers = {
187+
"X-Amzn-Trace-Id": invocation_context.trace_id,
188+
"Content-Type": APPLICATION_JSON,
189+
}
190+
191+
test_response = Response(headers=base_headers)
184192
start_time = datetime.datetime.now()
185193
test_chain.handle(context=invocation_context, response=test_response)
186194
end_time = datetime.datetime.now()

tests/aws/services/apigateway/apigateway_fixtures.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ def assert_response_is_201(response: Dict) -> bool:
2626
return True
2727

2828

29+
def import_rest_api(apigateway_client, **kwargs):
30+
response = apigateway_client.import_rest_api(**kwargs)
31+
assert_response_is_201(response)
32+
resources = apigateway_client.get_resources(restApiId=response.get("id"))
33+
root_id = next(item for item in resources["items"] if item["path"] == "/")["id"]
34+
35+
return response, root_id
36+
37+
2938
def create_rest_resource(apigateway_client, **kwargs):
3039
response = apigateway_client.create_resource(**kwargs)
3140
assert_response_is_201(response)

tests/aws/services/apigateway/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
create_rest_api_stage,
1414
create_rest_resource,
1515
create_rest_resource_method,
16+
import_rest_api,
1617
)
1718
from tests.aws.services.lambda_.test_lambda import TEST_LAMBDA_PYTHON_ECHO_STATUS_CODE
1819

@@ -223,7 +224,7 @@ def import_apigw(aws_client, aws_client_factory):
223224
apigateway_client = aws_client.apigateway
224225

225226
def _import_apigateway_function(*args, **kwargs):
226-
response, root_id = apigateway_client.import_rest_api(**kwargs)
227+
response, root_id = import_rest_api(apigateway_client**kwargs)
227228
rest_api_ids.append(response.get("id"))
228229
return response, root_id
229230

tests/aws/services/apigateway/test_apigateway_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,7 @@ def test_invoke_test_method(self, create_rest_apigw, snapshot, aws_client):
23202320
lambda k, v: str(v) if k == "latency" else None, "latency", replace_reference=False
23212321
)
23222322
)
2323+
# TODO: maybe transformer `log` better
23232324
snapshot.add_transformer(
23242325
snapshot.transform.key_value("log", "log", reference_replacement=False)
23252326
)

0 commit comments

Comments
 (0)