Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(apigatewayv2): answer an unmatched route with AWS's exact 404 body (#1890)

Jeff Heller16bf8771d762
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f4a87458..83202148 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
+- **API Gateway v2 (HTTP API) — an unmatched route answers AWS's exact 404 body** — a request with no matching route answered `{"message": "No route found"}`; AWS answers `{"message":"Not Found"}` (compact JSON, no space after the colon). REST API (v1) is unaffected — it already answers a route miss with its own `403 MISSING_AUTHENTICATION_TOKEN` gateway response, not this literal.
- **API Gateway v2 — `apiEndpoint` follows the gateway's TLS scheme** — with `USE_SSL=1` the gateway listener serves only HTTPS, but CreateApi and the `AWS::ApiGatewayV2::Api` CloudFormation resource always returned an `http://` `apiEndpoint`, which nothing answers. They now return `https://`, or `wss://` for a WebSocket API, as AWS does. Without `USE_SSL` the endpoint keeps `http://`.
- **IoT — `DescribeEventConfigurations` and `UpdateEventConfigurations`** — both answered `Unsupported IoT path`; they now store the registry event switches per account and region, every type starting disabled, an update changing only the types it names, and `creationDate` / `lastModifiedDate` set from the first update on, as on AWS.
diff --git a/ministack/services/apigateway.py b/ministack/services/apigateway.py
index b334425b..ea4f6f0b 100644
--- a/ministack/services/apigateway.py
+++ b/ministack/services/apigateway.py
@@ -1296,7 +1296,12 @@ async def _handle_execute_in_scope(
route = _match_route(api_id, method, path)
if not route:
- return 404, {"Content-Type": "application/json"}, json.dumps({"message": "No route found"}).encode()
+ # AWS's body for an unmatched HTTP API route: compact JSON.
+ return (
+ 404,
+ {"Content-Type": "application/json"},
+ json.dumps({"message": "Not Found"}, separators=(",", ":")).encode(),
+ )
request_headers = {k.lower(): v for k, v in (headers or {}).items()}
route_key = route.get("routeKey", "$default")
diff --git a/tests/test_apigatewayv2.py b/tests/test_apigatewayv2.py
index 67dccdfb..bd4fa780 100644
--- a/tests/test_apigatewayv2.py
+++ b/tests/test_apigatewayv2.py
@@ -1009,7 +1009,8 @@ def test_apigw_execute_lambda_proxy_binary_request_body(apigw, lam):
lam.delete_function(FunctionName=fname)
def test_apigw_execute_no_route(apigw):
- """execute-api returns 404 when no matching route exists."""
+ """execute-api returns 404 when no matching route exists, with AWS's
+ exact compact-JSON body (no space after the colon)."""
import urllib.error as _urlerr
import urllib.request as _urlreq
@@ -1023,6 +1024,7 @@ def test_apigw_execute_no_route(apigw):
assert False, "Expected 404"
except _urlerr.HTTPError as e:
assert e.code == 404
+ assert e.read() == b'{"message":"Not Found"}'
apigw.delete_api(ApiId=api_id)
def test_apigw_execute_default_route(apigw, lam):