Skip to content
Myr

test_auth.py

This file

Path

tests/client/test_auth.py

72 lines · 1.8 KiB · Python

Last changed by

http3 (#86)Tom Christiebefore adoptionc9747aa

History of this file

Take it

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 1–4why, one step back
1import json
23import pytest4

http3 (#86)Tom Christiebefore adoptionc9747aa

line 5why, one step back
5from http3 import (

Auth (#65)Tom Christiebefore adoption5afa7dd

line 6why, one step back
6 URL,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 7–9why, one step back
7 AsyncDispatcher,
8 AsyncRequest,9 AsyncResponse,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 10why, one step back
10 CertTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 11why, one step back
11 Client,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 12–13why, one step back
12 TimeoutTypes,
13 VerifyTypes,

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 14–16why, one step back
14)
1516

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 17why, one step back
17class MockDispatch(AsyncDispatcher):

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 18–19why, one step back
18 async def send(
19 self,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 20why, one step back
20 request: AsyncRequest,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 21–23why, one step back
21 verify: VerifyTypes = None,
22 cert: CertTypes = None,23 timeout: TimeoutTypes = None,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 24why, one step back
24 ) -> AsyncResponse:

Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885

line 25why, one step back
25 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 26why, one step back
26 return AsyncResponse(200, content=body, request=request)

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 27–30why, one step back
27
2829def test_basic_auth():30 url = "https://example.org/"

Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885

line 31why, one step back
31 auth = ("tomchristie", "password123")

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 32–36why, one step back
32
33 with Client(dispatch=MockDispatch()) as client:34 response = client.get(url, auth=auth)3536 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 37why, one step back
37 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 38–46why, one step back
38
3940def test_basic_auth_in_url():41 url = "https://tomchristie:password123@example.org/"4243 with Client(dispatch=MockDispatch()) as client:44 response = client.get(url)4546 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 47why, one step back
47 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 48–51why, one step back
48
4950def test_basic_auth_on_session():51 url = "https://example.org/"

Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885

line 52why, one step back
52 auth = ("tomchristie", "password123")

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 53–57why, one step back
53
54 with Client(dispatch=MockDispatch(), auth=auth) as client:55 response = client.get(url)5657 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 58why, one step back
58 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 59–64why, one step back
59
6061def test_custom_auth():62 url = "https://example.org/"6364 def auth(request):

Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885

line 65why, one step back
65 request.headers["Authorization"] = "Token 123"

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 66–71why, one step back
66 return request
6768 with Client(dispatch=MockDispatch()) as client:69 response = client.get(url, auth=auth)7071 assert response.status_code == 200

Add response.json()Tom Christiebefore adoption2116d44

line 72why, one step back
72 assert response.json() == {"auth": "Token 123"}