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

Add HEAD to CORS ALL_METHODS list #1112

Merged
merged 4 commits into from Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion starlette/middleware/cors.py
Expand Up @@ -6,7 +6,7 @@
from starlette.responses import PlainTextResponse, Response
from starlette.types import ASGIApp, Message, Receive, Scope, Send

ALL_METHODS = ("DELETE", "GET", "OPTIONS", "PATCH", "POST", "PUT")
ALL_METHODS = ("DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT")
SAFELISTED_HEADERS = {"Accept", "Accept-Language", "Content-Language", "Content-Type"}


Expand Down
50 changes: 50 additions & 0 deletions tests/middleware/test_cors.py
Expand Up @@ -118,6 +118,56 @@ def homepage(request):
assert response.text == "Disallowed CORS origin, method, headers"


def test_cors_preflight_allow_all_methods():
app = Starlette()

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
)

@app.route("/")
def homepage(request):
pass # pragma: no cover

client = TestClient(app)

headers = {
"Origin": "https://example.org",
"Access-Control-Request-Method": "POST",
}

for method in ("DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"):
response = client.options("/", headers=headers)
assert response.status_code == 200
assert method in response.headers["access-control-allow-methods"]


def test_cors_allow_all_methods():
app = Starlette()

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
)

@app.route(
"/", methods=("delete", "get", "head", "options", "patch", "post", "put")
)
def homepage(request):
return PlainTextResponse("Homepage", status_code=200)

client = TestClient(app)

headers = {"Origin": "https://example.org"}

for method in ("delete", "get", "head", "options", "patch", "post", "put"):
response = getattr(client, method)("/", headers=headers, json={})
assert response.status_code / 100 == 2
jcwilson marked this conversation as resolved.
Show resolved Hide resolved
jcwilson marked this conversation as resolved.
Show resolved Hide resolved


def test_cors_allow_origin_regex():
app = Starlette()

Expand Down