redirect_scheme = {"http": "https", "ws": "wss"}[url.scheme]
netloc = url.hostname if url.port in (80, 443) else url.netloc
url = url.replace(scheme=redirect_scheme, netloc=netloc)
- response = RedirectResponse(url, status_code=301)
+ response = RedirectResponse(url, status_code=308)
await response(scope, receive, send)
else:
await self.app(scope, receive, send)
class RedirectResponse(Response):
def __init__(
- self, url: typing.Union[str, URL], status_code: int = 302, headers: dict = None
+ self, url: typing.Union[str, URL], status_code: int = 307, headers: dict = None
) -> None:
super().__init__(content=b"", status_code=status_code, headers=headers)
self.headers["location"] = quote_plus(str(url), safe=":/%#?&=@[]!$&'()*+,;")
client = TestClient(app)
response = client.get("/", allow_redirects=False)
- assert response.status_code == 301
+ assert response.status_code == 308
assert response.headers["location"] == "https://testserver/"
client = TestClient(app, base_url="http://testserver:80")
response = client.get("/", allow_redirects=False)
- assert response.status_code == 301
+ assert response.status_code == 308
assert response.headers["location"] == "https://testserver/"
client = TestClient(app, base_url="http://testserver:443")
response = client.get("/", allow_redirects=False)
- assert response.status_code == 301
+ assert response.status_code == 308
assert response.headers["location"] == "https://testserver/"
client = TestClient(app, base_url="http://testserver:123")
response = client.get("/", allow_redirects=False)
- assert response.status_code == 301
+ assert response.status_code == 308
assert response.headers["location"] == "https://testserver:123/"