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

Fix request.url with full URI as SERVER_NAME #2458

Draft
wants to merge 3 commits into
base: 21.12LTS
Choose a base branch
from
Draft
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 sanic/request.py
Expand Up @@ -620,7 +620,7 @@ def scheme(self) -> str:
:rtype: str
"""
if "//" in self.app.config.get("SERVER_NAME", ""):
return self.app.config.SERVER_NAME.split("//")[0]
return self.app.config.SERVER_NAME.split("//")[0].rstrip(":")
if "proto" in self.forwarded:
return str(self.forwarded["proto"])

Expand Down
18 changes: 14 additions & 4 deletions tests/test_requests.py
Expand Up @@ -1917,16 +1917,26 @@ def handler(request):
return text("ok")

app.config.SERVER_NAME = "my-server" # This means default port
assert app.url_for("handler", _external=True) == "http://my-server/foo"
path = "/foo"
url = f"http://my-server{path}"
assert app.url_for("handler", _external=True) == url
request, response = app.test_client.get("/foo")
assert request.url_for("handler") == f"http://my-server/foo"
assert request.url_for("handler") == url
assert request.scheme == "http"
assert request.path == path
assert request.url == url

app.config.SERVER_NAME = "https://my-server/path"
request, response = app.test_client.get("/foo")
url = f"https://my-server/path/foo"
path = "/path/foo"
url = f"https://my-server{path}"
assert app.url_for("handler", _external=True) == url
assert request.url_for("handler") == url

assert request.scheme == "https"
# These two currently fail, because SERVER_NAME’s path is not
# taken into account.
#assert request.path == path
#assert request.url == url

def test_url_for_with_forwarded_request(app):
@app.get("/")
Expand Down