test_auth.py
This file
Path
tests/test_auth.py
103 lines · 3.6 KiB · Python
Last changed by
reuse the digest auth state to avoid unnecessary requests (#2463)rettierbefore adoption8327e13
History of this fileTake it
The walk stopped after 200 commits: lines older than 67c2970 point at it.
Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970
lines 1–5why, one step back1"""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 back6from urllib.request import parse_keqv_list7Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970
lines 8–60why, one step back8import pytest910import 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(57 content=b"Auth required", status_code=401, headers=headers58 )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–98why, one step back6162 # 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(82 content=b"Auth required", status_code=401, headers=headers83 )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 EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970
lines 99–103why, one step back99100 # No other requests are made.101 response = httpx.Response(content=b"Hello, world!", status_code=200)102 with pytest.raises(StopIteration):103 flow.send(response)