test_event_hooks.py
This file
Path
tests/client/test_event_hooks.py
189 lines · 5.8 KiB · Python
Last changed by
Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
History of this fileTake it
Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 1–3why, one step back1import pytest23import httpxVersion 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 4why, one step back4from tests.utils import MockTransportEvent hooks (#1246)Tom Christiebefore adoption54f7708
lines 5–75why, one step back567def app(request: httpx.Request) -> httpx.Response:8 if request.url.path == "/redirect":9 return httpx.Response(303, headers={"server": "testserver", "location": "/"})10 elif request.url.path.startswith("/status/"):11 status_code = int(request.url.path[-3:])12 return httpx.Response(status_code, headers={"server": "testserver"})1314 return httpx.Response(200, headers={"server": "testserver"})151617def test_event_hooks():18 events = []1920 def on_request(request):21 events.append({"event": "request", "headers": dict(request.headers)})2223 def on_response(response):24 events.append({"event": "response", "headers": dict(response.headers)})2526 event_hooks = {"request": [on_request], "response": [on_response]}2728 with httpx.Client(event_hooks=event_hooks, transport=MockTransport(app)) as http:29 http.get("http://127.0.0.1:8000/", auth=("username", "password"))3031 assert events == [32 {33 "event": "request",34 "headers": {35 "host": "127.0.0.1:8000",36 "user-agent": f"python-httpx/{httpx.__version__}",37 "accept": "*/*",38 "accept-encoding": "gzip, deflate, br",39 "connection": "keep-alive",40 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",41 },42 },43 {44 "event": "response",45 "headers": {"server": "testserver"},46 },47 ]484950def test_event_hooks_raising_exception(server):51 def raise_on_4xx_5xx(response):52 response.raise_for_status()5354 event_hooks = {"response": [raise_on_4xx_5xx]}5556 with httpx.Client(event_hooks=event_hooks, transport=MockTransport(app)) as http:57 try:58 http.get("http://127.0.0.1:8000/status/400")59 except httpx.HTTPStatusError as exc:60 assert exc.response.is_closed616263@pytest.mark.usefixtures("async_environment")64async def test_async_event_hooks():65 events = []6667 async def on_request(request):68 events.append({"event": "request", "headers": dict(request.headers)})6970 async def on_response(response):71 events.append({"event": "response", "headers": dict(response.headers)})7273 event_hooks = {"request": [on_request], "response": [on_response]}7475 async with httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 76why, one step back76 event_hooks=event_hooks, transport=MockTransport(app)Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 77–106why, one step back77 ) as http:78 await http.get("http://127.0.0.1:8000/", auth=("username", "password"))7980 assert events == [81 {82 "event": "request",83 "headers": {84 "host": "127.0.0.1:8000",85 "user-agent": f"python-httpx/{httpx.__version__}",86 "accept": "*/*",87 "accept-encoding": "gzip, deflate, br",88 "connection": "keep-alive",89 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",90 },91 },92 {93 "event": "response",94 "headers": {"server": "testserver"},95 },96 ]979899@pytest.mark.usefixtures("async_environment")100async def test_async_event_hooks_raising_exception():101 async def raise_on_4xx_5xx(response):102 response.raise_for_status()103104 event_hooks = {"response": [raise_on_4xx_5xx]}105106 async with httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 107why, one step back107 event_hooks=event_hooks, transport=MockTransport(app)Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 108–168why, one step back108 ) as http:109 try:110 await http.get("http://127.0.0.1:8000/status/400")111 except httpx.HTTPStatusError as exc:112 assert exc.response.is_closed113114115def test_event_hooks_with_redirect():116 """117 A redirect request should not trigger a second 'request' event hook.118 """119120 events = []121122 def on_request(request):123 events.append({"event": "request", "headers": dict(request.headers)})124125 def on_response(response):126 events.append({"event": "response", "headers": dict(response.headers)})127128 event_hooks = {"request": [on_request], "response": [on_response]}129130 with httpx.Client(event_hooks=event_hooks, transport=MockTransport(app)) as http:131 http.get("http://127.0.0.1:8000/redirect", auth=("username", "password"))132133 assert events == [134 {135 "event": "request",136 "headers": {137 "host": "127.0.0.1:8000",138 "user-agent": f"python-httpx/{httpx.__version__}",139 "accept": "*/*",140 "accept-encoding": "gzip, deflate, br",141 "connection": "keep-alive",142 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",143 },144 },145 {146 "event": "response",147 "headers": {"server": "testserver"},148 },149 ]150151152@pytest.mark.usefixtures("async_environment")153async def test_async_event_hooks_with_redirect():154 """155 A redirect request should not trigger a second 'request' event hook.156 """157158 events = []159160 async def on_request(request):161 events.append({"event": "request", "headers": dict(request.headers)})162163 async def on_response(response):164 events.append({"event": "response", "headers": dict(response.headers)})165166 event_hooks = {"request": [on_request], "response": [on_response]}167168 async with httpx.AsyncClient(Version 0.15.0 (#1301)Tom Christiebefore adoptionf932af9
line 169why, one step back169 event_hooks=event_hooks, transport=MockTransport(app)Event hooks (#1246)Tom Christiebefore adoption54f7708
lines 170–189why, one step back170 ) as http:171 await http.get("http://127.0.0.1:8000/redirect", auth=("username", "password"))172173 assert events == [174 {175 "event": "request",176 "headers": {177 "host": "127.0.0.1:8000",178 "user-agent": f"python-httpx/{httpx.__version__}",179 "accept": "*/*",180 "accept-encoding": "gzip, deflate, br",181 "connection": "keep-alive",182 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",183 },184 },185 {186 "event": "response",187 "headers": {"server": "testserver"},188 },189 ]