Skip to content
Myr

test_auth.py

This file

Path

tests/test_auth.py

142 lines · 4.9 KiB · Python

Last changed by

Add cookies to the retried request when performing digest authentication. (#2846)Musale Martinbefore adoption88e8431

History of this file

Take it

The walk stopped after 200 commits: lines older than 8752e26 point at it.

Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)Tom Christiebefore adoption8752e26

lines 1–5why, one step back
1"""
2Unit tests for auth classes.34Integration tests also exist in tests/client/test_auth.py5"""

reuse the digest auth state to avoid unnecessary requests (#2463)rettierbefore adoption8327e13

lines 6–7why, one step back
6from urllib.request import parse_keqv_list
7

Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)Tom Christiebefore adoption8752e26

lines 8–56why, one step back
8import pytest
910import httpx111213def test_basic_auth():14 auth = httpx.BasicAuth(username="user", password="pass")15 request = httpx.Request("GET", "https://www.example.com")1617 # The initial request should include a basic auth header.18 flow = auth.sync_auth_flow(request)19 request = next(flow)20 assert request.headers["Authorization"].startswith("Basic")2122 # No other requests are made.23 response = httpx.Response(content=b"Hello, world!", status_code=200)24 with pytest.raises(StopIteration):25 flow.send(response)262728def test_digest_auth_with_200():29 auth = httpx.DigestAuth(username="user", password="pass")30 request = httpx.Request("GET", "https://www.example.com")3132 # The initial request should not include an auth header.33 flow = auth.sync_auth_flow(request)34 request = next(flow)35 assert "Authorization" not in request.headers3637 # If a 200 response is returned, then no other requests are made.38 response = httpx.Response(content=b"Hello, world!", status_code=200)39 with pytest.raises(StopIteration):40 flow.send(response)414243def test_digest_auth_with_401():44 auth = httpx.DigestAuth(username="user", password="pass")45 request = httpx.Request("GET", "https://www.example.com")4647 # The initial request should not include an auth header.48 flow = auth.sync_auth_flow(request)49 request = next(flow)50 assert "Authorization" not in request.headers5152 # If a 401 response is returned, then a digest auth request is made.53 headers = {54 "WWW-Authenticate": 'Digest realm="...", qop="auth", nonce="...", opaque="..."'55 }56 response = httpx.Response(

Add cookies to the retried request when performing digest authentication. (#2846)Musale Martinbefore adoption88e8431

line 57why, one step back
57 content=b"Auth required", status_code=401, headers=headers, request=request

Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)Tom Christiebefore adoption8752e26

lines 58–60why, one step back
58 )
59 request = flow.send(response)60 assert request.headers["Authorization"].startswith("Digest")

reuse the digest auth state to avoid unnecessary requests (#2463)rettierbefore adoption8327e13

lines 61–81why, one step back
61
62 # No other requests are made.63 response = httpx.Response(content=b"Hello, world!", status_code=200)64 with pytest.raises(StopIteration):65 flow.send(response)666768def test_digest_auth_with_401_nonce_counting():69 auth = httpx.DigestAuth(username="user", password="pass")70 request = httpx.Request("GET", "https://www.example.com")7172 # The initial request should not include an auth header.73 flow = auth.sync_auth_flow(request)74 request = next(flow)75 assert "Authorization" not in request.headers7677 # If a 401 response is returned, then a digest auth request is made.78 headers = {79 "WWW-Authenticate": 'Digest realm="...", qop="auth", nonce="...", opaque="..."'80 }81 response = httpx.Response(

Add cookies to the retried request when performing digest authentication. (#2846)Musale Martinbefore adoption88e8431

line 82why, one step back
82 content=b"Auth required", status_code=401, headers=headers, request=request

reuse the digest auth state to avoid unnecessary requests (#2463)rettierbefore adoption8327e13

lines 83–98why, one step back
83 )
84 first_request = flow.send(response)85 assert first_request.headers["Authorization"].startswith("Digest")8687 # Each subsequent request contains the digest header by default...88 request = httpx.Request("GET", "https://www.example.com")89 flow = auth.sync_auth_flow(request)90 second_request = next(flow)91 assert second_request.headers["Authorization"].startswith("Digest")9293 # ... and the client nonce count (nc) is increased94 first_nc = parse_keqv_list(first_request.headers["Authorization"].split(", "))["nc"]95 second_nc = parse_keqv_list(second_request.headers["Authorization"].split(", "))[96 "nc"97 ]98 assert int(first_nc, 16) + 1 == int(second_nc, 16)

Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)Tom Christiebefore adoption8752e26

lines 99–101why, one step back
99
100 # No other requests are made.101 response = httpx.Response(content=b"Hello, world!", status_code=200)

Add cookies to the retried request when performing digest authentication. (#2846)Musale Martinbefore adoption88e8431

lines 102–140why, one step back
102 with pytest.raises(StopIteration):
103 flow.send(response)104105106def set_cookies(request: httpx.Request) -> httpx.Response:107 headers = {108 "Set-Cookie": "session=.session_value...",109 "WWW-Authenticate": 'Digest realm="...", qop="auth", nonce="...", opaque="..."',110 }111 if request.url.path == "/auth":112 return httpx.Response(113 content=b"Auth required", status_code=401, headers=headers114 )115 else:116 raise NotImplementedError() # pragma: no cover117118119def test_digest_auth_setting_cookie_in_request():120 url = "https://www.example.com/auth"121 client = httpx.Client(transport=httpx.MockTransport(set_cookies))122 request = client.build_request("GET", url)123124 auth = httpx.DigestAuth(username="user", password="pass")125 flow = auth.sync_auth_flow(request)126 request = next(flow)127 assert "Authorization" not in request.headers128129 response = client.get(url)130 assert len(response.cookies) > 0131 assert response.cookies["session"] == ".session_value..."132133 request = flow.send(response)134 assert request.headers["Authorization"].startswith("Digest")135 assert request.headers["Cookie"] == "session=.session_value..."136137 # No other requests are made.138 response = httpx.Response(139 content=b"Hello, world!", status_code=200, request=request140 )

Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)Tom Christiebefore adoption8752e26

lines 141–142why, one step back
141 with pytest.raises(StopIteration):
142 flow.send(response)