Skip to content

APIGW: fix TestInvokeMethod path logic #13030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 19, 2025
Merged

APIGW: fix TestInvokeMethod path logic #13030

merged 3 commits into from
Aug 19, 2025

Conversation

bentsku
Copy link
Contributor

@bentsku bentsku commented Aug 19, 2025

Motivation

We got a report that the TestInvokeMethod would not work when the pathWithQueryString was not provided. After testing in AWS, it showed that this parameter was not required and this was an assumption that stayed from the previous implementation.

This PR implements more tests around this parameter and the behavior of AWS when it is not provided.

It also showed AWS is not sending the same log response when the integration is of type MOCK.

I took the opportunity to refactor a bit the utilities we have around TestInvokeMethod, because it is quite tricky to snapshot match as it returns a big string with newlines that we would like to snapshot match more precisely.

Changes

  • fix the behavior of TestInvokeMethod when pathWithQueryString is not provided: it will fallback to the Resource path value
  • fix the behavior when it cannot extract the path parameters from the provided path: it won't fail but silently set the path parameters to an empty map
  • add a new log template for MOCK integration, this should probably be revisited at some point
  • improve the existing tests and add more cases
  • refactor the test utilities
  • remove overly verbose stack trace logging when debug is enabled when we don't match a route in API Gateway REST API router

@bentsku bentsku added this to the 4.8 milestone Aug 19, 2025
@bentsku bentsku self-assigned this Aug 19, 2025
@bentsku bentsku added aws:apigateway Amazon API Gateway semver: patch Non-breaking changes which can be included in patch releases labels Aug 19, 2025

# we do not need a true HTTP request for the context, as we are skipping all the parsing steps and using the
# provider data
invocation_context = RestApiInvocationContext(
request=Request(method=http_method),
)
path_query = test_request.get("pathWithQueryString", "/").split("?")
test_request_path = test_request.get("pathWithQueryString") or resource_path
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this single line is effectively the fix for it

Comment on lines 184 to 189
try:
# this is AWS behavior, it will accept any value for the `pathWithQueryString`, even if it doesn't match
# the expected format. It will just fall back to no value if it cannot parse the path parameters out of it
_, path_parameters = RestAPIResourceRouter(deployment).match(invocation_context)
except Exception:
path_parameters = {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also part of the fix, due to the ability to pass invalid values, but it was not strictly needed for the sample to work

Copy link

github-actions bot commented Aug 19, 2025

Test Results - Preflight, Unit

22 144 tests  ±0   20 407 ✅ ±0   6m 19s ⏱️ -3s
     1 suites ±0    1 737 💤 ±0 
     1 files   ±0        0 ❌ ±0 

Results for commit 12c143c. ± Comparison against base commit 3f463b5.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Aug 19, 2025

LocalStack Community integration with Pro

    2 files  ±    0      2 suites  ±0   18m 16s ⏱️ - 1h 25m 44s
1 159 tests  - 3 474  1 091 ✅  - 3 102  68 💤  - 372  0 ❌ ±0 
1 161 runs   - 3 474  1 091 ✅  - 3 102  70 💤  - 372  0 ❌ ±0 

Results for commit 12c143c. ± Comparison against base commit 3f463b5.

This pull request removes 3474 tests.
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_lambda_dynamodb
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_opensearch_crud
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_search_books
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_setup
tests.aws.scenario.kinesis_firehose.test_kinesis_firehose.TestKinesisFirehoseScenario ‑ test_kinesis_firehose_s3
tests.aws.scenario.lambda_destination.test_lambda_destination_scenario.TestLambdaDestinationScenario ‑ test_destination_sns
tests.aws.scenario.lambda_destination.test_lambda_destination_scenario.TestLambdaDestinationScenario ‑ test_infra
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_prefill_dynamodb_table
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_stepfunctions_input_recipient_list[step_function_input0-SUCCEEDED]
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_stepfunctions_input_recipient_list[step_function_input1-SUCCEEDED]
…

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Aug 19, 2025

Test Results (amd64) - Acceptance

7 tests  ±0   5 ✅ ±0   3m 6s ⏱️ -1s
1 suites ±0   2 💤 ±0 
1 files   ±0   0 ❌ ±0 

Results for commit 12c143c. ± Comparison against base commit 3f463b5.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Aug 19, 2025

Test Results (amd64) - Integration, Bootstrap

    5 files      5 suites   35m 13s ⏱️
1 183 tests 1 116 ✅ 67 💤 0 ❌
1 189 runs  1 116 ✅ 73 💤 0 ❌

Results for commit 12c143c.

♻️ This comment has been updated with latest results.

Copy link
Member

@joe4dev joe4dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the thorough parity improvements 🚀

# this is AWS behavior, it will accept any value for the `pathWithQueryString`, even if it doesn't match
# the expected format. It will just fall back to no value if it cannot parse the path parameters out of it
_, path_parameters = RestAPIResourceRouter(deployment).match(invocation_context)
except Exception:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we ok swalling the exception here, or are there scenarios where debug logs would be necessary to understand a bug in .match?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought we're fine, the routing step is already done and we're using it only to extract the path parameters. As the input can be anything, any error could surface. I was thinking about adding debug logs, but I wasn't sure it would be very useful. But maybe just a warning for user could still be useful somehow 🤔 I'll add something

@@ -45,6 +45,19 @@
{formatted_date} : Method completed with status: {method_response_status}
"""

TEST_INVOKE_TEMPLATE_MOCK = """Execution log for request {request_id}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: neat detail considering these verbose mock logs ✨


# we want to do very precise matching on the log, and splitting on new lines will help in case the snapshot
# fails
# the snapshot library does not allow to ignore an array index as the last node, so we need to put it in a dict
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: @tiurin snapshot library limitation to consider tracking

Copy link
Contributor Author

@bentsku bentsku Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was copy-pasted from the previous utility, I actually fixed that issue with localstack/localstack-snapshot#16 but we don't yet have a release of localstack-snapshot 😅 soon I'll update these!

target_resource_id=resource_id,
method="GET",
)
# assert "HTTP Method: GET, Resource Path: /pets/123" in response["log"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is this comment intentional (same below)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch! I'll update them, I think I had the case once that the snapshots were not exactly what I was expecting from AWS, so those made sure that we got what we wanted. Will update them 👍

@bentsku bentsku marked this pull request as ready for review August 19, 2025 21:31
@bentsku bentsku requested a review from cloutierMat as a code owner August 19, 2025 21:31
@bentsku bentsku merged commit 7b9533a into main Aug 19, 2025
42 of 44 checks passed
@bentsku bentsku deleted the apigw/fix-test-invoke branch August 19, 2025 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws:apigateway Amazon API Gateway semver: patch Non-breaking changes which can be included in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants