Skip to content

Commit

Permalink
Fix request.url with full URI as SERVER_NAME
Browse files Browse the repository at this point in the history
  • Loading branch information
PromyLOPh committed May 25, 2022
1 parent c4da66b commit d41c478
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
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

0 comments on commit d41c478

Please sign in to comment.