Skip to content
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

refactor(test): make CORS test consistent with expected behavior #4882

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor Author

Choose a reason for hiding this comment

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

low hanging fruit to test the UserWarnings and reduce noise in the pytest output

Copy link
Contributor

Choose a reason for hiding this comment

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

Great attention to details @Wurstnase! Thank you so much!! ❤️

Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def test_cors():
def with_cors() -> Response:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we can remove cors=True here. It's the default behavior when cors is set in ApiGatewayResolver. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I will check and test some other places to.

return Response(200, content_types.TEXT_HTML, "test")

@app.get("/without-cors")
@app.get("/without-cors", cors=False)
def without_cors() -> Response:
return Response(200, content_types.TEXT_HTML, "test")

Expand All @@ -350,17 +350,17 @@ def handler(event, context):
assert headers["Access-Control-Allow-Headers"] == [",".join(sorted(CORSConfig._REQUIRED_HEADERS))]

# THEN for routes without cors flag return no cors headers
mock_event = {"path": "/my/request", "httpMethod": "GET"}
mock_event = {"path": "/without-cors", "httpMethod": "GET"}
result = handler(mock_event, None)
assert "Access-Control-Allow-Origin" not in result["multiValueHeaders"]


def test_cors_no_request_origin():
# GIVEN a function with cors=True
# GIVEN a function
# AND http method set to GET
app = ApiGatewayResolver()
app = ApiGatewayResolver(cors=CORSConfig())

@app.get("/my/path", cors=True)
@app.get("/my/path")
def with_cors() -> Response:
return Response(200, content_types.TEXT_HTML, "test")

Expand All @@ -381,7 +381,7 @@ def handler(event, context):


def test_cors_allow_all_request_origins():
# GIVEN a function with cors=True
# GIVEN a function
# AND http method set to GET
app = ApiGatewayResolver(
cors=CORSConfig(
Expand All @@ -390,11 +390,11 @@ def test_cors_allow_all_request_origins():
),
)

@app.get("/my/path", cors=True)
@app.get("/my/path")
def with_cors() -> Response:
return Response(200, content_types.TEXT_HTML, "test")

@app.get("/without-cors")
@app.get("/without-cors", cors=False)
def without_cors() -> Response:
return Response(200, content_types.TEXT_HTML, "test")

Expand All @@ -413,7 +413,7 @@ def handler(event, context):
assert headers["Access-Control-Allow-Headers"] == [",".join(sorted(CORSConfig._REQUIRED_HEADERS))]

# THEN for routes without cors flag return no cors headers
mock_event = {"path": "/my/request", "httpMethod": "GET"}
mock_event = {"path": "/without-cors", "httpMethod": "GET"}
result = handler(mock_event, None)
assert "Access-Control-Allow-Origin" not in result["multiValueHeaders"]

Expand Down