Skip to content
Myr

Swap auth/redirects ordering (#1267)

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

* Internal refactoring to swap auth/redirects ordering * Test for auth with cross domain redirect

authored by Tom Christie · committed by GitHub · parent 016e4ee · Browse files · 2 files+43 added−31 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+43 added−31 removed

  • tests:+9 added−1 removed
  • other:+34 added−30 removed

2 files+43 added−31 removed

modified:httpx/_client.py+34 added-30 removed
723 unmodified lines
724 timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)725726 auth = self._build_request_auth(request, auth)727728 response = self._send_handling_redirects(729 request, auth=auth, timeout=timeout, allow_redirects=allow_redirects728 response = self._send_handling_auth(729 request,730 auth=auth,731 timeout=timeout,732 allow_redirects=allow_redirects,733 history=[],734 )735736 if not stream:737 try:
5 unmodified lines
743744 def _send_handling_redirects(745 self,746 request: Request,743 auth: Auth,747 timeout: Timeout,745 allow_redirects: bool = True,746 history: typing.List[Response] = None,748 allow_redirects: bool,749 history: typing.List[Response],750 ) -> Response:748 if history is None:749 history = []750751 while True:752 if len(history) > self.max_redirects:753 raise TooManyRedirects(754 "Exceeded maximum allowed redirects.", request=request755 )756757 response = self._send_handling_auth(758 request, auth=auth, timeout=timeout, history=history759 )757 response = self._send_single_request(request, timeout)758 response.history = list(history)759760 if not response.is_redirect:761 return response
6 unmodified lines
768 if not allow_redirects:769 response.call_next = functools.partial(770 self._send_handling_redirects,771 request=request,774 auth=auth,772 timeout=timeout,773 allow_redirects=False,774 history=history,775 )
1 unmodified line
777778 def _send_handling_auth(779 self,780 request: Request,784 history: typing.List[Response],781 auth: Auth,782 timeout: Timeout,783 allow_redirects: bool,784 history: typing.List[Response],785 ) -> Response:786 auth_flow = auth.sync_auth_flow(request)787 request = next(auth_flow)788789 while True:792 response = self._send_single_request(request, timeout)793790 response = self._send_handling_redirects(791 request,792 timeout=timeout,793 allow_redirects=allow_redirects,794 history=history,795 )796 try:797 next_request = auth_flow.send(response)798 except StopIteration:799 return response
547 unmodified lines
1347 timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)13481349 auth = self._build_request_auth(request, auth)13501349 response = await self._send_handling_redirects(1350 request, auth=auth, timeout=timeout, allow_redirects=allow_redirects1351 response = await self._send_handling_auth(1352 request,1353 auth=auth,1354 timeout=timeout,1355 allow_redirects=allow_redirects,1356 history=[],1357 )13581359 if not stream:1360 try:
5 unmodified lines
13661367 async def _send_handling_redirects(1368 self,1369 request: Request,1364 auth: Auth,1370 timeout: Timeout,1366 allow_redirects: bool = True,1367 history: typing.List[Response] = None,1371 allow_redirects: bool,1372 history: typing.List[Response],1373 ) -> Response:1369 if history is None:1370 history = []13711374 while True:1375 if len(history) > self.max_redirects:1376 raise TooManyRedirects(1377 "Exceeded maximum allowed redirects.", request=request1378 )13791378 response = await self._send_handling_auth(1379 request, auth=auth, timeout=timeout, history=history1380 )1380 response = await self._send_single_request(request, timeout)1381 response.history = list(history)13821383 if not response.is_redirect:1384 return response
6 unmodified lines
1391 if not allow_redirects:1392 response.call_next = functools.partial(1393 self._send_handling_redirects,1394 request=request,1395 auth=auth,1395 timeout=timeout,1396 allow_redirects=False,1397 history=history,1398 )
1 unmodified line
14001401 async def _send_handling_auth(1402 self,1403 request: Request,1405 history: typing.List[Response],1404 auth: Auth,1405 timeout: Timeout,1406 allow_redirects: bool,1407 history: typing.List[Response],1408 ) -> Response:1409 auth_flow = auth.async_auth_flow(request)1410 request = await auth_flow.__anext__()14111412 while True:1413 response = await self._send_single_request(request, timeout)14141413 response = await self._send_handling_redirects(1414 request,1415 timeout=timeout,1416 allow_redirects=allow_redirects,1417 history=history,1418 )1419 try:1420 next_request = await auth_flow.asend(response)1421 except StopAsyncIteration:1422 return response
358 unmodified lines
modified:tests/client/test_redirects.py+9 added-1 removed
321 unmodified lines
322 with pytest.raises(httpx.TooManyRedirects):323 client.get("https://example.org/redirect_loop")324325326def test_cross_domain_redirect():326def test_cross_domain_redirect_with_auth_header():327 client = httpx.Client(transport=SyncMockTransport())328 url = "https://example.com/cross_domain"329 headers = {"Authorization": "abc"}330 response = client.get(url, headers=headers)331 assert response.url == "https://example.org/cross_domain_target"332 assert "authorization" not in response.json()["headers"]333334335def test_cross_domain_redirect_with_auth():336 client = httpx.Client(transport=SyncMockTransport())337 url = "https://example.com/cross_domain"338 response = client.get(url, auth=("user", "pass"))339 assert response.url == "https://example.org/cross_domain_target"340 assert "authorization" not in response.json()["headers"]341342343def test_same_domain_redirect():344 client = httpx.Client(transport=SyncMockTransport())345 url = "https://example.org/cross_domain"346 headers = {"Authorization": "abc"}
142 unmodified lines