From: Tom Christie Date: Wed, 4 Sep 2019 14:06:14 +0000 (+0100) Subject: Add tests for dropping Content-Length headers X-Git-Tag: 0.7.3~21^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4336550d227411b2b2029a4ef57e7587ea633eaf;p=thirdparty%2Fhttpx.git Add tests for dropping Content-Length headers --- diff --git a/tests/client/test_redirects.py b/tests/client/test_redirects.py index 7daf6c80..15e2c6a8 100644 --- a/tests/client/test_redirects.py +++ b/tests/client/test_redirects.py @@ -85,9 +85,17 @@ class MockDispatch(AsyncDispatcher): codes.PERMANENT_REDIRECT, headers=headers, request=request ) + elif request.url.path == "/redirect_no_body": + await request.read() + headers = {"location": "/redirect_body_target"} + return AsyncResponse( + codes.SEE_OTHER, headers=headers, request=request + ) + elif request.url.path == "/redirect_body_target": content = await request.read() - body = json.dumps({"body": content.decode()}).encode() + headers = dict(request.headers.items()) + body = json.dumps({"body": content.decode(), "headers": headers}).encode() return AsyncResponse(codes.OK, content=body, request=request) elif request.url.path == "/cross_subdomain": @@ -243,12 +251,29 @@ async def test_same_domain_redirect(backend): async def test_body_redirect(backend): + """ + A 308 redirect should preserve the request body. + """ client = AsyncClient(dispatch=MockDispatch(), backend=backend) url = "https://example.org/redirect_body" data = b"Example request body" response = await client.post(url, data=data) assert response.url == URL("https://example.org/redirect_body_target") - assert response.json() == {"body": "Example request body"} + assert response.json()["body"] == "Example request body" + assert "content-length" in response.json()["headers"] + + +async def test_no_body_redirect(backend): + """ + A 303 redirect should remove the request body. + """ + client = AsyncClient(dispatch=MockDispatch(), backend=backend) + url = "https://example.org/redirect_no_body" + data = b"Example request body" + response = await client.post(url, data=data) + assert response.url == URL("https://example.org/redirect_body_target") + assert response.json()["body"] == "" + assert "content-length" not in response.json()["headers"] async def test_cannot_redirect_streaming_body(backend):