8000 add echo_http_server_post fixture for local/remote processing · localstack/localstack@a000d33 · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit a000d33

Browse files
committed
add echo_http_server_post fixture for local/remote processing
1 parent c0fe894 commit a000d33

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

.github/workflows/pro-integration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ jobs:
204204
python -m pytest --reruns 2 --durations=10 --show-capture=no --junitxml=target/reports/lambda-docker.xml -o junit_suite_name='lambda-docker' \
205205
../localstack/tests/integration/awslambda/ \
206206
../localstack/tests/integration/test_integration.py \
207-
../localstack/tests/integration/test_apigateway.py
207+
../localstack/tests/integration/apigateway/test_apigateway_basic.py
208208
- name: Run Lambda Tests for lambda executor docker-reuse
209209
env:
210210
DEBUG: 1
@@ -219,7 +219,7 @@ jobs:
219219
python -m pytest --reruns 2 --durations=10 --show-capture=no --junitxml=target/reports/lambda-docker-reuse.xml -o junit_suite_name='lambda-docker-reuse' \
220220
../localstack/tests/integration/awslambda/ \
221221
../localstack/tests/integration/test_integration.py \
222-
../localstack/tests/integration/test_apigateway.py
222+
../localstack/tests/integration/apigateway/test_apigateway_basic.py
223223
- name: Publish LocalStack Community Integration Test Results
224224
uses: EnricoMi/publish-unit-test-result-action@v1
225225
if: always()

localstack/services/apigateway/integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def invoke(self, invocation_context: ApiInvocationContext):
449449

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

455455
# construct final response

localstack/services/apigateway/templates.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ 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!
267+
# TODO - support other content types, besides application/json (based on `Accept` request header)
268+
# see https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html#selecting-mapping-templates
268269
template = response_templates.get(APPLICATION_JSON, {})
269270
if not template:
270271
return response._content

localstack/testing/pytest/fixtures.py

Lines changed: 20 additions & 3 deletions
2033
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
LocalAttribute,
3333
)
3434
from localstack.testing.aws.cloudformation_utils import load_template_file, render_template
35-
from localstack.testing.aws.util import get_lambda_logs
35+
from localstack.testing.aws.util import get_lambda_logs, is_aws_cloud
3636
from localstack.utils import testutil
3737
from localstack.utils.aws import aws_stack
3838
from localstack.utils.aws.client import SigningHttpClient
@@ -84,6 +84,9 @@
8484

8585
LOG = logging.getLogger(__name__)
8686

87+
# URL of public HTTP echo server, used primarily for AWS parity/snapshot testing
88+
PUBLIC_HTTP_ECHO_SERVER_URL = "http://httpbin.org"
89+
8790

8891
def _client(service, region_name=None, aws_access_key_id=None, *, additional_config=None):
8992
config = botocore.config.Config()
@@ -2003,13 +2006,16 @@ def factory(**kwargs):
20032006
LOG.debug(f"Error cleaning up AppSync API: {api}, {e}")
20042007

20052008

2006-
@pytest.fixture(scope="function")
2009+
@pytest.fixture
20072010
def echo_http_server(httpserver: HTTPServer):
2011+
"""Spins up a local HTTP echo server and returns the endpoint URL"""
2012+
20082013
def _echo(request: Request) -> Response:
20092014
result = {
20102015
"data": request.data or "{}",
20112016
"headers": dict(request.headers),
2012-
"request_url": request.url,
2017+
"url": request.url,
2018+
"method": request.method,
20132019
}
20142020
response_body = json.dumps(json_safe(result))
20152021
return Response(response_body, status=200)
@@ -2018,3 +2024,14 @@ def _echo(request: Request) -> Response:
20182024
http_endpoint = httpserver.url_for("/")
20192025

20202026
return http_endpoint
2027+
2028+
2029+
@pytest.fixture
2030+
def echo_http_server_post(echo_http_server):
2031+
"""
2032+
Returns an HTTP echo server URL for POST requests that work both locally and for parity tests (against real AWS)
+
"""
2034+
if is_aws_cloud():
2035+
return f"{PUBLIC_HTTP_ECHO_SERVER_URL}/post"
2036+
2037+
return f"{echo_http_server}/post"

tests/integration/cloudformation/resources/test_apigateway.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ def test_cfn_apigateway_aws_integration(
100100

101101

102102
@pytest.mark.aws_validated
103-
def test_cfn_apigateway_swagger_import(deploy_cfn_template, apigateway_client, echo_http_server):
103+
def test_cfn_apigateway_swagger_import(
104+
deploy_cfn_template, apigateway_client, echo_http_server_post
105+
):
104106
api_name = f"rest-api-{short_uid()}"
105107
deploy_cfn_template(
106108
template=TEST_TEMPLATE_1,
107-
parameters={"ApiName": api_name, "IntegrationUri": f"{echo_http_server}/post"},
109+
parameters={"ApiName": api_name, "IntegrationUri": echo_http_server_post},
108110
)
109111

110112
# get API details
@@ -120,7 +122,7 @@ def test_cfn_apigateway_swagger_import(deploy_cfn_template, apigateway_client, e
120122
assert result.ok
121123
content = json.loads(to_str(result.content))
122124
assert content["data"] == "test 123"
123-
assert content["request_url"].endswith("/post")
125+
assert content["url"].endswith("/post")
124126

125127

126128
@pytest.mark.only_localstack

0 commit comments

Comments
 (0)
0