Skip to content
Myr

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

4 files+74 added−10 removed

modified:httpx/middleware/redirect.py+5 added-1 removed
49 unmodified lines
50 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 lines
113 # 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)117118 # 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)121122 return headers123124 def redirect_content(self, request: AsyncRequest, method: str) -> bytes:125 """
7 unmodified lines
modified:tests/client/test_redirects.py+68 added
288 unmodified lines
289 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")293294295class 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)309310 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 }319320 return AsyncResponse(status_code, headers=headers, request=request)321322 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)332333334async def test_redirect_cookie_behavior(backend):335 client = AsyncClient(dispatch=MockCookieDispatch(), backend=backend)336337 # 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"341342 # 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"346347 # 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"351352 # 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"356357 # 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 lines
2324@nox.session25def 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 )2930 session.run("black", "--check", "--diff", "--target-version=py36", *source_files)31 session.run("flake8", *source_files)
24 unmodified lines
modified:test-requirements.txt-1 removed
4 unmodified lines
56cryptography7flake88flake8-bugbear9flake8-comprehensions9flake8-pie10isort11mypy12pytest
6 unmodified lines