Skip to content
Myr

test_event_hooks.py

This file

Path

tests/client/test_event_hooks.py

228 lines · 7.0 KiB · Python

Last changed by

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

History of this file

Take it

The walk stopped after 200 commits: lines older than 69409bb point at it.

Switch to `USE_CLIENT_DEFAULT` instead of `UNSET`. (#1634)Tom Christiebefore adoption69409bb

lines 1–119why, one step back
1import pytest
23import httpx456def app(request: httpx.Request) -> httpx.Response:7 if request.url.path == "/redirect":8 return httpx.Response(303, headers={"server": "testserver", "location": "/"})9 elif request.url.path.startswith("/status/"):10 status_code = int(request.url.path[-3:])11 return httpx.Response(status_code, headers={"server": "testserver"})1213 return httpx.Response(200, headers={"server": "testserver"})141516def test_event_hooks():17 events = []1819 def on_request(request):20 events.append({"event": "request", "headers": dict(request.headers)})2122 def on_response(response):23 events.append({"event": "response", "headers": dict(response.headers)})2425 event_hooks = {"request": [on_request], "response": [on_response]}2627 with httpx.Client(28 event_hooks=event_hooks, transport=httpx.MockTransport(app)29 ) as http:30 http.get("http://127.0.0.1:8000/", auth=("username", "password"))3132 assert events == [33 {34 "event": "request",35 "headers": {36 "host": "127.0.0.1:8000",37 "user-agent": f"python-httpx/{httpx.__version__}",38 "accept": "*/*",39 "accept-encoding": "gzip, deflate, br",40 "connection": "keep-alive",41 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",42 },43 },44 {45 "event": "response",46 "headers": {"server": "testserver"},47 },48 ]495051def test_event_hooks_raising_exception(server):52 def raise_on_4xx_5xx(response):53 response.raise_for_status()5455 event_hooks = {"response": [raise_on_4xx_5xx]}5657 with httpx.Client(58 event_hooks=event_hooks, transport=httpx.MockTransport(app)59 ) as http:60 try:61 http.get("http://127.0.0.1:8000/status/400")62 except httpx.HTTPStatusError as exc:63 assert exc.response.is_closed646566@pytest.mark.usefixtures("async_environment")67async def test_async_event_hooks():68 events = []6970 async def on_request(request):71 events.append({"event": "request", "headers": dict(request.headers)})7273 async def on_response(response):74 events.append({"event": "response", "headers": dict(response.headers)})7576 event_hooks = {"request": [on_request], "response": [on_response]}7778 async with httpx.AsyncClient(79 event_hooks=event_hooks, transport=httpx.MockTransport(app)80 ) as http:81 await http.get("http://127.0.0.1:8000/", auth=("username", "password"))8283 assert events == [84 {85 "event": "request",86 "headers": {87 "host": "127.0.0.1:8000",88 "user-agent": f"python-httpx/{httpx.__version__}",89 "accept": "*/*",90 "accept-encoding": "gzip, deflate, br",91 "connection": "keep-alive",92 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",93 },94 },95 {96 "event": "response",97 "headers": {"server": "testserver"},98 },99 ]100101102@pytest.mark.usefixtures("async_environment")103async def test_async_event_hooks_raising_exception():104 async def raise_on_4xx_5xx(response):105 response.raise_for_status()106107 event_hooks = {"response": [raise_on_4xx_5xx]}108109 async with httpx.AsyncClient(110 event_hooks=event_hooks, transport=httpx.MockTransport(app)111 ) as http:112 try:113 await http.get("http://127.0.0.1:8000/status/400")114 except httpx.HTTPStatusError as exc:115 assert exc.response.is_closed116117118def test_event_hooks_with_redirect():119 """

Switch event hooks to also run on redirects. (#1806)Tom Christiebefore adoption2d9c358

line 120why, one step back
120 A redirect request should trigger additional 'request' and 'response' event hooks.

Switch to `USE_CLIENT_DEFAULT` instead of `UNSET`. (#1634)Tom Christiebefore adoption69409bb

lines 121–133why, one step back
121 """
122123 events = []124125 def on_request(request):126 events.append({"event": "request", "headers": dict(request.headers)})127128 def on_response(response):129 events.append({"event": "response", "headers": dict(response.headers)})130131 event_hooks = {"request": [on_request], "response": [on_response]}132133 with httpx.Client(

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 134–136why, one step back
134 event_hooks=event_hooks,
135 transport=httpx.MockTransport(app),136 follow_redirects=True,

Switch to `USE_CLIENT_DEFAULT` instead of `UNSET`. (#1634)Tom Christiebefore adoption69409bb

lines 137–153why, one step back
137 ) as http:
138 http.get("http://127.0.0.1:8000/redirect", auth=("username", "password"))139140 assert events == [141 {142 "event": "request",143 "headers": {144 "host": "127.0.0.1:8000",145 "user-agent": f"python-httpx/{httpx.__version__}",146 "accept": "*/*",147 "accept-encoding": "gzip, deflate, br",148 "connection": "keep-alive",149 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",150 },151 },152 {153 "event": "response",

Switch event hooks to also run on redirects. (#1806)Tom Christiebefore adoption2d9c358

lines 154–168why, one step back
154 "headers": {"location": "/", "server": "testserver"},
155 },156 {157 "event": "request",158 "headers": {159 "host": "127.0.0.1:8000",160 "user-agent": f"python-httpx/{httpx.__version__}",161 "accept": "*/*",162 "accept-encoding": "gzip, deflate, br",163 "connection": "keep-alive",164 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",165 },166 },167 {168 "event": "response",

Switch to `USE_CLIENT_DEFAULT` instead of `UNSET`. (#1634)Tom Christiebefore adoption69409bb

lines 169–176why, one step back
169 "headers": {"server": "testserver"},
170 },171 ]172173174@pytest.mark.usefixtures("async_environment")175async def test_async_event_hooks_with_redirect():176 """

Switch event hooks to also run on redirects. (#1806)Tom Christiebefore adoption2d9c358

line 177why, one step back
177 A redirect request should trigger additional 'request' and 'response' event hooks.

Switch to `USE_CLIENT_DEFAULT` instead of `UNSET`. (#1634)Tom Christiebefore adoption69409bb

lines 178–190why, one step back
178 """
179180 events = []181182 async def on_request(request):183 events.append({"event": "request", "headers": dict(request.headers)})184185 async def on_response(response):186 events.append({"event": "response", "headers": dict(response.headers)})187188 event_hooks = {"request": [on_request], "response": [on_response]}189190 async with httpx.AsyncClient(

Switch follow redirects default (#1808)Tom Christiebefore adoption47266d7

lines 191–193why, one step back
191 event_hooks=event_hooks,
192 transport=httpx.MockTransport(app),193 follow_redirects=True,

Switch to `USE_CLIENT_DEFAULT` instead of `UNSET`. (#1634)Tom Christiebefore adoption69409bb

lines 194–197why, one step back
194 ) as http:
195 await http.get("http://127.0.0.1:8000/redirect", auth=("username", "password"))196197 assert events == [

Switch event hooks to also run on redirects. (#1806)Tom Christiebefore adoption2d9c358

lines 198–212why, one step back
198 {
199 "event": "request",200 "headers": {201 "host": "127.0.0.1:8000",202 "user-agent": f"python-httpx/{httpx.__version__}",203 "accept": "*/*",204 "accept-encoding": "gzip, deflate, br",205 "connection": "keep-alive",206 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",207 },208 },209 {210 "event": "response",211 "headers": {"location": "/", "server": "testserver"},212 },

Switch to `USE_CLIENT_DEFAULT` instead of `UNSET`. (#1634)Tom Christiebefore adoption69409bb

lines 213–228why, one step back
213 {
214 "event": "request",215 "headers": {216 "host": "127.0.0.1:8000",217 "user-agent": f"python-httpx/{httpx.__version__}",218 "accept": "*/*",219 "accept-encoding": "gzip, deflate, br",220 "connection": "keep-alive",221 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",222 },223 },224 {225 "event": "response",226 "headers": {"server": "testserver"},227 },228 ]