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 18, 2022
1 parent c4da66b commit 5cfa763
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sanic/request.py
Expand Up @@ -619,8 +619,8 @@ def scheme(self) -> str:
:return: http|https|ws|wss or arbitrary value given by the headers.
:rtype: str
"""
if "//" in self.app.config.get("SERVER_NAME", ""):
return self.app.config.SERVER_NAME.split("//")[0]
if "://" in self.app.config.get("SERVER_NAME", ""):
return self.app.config.SERVER_NAME.split("://")[0]
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 5cfa763

Please sign in to comment.