test_event_hooks.py
This file
Path
tests/client/test_event_hooks.py
224 lines · 6.9 KiB · Python
Last changed by
Switch event hooks to also run on redirects. (#1806)Tom Christiebefore adoption2d9c358
History of this fileTake it
Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 1–26why, one step back1import pytest23import 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]}26Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
lines 27–29why, one step back27 with httpx.Client(28 event_hooks=event_hooks, transport=httpx.MockTransport(app)29 ) as http:Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 30–56why, one step back30 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]}56Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
lines 57–59why, one step back57 with httpx.Client(58 event_hooks=event_hooks, transport=httpx.MockTransport(app)59 ) as http:Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 60–78why, one step back60 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(Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
line 79why, one step back79 event_hooks=event_hooks, transport=httpx.MockTransport(app)Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 80–109why, one step back80 ) 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(Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
line 110why, one step back110 event_hooks=event_hooks, transport=httpx.MockTransport(app)Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 111–119why, one step back111 ) 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 back120 A redirect request should trigger additional 'request' and 'response' event hooks.Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 121–132why, one step back121 """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]}132Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
lines 133–135why, one step back133 with httpx.Client(134 event_hooks=event_hooks, transport=httpx.MockTransport(app)135 ) as http:Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 136–151why, one step back136 http.get("http://127.0.0.1:8000/redirect", auth=("username", "password"))137138 assert events == [139 {140 "event": "request",141 "headers": {142 "host": "127.0.0.1:8000",143 "user-agent": f"python-httpx/{httpx.__version__}",144 "accept": "*/*",145 "accept-encoding": "gzip, deflate, br",146 "connection": "keep-alive",147 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",148 },149 },150 {151 "event": "response",Switch event hooks to also run on redirects. (#1806)Tom Christiebefore adoption2d9c358
lines 152–166why, one step back152 "headers": {"location": "/", "server": "testserver"},153 },154 {155 "event": "request",156 "headers": {157 "host": "127.0.0.1:8000",158 "user-agent": f"python-httpx/{httpx.__version__}",159 "accept": "*/*",160 "accept-encoding": "gzip, deflate, br",161 "connection": "keep-alive",162 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",163 },164 },165 {166 "event": "response",Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 167–174why, one step back167 "headers": {"server": "testserver"},168 },169 ]170171172@pytest.mark.usefixtures("async_environment")173async def test_async_event_hooks_with_redirect():174 """Switch event hooks to also run on redirects. (#1806)Tom Christiebefore adoption2d9c358
line 175why, one step back175 A redirect request should trigger additional 'request' and 'response' event hooks.Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 176–188why, one step back176 """177178 events = []179180 async def on_request(request):181 events.append({"event": "request", "headers": dict(request.headers)})182183 async def on_response(response):184 events.append({"event": "response", "headers": dict(response.headers)})185186 event_hooks = {"request": [on_request], "response": [on_response]}187188 async with httpx.AsyncClient(Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
line 189why, one step back189 event_hooks=event_hooks, transport=httpx.MockTransport(app)Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 190–193why, one step back190 ) as http:191 await http.get("http://127.0.0.1:8000/redirect", auth=("username", "password"))192193 assert events == [Switch event hooks to also run on redirects. (#1806)Tom Christiebefore adoption2d9c358
lines 194–208why, one step back194 {195 "event": "request",196 "headers": {197 "host": "127.0.0.1:8000",198 "user-agent": f"python-httpx/{httpx.__version__}",199 "accept": "*/*",200 "accept-encoding": "gzip, deflate, br",201 "connection": "keep-alive",202 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",203 },204 },205 {206 "event": "response",207 "headers": {"location": "/", "server": "testserver"},208 },Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 209–224why, one step back209 {210 "event": "request",211 "headers": {212 "host": "127.0.0.1:8000",213 "user-agent": f"python-httpx/{httpx.__version__}",214 "accept": "*/*",215 "accept-encoding": "gzip, deflate, br",216 "connection": "keep-alive",217 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",218 },219 },220 {221 "event": "response",222 "headers": {"server": "testserver"},223 },224 ]