|  | 
|  | 1 | +import pytest | 
|  | 2 | +from requests import HTTPError, Request | 
|  | 3 | + | 
|  | 4 | +from tests.e2e.utils import data_fetcher | 
|  | 5 | + | 
|  | 6 | + | 
|  | 7 | +@pytest.fixture | 
|  | 8 | +def alb_basic_listener_endpoint(infrastructure: dict) -> str: | 
|  | 9 | + dns_name = infrastructure.get("ALBDnsName") | 
|  | 10 | + port = infrastructure.get("ALBBasicListenerPort", "") | 
|  | 11 | + return f"http://{dns_name}:{port}" | 
|  | 12 | + | 
|  | 13 | + | 
|  | 14 | +@pytest.fixture | 
|  | 15 | +def alb_multi_value_header_listener_endpoint(infrastructure: dict) -> str: | 
|  | 16 | + dns_name = infrastructure.get("ALBDnsName") | 
|  | 17 | + port = infrastructure.get("ALBMultiValueHeaderListenerPort", "") | 
|  | 18 | + return f"http://{dns_name}:{port}" | 
|  | 19 | + | 
|  | 20 | + | 
|  | 21 | +@pytest.fixture | 
|  | 22 | +def apigw_rest_endpoint(infrastructure: dict) -> str: | 
|  | 23 | + return infrastructure.get("APIGatewayRestUrl", "") | 
|  | 24 | + | 
|  | 25 | + | 
|  | 26 | +@pytest.fixture | 
|  | 27 | +def apigw_http_endpoint(infrastructure: dict) -> str: | 
|  | 28 | + return infrastructure.get("APIGatewayHTTPUrl", "") | 
|  | 29 | + | 
|  | 30 | + | 
|  | 31 | +@pytest.fixture | 
|  | 32 | +def lambda_function_url_endpoint(infrastructure: dict) -> str: | 
|  | 33 | + return infrastructure.get("LambdaFunctionUrl", "") | 
|  | 34 | + | 
|  | 35 | + | 
|  | 36 | +def test_api_gateway_rest_trailing_slash(apigw_rest_endpoint): | 
|  | 37 | + # GIVEN API URL ends in a trailing slash | 
|  | 38 | + url = f"{apigw_rest_endpoint}todos/" | 
|  | 39 | + body = "Hello World" | 
|  | 40 | + | 
|  | 41 | + # WHEN | 
|  | 42 | + response = data_fetcher.get_http_response( | 
|  | 43 | + Request( | 
|  | 44 | + method="POST", | 
|  | 45 | + url=url, | 
|  | 46 | + json={"body": body}, | 
|  | 47 | + ) | 
|  | 48 | + ) | 
|  | 49 | + | 
|  | 50 | + # THEN expect a HTTP 200 response | 
|  | 51 | + assert response.status_code == 200 | 
|  | 52 | + | 
|  | 53 | + | 
|  | 54 | +def test_api_gateway_http_trailing_slash(apigw_http_endpoint): | 
|  | 55 | + # GIVEN the URL for the API ends in a trailing slash API gateway should return a 404 | 
|  | 56 | + url = f"{apigw_http_endpoint}todos/" | 
|  | 57 | + body = "Hello World" | 
|  | 58 | + | 
|  | 59 | + # WHEN calling an invalid URL (with trailing slash) expect HTTPError exception from data_fetcher | 
|  | 60 | + with pytest.raises(HTTPError): | 
|  | 61 | + data_fetcher.get_http_response( | 
|  | 62 | + Request( | 
|  | 63 | + method="POST", | 
|  | 64 | + url=url, | 
|  | 65 | + json={"body": body}, | 
|  | 66 | + ) | 
|  | 67 | + ) | 
|  | 68 | + | 
|  | 69 | + | 
|  | 70 | +def test_lambda_function_url_trailing_slash(lambda_function_url_endpoint): | 
|  | 71 | + # GIVEN the URL for the API ends in a trailing slash it should behave as if there was not one | 
|  | 72 | + url = f"{lambda_function_url_endpoint}todos/" # the function url endpoint already has the trailing / | 
|  | 73 | + body = "Hello World" | 
|  | 74 | + | 
|  | 75 | + # WHEN calling an invalid URL (with trailing slash) expect HTTPError exception from data_fetcher | 
|  | 76 | + with pytest.raises(HTTPError): | 
|  | 77 | + data_fetcher.get_http_response( | 
|  | 78 | + Request( | 
|  | 79 | + method="POST", | 
|  | 80 | + url=url, | 
|  | 81 | + json={"body": body}, | 
|  | 82 | + ) | 
|  | 83 | + ) | 
|  | 84 | + | 
|  | 85 | + | 
|  | 86 | +def test_alb_url_trailing_slash(alb_multi_value_header_listener_endpoint): | 
|  | 87 | + # GIVEN url has a trailing slash - it should behave as if there was not one | 
|  | 88 | + url = f"{alb_multi_value_header_listener_endpoint}/todos/" | 
|  | 89 | + body = "Hello World" | 
|  | 90 | + | 
|  | 91 | + # WHEN calling an invalid URL (with trailing slash) expect HTTPError exception from data_fetcher | 
|  | 92 | + with pytest.raises(HTTPError): | 
|  | 93 | + data_fetcher.get_http_response( | 
|  | 94 | + Request( | 
|  | 95 | + method="POST", | 
|  | 96 | + url=url, | 
|  | 97 | + json={"body": body}, | 
|  | 98 | + ) | 
|  | 99 | + ) | 
0 commit comments