-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_client_session.py
1206 lines (986 loc) · 40.3 KB
/
test_client_session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import contextlib
import gc
import io
import json
from collections import deque
from http.cookies import SimpleCookie
from typing import (
Any,
Awaitable,
Callable,
Dict,
Iterator,
List,
NoReturn,
TypedDict,
Union,
)
from unittest import mock
from uuid import uuid4
import pytest
from multidict import CIMultiDict, MultiDict
from pytest_mock import MockerFixture
from yarl import URL
import aiohttp
from aiohttp import client, hdrs, tracing, web
from aiohttp.client import ClientSession
from aiohttp.client_proto import ResponseHandler
from aiohttp.client_reqrep import ClientRequest, ConnectionKey
from aiohttp.connector import BaseConnector, Connection, TCPConnector, UnixConnector
from aiohttp.cookiejar import CookieJar
from aiohttp.http import RawResponseMessage
from aiohttp.pytest_plugin import AiohttpClient, AiohttpServer
from aiohttp.test_utils import make_mocked_coro
from aiohttp.tracing import Trace
class _Params(TypedDict):
headers: Dict[str, str]
max_redirects: int
compress: str
chunked: bool
expect100: bool
read_until_eof: bool
@pytest.fixture
def connector(
loop: asyncio.AbstractEventLoop, create_mocked_conn: Callable[[], ResponseHandler]
) -> Iterator[BaseConnector]:
async def make_conn() -> BaseConnector:
return BaseConnector()
key = ConnectionKey("localhost", 80, False, True, None, None, None)
conn = loop.run_until_complete(make_conn())
proto = create_mocked_conn()
conn._conns[key] = deque([(proto, 123)])
yield conn
loop.run_until_complete(conn.close())
@pytest.fixture
def create_session(
loop: asyncio.AbstractEventLoop,
) -> Iterator[Callable[..., Awaitable[ClientSession]]]:
session = None
async def maker(*args: Any, **kwargs: Any) -> ClientSession:
nonlocal session
session = ClientSession(*args, **kwargs)
return session
yield maker
if session is not None:
loop.run_until_complete(session.close())
@pytest.fixture
def session(
create_session: Callable[..., Awaitable[ClientSession]],
loop: asyncio.AbstractEventLoop,
) -> ClientSession:
return loop.run_until_complete(create_session())
@pytest.fixture
def params() -> _Params:
return dict(
headers={"Authorization": "Basic ..."},
max_redirects=2,
compress="deflate",
chunked=True,
expect100=True,
read_until_eof=False,
)
async def test_close_coro(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session()
await session.close()
async def test_init_headers_simple_dict(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(headers={"h1": "header1", "h2": "header2"})
assert sorted(session.headers.items()) == ([("h1", "header1"), ("h2", "header2")])
async def test_init_headers_list_of_tuples(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(
headers=[("h1", "header1"), ("h2", "header2"), ("h3", "header3")]
)
assert session.headers == CIMultiDict(
[("h1", "header1"), ("h2", "header2"), ("h3", "header3")]
)
async def test_init_headers_MultiDict(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(
headers=MultiDict([("h1", "header1"), ("h2", "header2"), ("h3", "header3")])
)
assert session.headers == CIMultiDict(
[("H1", "header1"), ("H2", "header2"), ("H3", "header3")]
)
async def test_init_headers_list_of_tuples_with_duplicates(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(
headers=[("h1", "header11"), ("h2", "header21"), ("h1", "header12")]
)
assert session.headers == CIMultiDict(
[("H1", "header11"), ("H2", "header21"), ("H1", "header12")]
)
async def test_init_cookies_with_simple_dict(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(cookies={"c1": "cookie1", "c2": "cookie2"})
cookies = session.cookie_jar.filter_cookies(URL())
assert set(cookies) == {"c1", "c2"}
assert cookies["c1"].value == "cookie1"
assert cookies["c2"].value == "cookie2"
async def test_init_cookies_with_list_of_tuples(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(cookies=[("c1", "cookie1"), ("c2", "cookie2")])
cookies = session.cookie_jar.filter_cookies(URL())
assert set(cookies) == {"c1", "c2"}
assert cookies["c1"].value == "cookie1"
assert cookies["c2"].value == "cookie2"
async def test_merge_headers(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
# Check incoming simple dict
session = await create_session(headers={"h1": "header1", "h2": "header2"})
headers = session._prepare_headers({"h1": "h1"})
assert isinstance(headers, CIMultiDict)
assert headers == {"h1": "h1", "h2": "header2"}
async def test_merge_headers_with_multi_dict(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(headers={"h1": "header1", "h2": "header2"})
headers = session._prepare_headers(MultiDict([("h1", "h1")]))
assert isinstance(headers, CIMultiDict)
assert headers == {"h1": "h1", "h2": "header2"}
async def test_merge_headers_with_list_of_tuples(
create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(headers={"h1": "header1", "h2": "header2"})
headers = session._prepare_headers([("h1", "h1")])
assert isinstance(headers, CIMultiDict)
assert headers == {"h1": "h1", "h2": "header2"}
async def test_merge_headers_with_list_of_tuples_duplicated_names(
create_session: Callable[..., Awaitable[ClientSession]],
) -> None:
session = await create_session(headers={"h1": "header1", "h2": "header2"})
headers = session._prepare_headers([("h1", "v1"), ("h1", "v2")])
assert isinstance(headers, CIMultiDict)
assert list(sorted(headers.items())) == [
("h1", "v1"),
("h1", "v2"),
("h2", "header2"),
]
@pytest.mark.parametrize("obj", (object(), None))
async def test_invalid_data(session: ClientSession, obj: object) -> None:
with pytest.raises(TypeError, match="expected str"):
await session.post("http://example.test/", data={"some": obj})
async def test_http_GET(session: ClientSession, params: _Params) -> None:
with mock.patch(
"aiohttp.client.ClientSession._request", autospec=True, spec_set=True
) as patched:
await session.get("http://test.example.com", params={"x": 1}, **params)
assert patched.called, "`ClientSession._request` not called"
assert list(patched.call_args) == [
(session, "GET", "http://test.example.com"),
dict(params={"x": 1}, allow_redirects=True, **params),
]
async def test_http_OPTIONS(session: ClientSession, params: _Params) -> None:
with mock.patch(
"aiohttp.client.ClientSession._request", autospec=True, spec_set=True
) as patched:
await session.options("http://opt.example.com", params={"x": 2}, **params)
assert patched.called, "`ClientSession._request` not called"
assert list(patched.call_args) == [
(session, "OPTIONS", "http://opt.example.com"),
dict(params={"x": 2}, allow_redirects=True, **params),
]
async def test_http_HEAD(session: ClientSession, params: _Params) -> None:
with mock.patch(
"aiohttp.client.ClientSession._request", autospec=True, spec_set=True
) as patched:
await session.head("http://head.example.com", params={"x": 2}, **params)
assert patched.called, "`ClientSession._request` not called"
assert list(patched.call_args) == [
(session, "HEAD", "http://head.example.com"),
dict(params={"x": 2}, allow_redirects=False, **params),
]
async def test_http_POST(session: ClientSession, params: _Params) -> None:
with mock.patch(
"aiohttp.client.ClientSession._request", autospec=True, spec_set=True
) as patched:
await session.post(
"http://post.example.com", params={"x": 2}, data="Some_data", **params
)
assert patched.called, "`ClientSession._request` not called"
assert list(patched.call_args) == [
(session, "POST", "http://post.example.com"),
dict(params={"x": 2}, data="Some_data", **params),
]
async def test_http_PUT(session: ClientSession, params: _Params) -> None:
with mock.patch(
"aiohttp.client.ClientSession._request", autospec=True, spec_set=True
) as patched:
await session.put(
"http://put.example.com", params={"x": 2}, data="Some_data", **params
)
assert patched.called, "`ClientSession._request` not called"
assert list(patched.call_args) == [
(session, "PUT", "http://put.example.com"),
dict(params={"x": 2}, data="Some_data", **params),
]
async def test_http_PATCH(session: ClientSession, params: _Params) -> None:
with mock.patch(
"aiohttp.client.ClientSession._request", autospec=True, spec_set=True
) as patched:
await session.patch(
"http://patch.example.com", params={"x": 2}, data="Some_data", **params
)
assert patched.called, "`ClientSession._request` not called"
assert list(patched.call_args) == [
(session, "PATCH", "http://patch.example.com"),
dict(params={"x": 2}, data="Some_data", **params),
]
async def test_http_DELETE(session: ClientSession, params: _Params) -> None:
with mock.patch(
"aiohttp.client.ClientSession._request", autospec=True, spec_set=True
) as patched:
await session.delete("http://delete.example.com", params={"x": 2}, **params)
assert patched.called, "`ClientSession._request` not called"
assert list(patched.call_args) == [
(session, "DELETE", "http://delete.example.com"),
dict(params={"x": 2}, **params),
]
async def test_close(
create_session: Callable[..., Awaitable[ClientSession]], connector: BaseConnector
) -> None:
session = await create_session(connector=connector)
await session.close()
assert session.connector is None
assert connector.closed
async def test_closed(session: ClientSession) -> None:
assert not session.closed
await session.close()
assert session.closed
async def test_connector(
create_session: Callable[..., Awaitable[ClientSession]],
loop: asyncio.AbstractEventLoop,
mocker: MockerFixture,
) -> None:
connector = TCPConnector()
m = mocker.spy(connector, "close")
session = await create_session(connector=connector)
assert session.connector is connector
await session.close()
assert m.called
await connector.close()
async def test_create_connector(
create_session: Callable[..., Awaitable[ClientSession]],
loop: asyncio.AbstractEventLoop,
mocker: MockerFixture,
) -> None:
session = await create_session()
m = mocker.spy(session.connector, "close")
await session.close()
assert m.called
def test_connector_loop(loop: asyncio.AbstractEventLoop) -> None:
with contextlib.ExitStack() as stack:
another_loop = asyncio.new_event_loop()
stack.enter_context(contextlib.closing(another_loop))
async def make_connector() -> TCPConnector:
return TCPConnector()
connector = another_loop.run_until_complete(make_connector())
with pytest.raises(RuntimeError) as ctx:
async def make_sess() -> ClientSession:
return ClientSession(connector=connector)
loop.run_until_complete(make_sess())
expected = "Session and connector have to use same event loop"
assert str(ctx.value).startswith(expected)
another_loop.run_until_complete(connector.close())
def test_detach(loop: asyncio.AbstractEventLoop, session: ClientSession) -> None:
conn = session.connector
assert conn is not None
try:
assert not conn.closed
session.detach()
assert session.connector is None
assert session.closed
assert not conn.closed
finally:
loop.run_until_complete(conn.close())
async def test_request_closed_session(session: ClientSession) -> None:
await session.close()
with pytest.raises(RuntimeError):
await session.request("get", "/")
async def test_close_flag_for_closed_connector(session: ClientSession) -> None:
conn = session.connector
assert conn is not None
assert not session.closed
await conn.close()
assert session.closed
async def test_double_close(
connector: BaseConnector, create_session: Callable[..., Awaitable[ClientSession]]
) -> None:
session = await create_session(connector=connector)
await session.close()
assert session.connector is None
await session.close()
assert session.closed
assert connector.closed
async def test_del(connector: BaseConnector, loop: asyncio.AbstractEventLoop) -> None:
loop.set_debug(False)
# N.B. don't use session fixture, it stores extra reference internally
session = ClientSession(connector=connector)
logs = []
loop.set_exception_handler(lambda loop, ctx: logs.append(ctx))
with pytest.warns(ResourceWarning):
del session
gc.collect()
assert len(logs) == 1
expected = {"client_session": mock.ANY, "message": "Unclosed client session"}
assert logs[0] == expected
async def test_del_debug(
connector: BaseConnector, loop: asyncio.AbstractEventLoop
) -> None:
loop.set_debug(True)
# N.B. don't use session fixture, it stores extra reference internally
session = ClientSession(connector=connector)
logs = []
loop.set_exception_handler(lambda loop, ctx: logs.append(ctx))
with pytest.warns(ResourceWarning):
del session
gc.collect()
assert len(logs) == 1
expected = {
"client_session": mock.ANY,
"message": "Unclosed client session",
"source_traceback": mock.ANY,
}
assert logs[0] == expected
async def test_borrow_connector_loop(
connector: BaseConnector,
create_session: Callable[..., Awaitable[ClientSession]],
loop: asyncio.AbstractEventLoop,
) -> None:
async with ClientSession(connector=connector) as session:
assert session._loop is loop
async def test_reraise_os_error(
create_session: Callable[..., Awaitable[ClientSession]],
create_mocked_conn: Callable[[], ResponseHandler],
) -> None:
err = OSError(1, "permission error")
req = mock.Mock()
req_factory = mock.Mock(return_value=req)
req.send = mock.Mock(side_effect=err)
session = await create_session(request_class=req_factory)
async def create_connection(
req: object, traces: object, timeout: object
) -> ResponseHandler:
# return self.transport, self.protocol
return create_mocked_conn()
with mock.patch.object(session._connector, "_create_connection", create_connection):
with mock.patch.object(
session._connector, "_release", autospec=True, spec_set=True
):
with pytest.raises(aiohttp.ClientOSError) as ctx:
await session.request("get", "http://example.com")
e = ctx.value
assert e.errno == err.errno
assert e.strerror == err.strerror
async def test_close_conn_on_error(
create_session: Callable[..., Awaitable[ClientSession]],
create_mocked_conn: Callable[[], ResponseHandler],
) -> None:
class UnexpectedException(BaseException):
pass
err = UnexpectedException("permission error")
req = mock.Mock()
req_factory = mock.Mock(return_value=req)
req.send = mock.Mock(side_effect=err)
session = await create_session(request_class=req_factory)
connections = []
assert session._connector is not None
original_connect = session._connector.connect
async def connect(
req: ClientRequest, traces: List[Trace], timeout: aiohttp.ClientTimeout
) -> Connection:
conn = await original_connect(req, traces, timeout)
connections.append(conn)
return conn
async def create_connection(
req: object, traces: object, timeout: object
) -> ResponseHandler:
# return self.transport, self.protocol
conn = create_mocked_conn()
return conn
with mock.patch.object(session._connector, "connect", connect):
with mock.patch.object(
session._connector, "_create_connection", create_connection
):
with mock.patch.object(
session._connector, "_release", autospec=True, spec_set=True
):
with pytest.raises(UnexpectedException):
async with session.request("get", "http://example.com") as resp:
await resp.text()
# normally called during garbage collection. triggers an exception
# if the connection wasn't already closed
for c in connections:
c.__del__()
@pytest.mark.parametrize("protocol", ["http", "https", "ws", "wss"])
async def test_ws_connect_allowed_protocols(
create_session: Callable[..., Awaitable[ClientSession]],
create_mocked_conn: Callable[[], ResponseHandler],
protocol: str,
ws_key: str,
key_data: bytes,
) -> None:
resp = mock.create_autospec(aiohttp.ClientResponse)
resp.status = 101
resp.headers = {
hdrs.UPGRADE: "websocket",
hdrs.CONNECTION: "upgrade",
hdrs.SEC_WEBSOCKET_ACCEPT: ws_key,
}
resp.url = URL(f"{protocol}://example")
resp.cookies = SimpleCookie()
resp.start = mock.AsyncMock()
req = mock.create_autospec(aiohttp.ClientRequest, spec_set=True)
req_factory = mock.Mock(return_value=req)
req.send = mock.AsyncMock(return_value=resp)
# BaseConnector allows all high level protocols by default
connector = BaseConnector()
session = await create_session(connector=connector, request_class=req_factory)
connections = []
assert session._connector is not None
original_connect = session._connector.connect
async def connect(
req: ClientRequest, traces: List[Trace], timeout: aiohttp.ClientTimeout
) -> Connection:
conn = await original_connect(req, traces, timeout)
connections.append(conn)
return conn
async def create_connection(
req: object, traces: object, timeout: object
) -> ResponseHandler:
return create_mocked_conn()
connector = session._connector
with (
mock.patch.object(connector, "connect", connect),
mock.patch.object(connector, "_create_connection", create_connection),
mock.patch.object(connector, "_release"),
mock.patch("aiohttp.client.os") as m_os,
):
m_os.urandom.return_value = key_data
await session.ws_connect(f"{protocol}://example")
# normally called during garbage collection. triggers an exception
# if the connection wasn't already closed
for c in connections:
c.close()
c.__del__()
await session.close()
@pytest.mark.parametrize("protocol", ["http", "https", "ws", "wss", "unix"])
async def test_ws_connect_unix_socket_allowed_protocols(
create_session: Callable[..., Awaitable[ClientSession]],
create_mocked_conn: Callable[[], ResponseHandler],
protocol: str,
ws_key: str,
key_data: bytes,
) -> None:
resp = mock.create_autospec(aiohttp.ClientResponse)
resp.status = 101
resp.headers = {
hdrs.UPGRADE: "websocket",
hdrs.CONNECTION: "upgrade",
hdrs.SEC_WEBSOCKET_ACCEPT: ws_key,
}
resp.url = URL(f"{protocol}://example")
resp.cookies = SimpleCookie()
resp.start = mock.AsyncMock()
req = mock.create_autospec(aiohttp.ClientRequest, spec_set=True)
req_factory = mock.Mock(return_value=req)
req.send = mock.AsyncMock(return_value=resp)
# UnixConnector allows all high level protocols by default and unix sockets
session = await create_session(
connector=UnixConnector(path=""), request_class=req_factory
)
connections = []
assert session._connector is not None
original_connect = session._connector.connect
async def connect(
req: ClientRequest, traces: List[Trace], timeout: aiohttp.ClientTimeout
) -> Connection:
conn = await original_connect(req, traces, timeout)
connections.append(conn)
return conn
async def create_connection(
req: object, traces: object, timeout: object
) -> ResponseHandler:
return create_mocked_conn()
connector = session._connector
with (
mock.patch.object(connector, "connect", connect),
mock.patch.object(connector, "_create_connection", create_connection),
mock.patch.object(connector, "_release"),
mock.patch("aiohttp.client.os") as m_os,
):
m_os.urandom.return_value = key_data
await session.ws_connect(f"{protocol}://example")
# normally called during garbage collection. triggers an exception
# if the connection wasn't already closed
for c in connections:
c.close()
c.__del__()
await session.close()
async def test_cookie_jar_usage(
loop: asyncio.AbstractEventLoop, aiohttp_client: AiohttpClient
) -> None:
req_url = None
jar = mock.Mock()
jar.filter_cookies.return_value = None
async def handler(request: web.Request) -> web.Response:
nonlocal req_url
req_url = "http://%s/" % request.host
resp = web.Response()
resp.set_cookie("response", "resp_value")
return resp
app = web.Application()
app.router.add_route("GET", "/", handler)
session = await aiohttp_client(
app, cookies={"request": "req_value"}, cookie_jar=jar
)
# Updating the cookie jar with initial user defined cookies
jar.update_cookies.assert_called_with({"request": "req_value"})
jar.update_cookies.reset_mock()
resp = await session.get("/")
resp.release()
assert req_url is not None
# Filtering the cookie jar before sending the request,
# getting the request URL as only parameter
jar.filter_cookies.assert_called_with(URL(req_url))
# Updating the cookie jar with the response cookies
assert jar.update_cookies.called
resp_cookies = jar.update_cookies.call_args[0][0]
assert isinstance(resp_cookies, SimpleCookie)
assert "response" in resp_cookies
assert resp_cookies["response"].value == "resp_value"
async def test_cookies_with_not_quoted_cookie_jar(
aiohttp_server: AiohttpServer,
) -> None:
async def handler(_: web.Request) -> web.Response:
return web.Response()
app = web.Application()
app.router.add_route("GET", "/", handler)
server = await aiohttp_server(app)
jar = CookieJar(quote_cookie=False)
cookies = {"name": "val=foobar"}
async with aiohttp.ClientSession(cookie_jar=jar) as sess:
resp = await sess.request("GET", server.make_url("/"), cookies=cookies)
assert resp.request_info.headers.get("Cookie", "") == "name=val=foobar"
async def test_session_default_version(loop: asyncio.AbstractEventLoop) -> None:
session = aiohttp.ClientSession()
assert session.version == aiohttp.HttpVersion11
await session.close()
async def test_proxy_str(session: ClientSession, params: _Params) -> None:
with mock.patch(
"aiohttp.client.ClientSession._request", autospec=True, spec_set=True
) as patched:
await session.get("http://test.example.com", proxy="http://proxy.com", **params)
assert patched.called, "`ClientSession._request` not called"
assert list(patched.call_args) == [
(session, "GET", "http://test.example.com"),
dict(allow_redirects=True, proxy="http://proxy.com", **params),
]
async def test_default_proxy(loop: asyncio.AbstractEventLoop) -> None:
proxy_url = URL("http://proxy.example.com")
proxy_auth = mock.Mock()
proxy_url2 = URL("http://proxy.example2.com")
proxy_auth2 = mock.Mock()
class OnCall(Exception):
pass
request_class_mock = mock.Mock(side_effect=OnCall())
session = ClientSession(
proxy=proxy_url, proxy_auth=proxy_auth, request_class=request_class_mock
)
assert session._default_proxy == proxy_url, "`ClientSession._default_proxy` not set"
assert (
session._default_proxy_auth == proxy_auth
), "`ClientSession._default_proxy_auth` not set"
with pytest.raises(OnCall):
await session.get(
"http://example.com",
)
assert request_class_mock.called, "request class not called"
assert (
request_class_mock.call_args[1].get("proxy") == proxy_url
), "`ClientSession._request` uses default proxy not one used in ClientSession.get"
assert (
request_class_mock.call_args[1].get("proxy_auth") == proxy_auth
), "`ClientSession._request` uses default proxy_auth not one used in ClientSession.get"
request_class_mock.reset_mock()
with pytest.raises(OnCall):
await session.get(
"http://example.com", proxy=proxy_url2, proxy_auth=proxy_auth2
)
assert request_class_mock.called, "request class not called"
assert (
request_class_mock.call_args[1].get("proxy") == proxy_url2
), "`ClientSession._request` uses default proxy not one used in ClientSession.get"
assert (
request_class_mock.call_args[1].get("proxy_auth") == proxy_auth2
), "`ClientSession._request` uses default proxy_auth not one used in ClientSession.get"
await session.close()
async def test_request_tracing(
loop: asyncio.AbstractEventLoop, aiohttp_client: AiohttpClient
) -> None:
async def handler(request: web.Request) -> web.Response:
return web.json_response({"ok": True})
app = web.Application()
app.router.add_post("/", handler)
trace_config_ctx = mock.Mock()
body = "This is request body"
gathered_req_headers: CIMultiDict[str] = CIMultiDict()
on_request_start = mock.Mock(side_effect=make_mocked_coro(mock.Mock()))
on_request_redirect = mock.Mock(side_effect=make_mocked_coro(mock.Mock()))
on_request_end = mock.Mock(side_effect=make_mocked_coro(mock.Mock()))
with io.BytesIO() as gathered_req_body, io.BytesIO() as gathered_res_body:
async def on_request_chunk_sent(
session: object,
context: object,
params: tracing.TraceRequestChunkSentParams,
) -> None:
gathered_req_body.write(params.chunk)
async def on_response_chunk_received(
session: object,
context: object,
params: tracing.TraceResponseChunkReceivedParams,
) -> None:
gathered_res_body.write(params.chunk)
async def on_request_headers_sent(
session: object,
context: object,
params: tracing.TraceRequestHeadersSentParams,
) -> None:
gathered_req_headers.extend(**params.headers)
trace_config = aiohttp.TraceConfig(
trace_config_ctx_factory=mock.Mock(return_value=trace_config_ctx)
)
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_end.append(on_request_end)
trace_config.on_request_chunk_sent.append(on_request_chunk_sent)
trace_config.on_response_chunk_received.append(on_response_chunk_received)
trace_config.on_request_redirect.append(on_request_redirect)
trace_config.on_request_headers_sent.append(on_request_headers_sent)
headers = CIMultiDict({"Custom-Header": "Custom value"})
session = await aiohttp_client(
app, trace_configs=[trace_config], headers=headers
)
async with session.post("/", data=body, trace_request_ctx={}) as resp:
await resp.json()
on_request_start.assert_called_once_with(
session.session,
trace_config_ctx,
aiohttp.TraceRequestStartParams(
hdrs.METH_POST, session.make_url("/"), headers
),
)
on_request_end.assert_called_once_with(
session.session,
trace_config_ctx,
aiohttp.TraceRequestEndParams(
hdrs.METH_POST, session.make_url("/"), headers, resp
),
)
assert not on_request_redirect.called
assert gathered_req_body.getvalue() == body.encode("utf8")
assert gathered_res_body.getvalue() == json.dumps({"ok": True}).encode(
"utf8"
)
assert gathered_req_headers["Custom-Header"] == "Custom value"
async def test_request_tracing_url_params(
loop: asyncio.AbstractEventLoop, aiohttp_client: AiohttpClient
) -> None:
async def root_handler(request: web.Request) -> web.Response:
return web.Response()
async def redirect_handler(request: web.Request) -> NoReturn:
raise web.HTTPFound("/")
app = web.Application()
app.router.add_get("/", root_handler)
app.router.add_get("/redirect", redirect_handler)
mocks = [mock.Mock(side_effect=make_mocked_coro(mock.Mock())) for _ in range(7)]
(
on_request_start,
on_request_redirect,
on_request_end,
on_request_exception,
on_request_chunk_sent,
on_response_chunk_received,
on_request_headers_sent,
) = mocks
trace_config = aiohttp.TraceConfig(
trace_config_ctx_factory=mock.Mock(return_value=mock.Mock())
)
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_redirect.append(on_request_redirect)
trace_config.on_request_end.append(on_request_end)
trace_config.on_request_exception.append(on_request_exception)
trace_config.on_request_chunk_sent.append(on_request_chunk_sent)
trace_config.on_response_chunk_received.append(on_response_chunk_received)
trace_config.on_request_headers_sent.append(on_request_headers_sent)
session = await aiohttp_client(app, trace_configs=[trace_config])
def reset_mocks() -> None:
for m in mocks:
m.reset_mock()
def to_trace_urls(mock_func: mock.Mock) -> List[URL]:
return [call_args[0][-1].url for call_args in mock_func.call_args_list]
def to_url(path: str) -> URL:
return session.make_url(path)
# Standard
req: Callable[[], Awaitable[aiohttp.ClientResponse]]
for req in (
lambda: session.get("/?x=0"),
lambda: session.get("/", params=dict(x=0)),
):
reset_mocks()
async with req() as resp:
await resp.text()
assert to_trace_urls(on_request_start) == [to_url("/?x=0")]
assert to_trace_urls(on_request_redirect) == []
assert to_trace_urls(on_request_end) == [to_url("/?x=0")]
assert to_trace_urls(on_request_exception) == []
assert to_trace_urls(on_request_chunk_sent) == []
assert to_trace_urls(on_response_chunk_received) == [to_url("/?x=0")]
assert to_trace_urls(on_request_headers_sent) == [to_url("/?x=0")]
# Redirect
for req in (
lambda: session.get("/redirect?x=0"),
lambda: session.get("/redirect", params=dict(x=0)),
):
reset_mocks()
async with req() as resp:
await resp.text()
assert to_trace_urls(on_request_start) == [to_url("/redirect?x=0")]
assert to_trace_urls(on_request_redirect) == [to_url("/redirect?x=0")]
assert to_trace_urls(on_request_end) == [to_url("/")]
assert to_trace_urls(on_request_exception) == []
assert to_trace_urls(on_request_chunk_sent) == []
assert to_trace_urls(on_response_chunk_received) == [to_url("/")]
assert to_trace_urls(on_request_headers_sent) == [
to_url("/redirect?x=0"),
to_url("/"),
]
# Exception
with mock.patch("aiohttp.client.TCPConnector.connect") as connect_patched:
connect_patched.side_effect = Exception()
for req in (
lambda: session.get("/?x=0"),
lambda: session.get("/", params=dict(x=0)),
):
reset_mocks()
with contextlib.suppress(Exception):
await req()
assert to_trace_urls(on_request_start) == [to_url("/?x=0")]
assert to_trace_urls(on_request_redirect) == []
assert to_trace_urls(on_request_end) == []
assert to_trace_urls(on_request_exception) == [to_url("?x=0")]
assert to_trace_urls(on_request_chunk_sent) == []
assert to_trace_urls(on_response_chunk_received) == []
assert to_trace_urls(on_request_headers_sent) == []
async def test_request_tracing_exception() -> None:
on_request_end = mock.Mock(side_effect=make_mocked_coro(mock.Mock()))
on_request_exception = mock.Mock(side_effect=make_mocked_coro(mock.Mock()))
trace_config = aiohttp.TraceConfig()
trace_config.on_request_end.append(on_request_end)
trace_config.on_request_exception.append(on_request_exception)
with mock.patch("aiohttp.client.TCPConnector.connect") as connect_patched:
error = Exception()
connect_patched.side_effect = error
session = aiohttp.ClientSession(trace_configs=[trace_config])
try:
await session.get("http://example.com")
except Exception:
pass
on_request_exception.assert_called_once_with(
session,
mock.ANY,
aiohttp.TraceRequestExceptionParams(
hdrs.METH_GET, URL("http://example.com"), CIMultiDict(), error
),
)
assert not on_request_end.called
await session.close()
async def test_request_tracing_interpose_headers(
loop: asyncio.AbstractEventLoop, aiohttp_client: AiohttpClient
) -> None:
async def handler(request: web.Request) -> web.Response:
return web.Response()