Skip to content
Myr

Redirect tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by Tom Christie · parent e09fc37 · Browse files · 2 files+24 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−1 removed

  • tests:+23 added−1 removed
  • other:+1 added−0 removed

2 files+24 added−1 removed

modified:httpcore/__init__.py+1 added
11 unmodified lines
12 ReadTimeout,13 ResponseClosed,14 StreamConsumed,15 Timeout,16 TooManyRedirects17)18from .interfaces import Adapter19from .models import URL, Headers, Origin, Request, Response20from .status_codes import codes
4 unmodified lines
modified:tests/adapters/test_redirects.py+23 added-1 removed
1import pytest2from urllib.parse import parse_qs33from httpcore import Adapter, RedirectAdapter, Request, Response, codes4from httpcore import Adapter, RedirectAdapter, Request, Response, TooManyRedirects, codes567class MockDispatch(Adapter):8 def prepare_request(self, request: Request) -> None:
5 unmodified lines
14 codes.see_other, headers=[(b"location", b"https://example.org/")]15 )16 elif request.url.path == "/relative_redirect":17 return Response(codes.see_other, headers=[(b"location", b"/")])18 elif request.url.path == "/multiple_redirects":19 params = parse_qs(request.url.query)20 count = int(params.get("count", "0")[0])21 code = codes.see_other if count else codes.ok22 location = "/multiple_redirects?count=" + str(count - 1)23 headers = [(b"location", location.encode())] if count else []24 return Response(code, headers=headers)25 return Response(codes.ok, body=b"Hello, world!")262728@pytest.mark.asyncio
7 unmodified lines
36async def test_relative_redirect():37 client = RedirectAdapter(MockDispatch())38 response = await client.request("GET", "https://example.org/relative_redirect")39 assert response.status_code == codes.ok404142@pytest.mark.asyncio43async def test_multiple_redirects():44 client = RedirectAdapter(MockDispatch())45 response = await client.request("GET", "https://example.org/multiple_redirects?count=20")46 assert response.status_code == codes.ok474849@pytest.mark.asyncio50async def test_too_many_redirects():51 client = RedirectAdapter(MockDispatch())52 with pytest.raises(TooManyRedirects):53 await client.request("GET", "https://example.org/multiple_redirects?count=21")