Fix redirect cookie behavior (#529)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Fix redirect cookie behavior * Drop flake8-comprehensions * Add redirect cookie tests
authored by Tom Christie · committed by GitHub · parent 1a32cf0 · Browse files · 4 files·+74 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+74 added−10 removed
- tests:+68 added−0 removed
- other:+6 added−10 removed
Provenance
- Commit
5aca0c0- Tree
e061991- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
1a32cf0
4 files changed+74 added−10 removed
modified:httpx/middleware/redirect.py+5 added-1 removed
49 unmodified lines50 url = self.redirect_url(request, response)51 headers = self.redirect_headers(request, url, method) # TODO: merge headers?52 content = self.redirect_content(request, method)53 cookies = Cookies(self.cookies)54- cookies.update(request.cookies)54 return AsyncRequest(55 method=method, url=url, headers=headers, data=content, cookies=cookies56 )57 55 unmodified lines113 # If we've switch to a 'GET' request, then strip any headers which114 # are only relevant to the request body.115 headers.pop("Content-Length", None)116 headers.pop("Transfer-Encoding", None)117+118+ # We should use the client cookie store to determine any cookie header,119+ # rather than whatever was on the original outgoing request.120+ headers.pop("Cookie", None)121+122 return headers123 124 def redirect_content(self, request: AsyncRequest, method: str) -> bytes:125 """7 unmodified linesmodified:tests/client/test_redirects.py+68 added
288 unmodified lines289 client = AsyncClient(dispatch=MockDispatch(), backend=backend)290 url = "https://example.com/cross_subdomain"291 response = await client.get(url)292 assert response.url == URL("https://www.example.org/cross_subdomain")293+294+295+class MockCookieDispatch(AsyncDispatcher):296+ async def send(297+ self,298+ request: AsyncRequest,299+ verify: VerifyTypes = None,300+ cert: CertTypes = None,301+ timeout: TimeoutTypes = None,302+ ) -> AsyncResponse:303+ if request.url.path == "/":304+ if "cookie" in request.headers:305+ content = b"Logged in"306+ else:307+ content = b"Not logged in"308+ return AsyncResponse(codes.OK, content=content, request=request)309+310+ elif request.url.path == "/login":311+ status_code = codes.SEE_OTHER312+ headers = {313+ "location": "/",314+ "set-cookie": (315+ "session=eyJ1c2VybmFtZSI6ICJ0b21; path=/; Max-Age=1209600; "316+ "httponly; samesite=lax"317+ ),318+ }319+320+ return AsyncResponse(status_code, headers=headers, request=request)321+322+ elif request.url.path == "/logout":323+ status_code = codes.SEE_OTHER324+ headers = {325+ "location": "/",326+ "set-cookie": (327+ "session=null; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; "328+ "httponly; samesite=lax"329+ ),330+ }331+ return AsyncResponse(status_code, headers=headers, request=request)332+333+334+async def test_redirect_cookie_behavior(backend):335+ client = AsyncClient(dispatch=MockCookieDispatch(), backend=backend)336+337+ # The client is not logged in.338+ response = await client.get("https://example.com/")339+ assert response.url == "https://example.com/"340+ assert response.text == "Not logged in"341+342+ # Login redirects to the homepage, setting a session cookie.343+ response = await client.post("https://example.com/login")344+ assert response.url == "https://example.com/"345+ assert response.text == "Logged in"346+347+ # The client is logged in.348+ response = await client.get("https://example.com/")349+ assert response.url == "https://example.com/"350+ assert response.text == "Logged in"351+352+ # Logout redirects to the homepage, expiring the session cookie.353+ response = await client.post("https://example.com/logout")354+ assert response.url == "https://example.com/"355+ assert response.text == "Not logged in"356+357+ # The client is not logged in.358+ response = await client.get("https://example.com/")359+ assert response.url == "https://example.com/"360+ assert response.text == "Not logged in"modified:noxfile.py+1 added-8 removed
22 unmodified lines23 24 @nox.session25 def check(session):26 session.install(27- "--upgrade",28- "black",29- "flake8",30- "flake8-bugbear",31- "flake8-comprehensions",32- "flake8-pie",33- "isort",34- "mypy",27+ "--upgrade", "black", "flake8", "flake8-bugbear", "flake8-pie", "isort", "mypy"28 )29 30 session.run("black", "--check", "--diff", "--target-version=py36", *source_files)31 session.run("flake8", *source_files)24 unmodified linesmodified:test-requirements.txt-1 removed
4 unmodified lines5 6 cryptography7 flake88 flake8-bugbear9-flake8-comprehensions9 flake8-pie10 isort11 mypy12 pytest6 unmodified lines