@@ -2712,6 +2712,7 @@ async def handler(request):
2712
2712
async with client .get ("/" ) as resp :
2713
2713
assert 200 == resp .status
2714
2714
cookie_names = {c .key for c in client .session .cookie_jar }
2715
+ _ = resp .cookies
2715
2716
assert cookie_names == {"c1" , "c2" }
2716
2717
2717
2718
m_log .warning .assert_called_with ("Can not load response cookies: %s" , mock .ANY )
@@ -5111,3 +5112,148 @@ async def redirect_handler(request: web.Request) -> web.Response:
5111
5112
assert (
5112
5113
payload .close_called
5113
5114
), "Payload.close() was not called when InvalidUrlRedirectClientError (invalid origin) was raised"
5115
+
5116
+
5117
+ async def test_amazon_like_cookie_scenario (aiohttp_client : AiohttpClient ) -> None :
5118
+ """Test real-world cookie scenario similar to Amazon."""
5119
+
5120
+ class FakeResolver (AbstractResolver ):
5121
+ def __init__ (self , port : int ):
5122
+ self ._port = port
5123
+
5124
+ async def resolve (
5125
+ self , host : str , port : int = 0 , family : int = 0
5126
+ ) -> List [ResolveResult ]:
5127
+ if host in ("amazon.it" , "www.amazon.it" ):
5128
+ return [
5129
+ {
5130
+ "hostname" : host ,
5131
+ "host" : "127.0.0.1" ,
5132
+ "port" : self ._port ,
5133
+ "family" : socket .AF_INET ,
5134
+ "proto" : 0 ,
5135
+ "flags" : 0 ,
5136
+ }
5137
+ ]
5138
+ assert False , f"Unexpected host: { host } "
5139
+
5140
+ async def close (self ) -> None :
5141
+ """Close the resolver if needed."""
5142
+
5143
+ async def handler (request : web .Request ) -> web .Response :
5144
+ response = web .Response (text = "Login successful" )
5145
+
5146
+ # Simulate Amazon-like cookies from the issue
5147
+ cookies = [
5148
+ "session-id=146-7423990-7621939; Domain=.amazon.it; "
5149
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/; "
5150
+ "Secure; HttpOnly" ,
5151
+ "session-id=147-8529641-8642103; Domain=.www.amazon.it; "
5152
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/; HttpOnly" ,
5153
+ "session-id-time=2082758401l; Domain=.amazon.it; "
5154
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/; Secure" ,
5155
+ "session-id-time=2082758402l; Domain=.www.amazon.it; "
5156
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/" ,
5157
+ "ubid-acbit=257-7531983-5395266; Domain=.amazon.it; "
5158
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/; Secure" ,
5159
+ 'x-acbit="KdvJzu8W@Fx6Jj3EuNFLuP0N7OtkuCfs"; Version=1; '
5160
+ "Domain=.amazon.it; Path=/; Secure; HttpOnly" ,
5161
+ "at-acbit=Atza|IwEBIM-gLr8; Domain=.amazon.it; "
5162
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/; "
5163
+ "Secure; HttpOnly" ,
5164
+ 'sess-at-acbit="4+6VzSJPHIFD/OqO264hFxIng8Y="; '
5165
+ "Domain=.amazon.it; Expires=Mon, 31-May-2027 10:00:00 GMT; "
5166
+ "Path=/; Secure; HttpOnly" ,
5167
+ "lc-acbit=it_IT; Domain=.amazon.it; "
5168
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/" ,
5169
+ "i18n-prefs=EUR; Domain=.amazon.it; "
5170
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/" ,
5171
+ "av-profile=null; Domain=.amazon.it; "
5172
+ "Expires=Mon, 31-May-2027 10:00:00 GMT; Path=/; Secure" ,
5173
+ 'user-pref-token="Am81ywsJ69xObBnuJ2FbilVH0mg="; '
5174
+ "Domain=.amazon.it; Path=/; Secure" ,
5175
+ ]
5176
+
5177
+ for cookie in cookies :
5178
+ response .headers .add ("Set-Cookie" , cookie )
5179
+
5180
+ return response
5181
+
5182
+ app = web .Application ()
5183
+ app .router .add_get ("/" , handler )
5184
+
5185
+ # Get the test server
5186
+ server = await aiohttp_client (app )
5187
+ port = server .port
5188
+
5189
+ # Create a new client session with our fake resolver
5190
+ resolver = FakeResolver (port )
5191
+
5192
+ async with (
5193
+ aiohttp .TCPConnector (resolver = resolver , force_close = True ) as connector ,
5194
+ aiohttp .ClientSession (connector = connector ) as session ,
5195
+ ):
5196
+ # Make request to www.amazon.it which will resolve to
5197
+ # 127.0.0.1:port. This allows cookies for both .amazon.it
5198
+ # and .www.amazon.it domains
5199
+ resp = await session .get (f"http://www.amazon.it:{ port } /" )
5200
+
5201
+ # Check headers
5202
+ cookie_headers = resp .headers .getall ("Set-Cookie" )
5203
+ assert (
5204
+ len (cookie_headers ) == 12
5205
+ ), f"Expected 12 headers, got { len (cookie_headers )} "
5206
+
5207
+ # Check parsed cookies - SimpleCookie only keeps the last
5208
+ # cookie with each name. So we expect 10 unique cookie names
5209
+ # (not 12)
5210
+ expected_cookie_names = {
5211
+ "session-id" , # Will only have one
5212
+ "session-id-time" , # Will only have one
5213
+ "ubid-acbit" ,
5214
+ "x-acbit" ,
5215
+ "at-acbit" ,
5216
+ "sess-at-acbit" ,
5217
+ "lc-acbit" ,
5218
+ "i18n-prefs" ,
5219
+ "av-profile" ,
5220
+ "user-pref-token" ,
5221
+ }
5222
+ assert set (resp .cookies .keys ()) == expected_cookie_names
5223
+ assert (
5224
+ len (resp .cookies ) == 10
5225
+ ), f"Expected 10 cookies in SimpleCookie, got { len (resp .cookies )} "
5226
+
5227
+ # The important part: verify the session's cookie jar has
5228
+ # all cookies. The cookie jar should have all 12 cookies,
5229
+ # not just 10
5230
+ jar_cookies = list (session .cookie_jar )
5231
+ assert (
5232
+ len (jar_cookies ) == 12
5233
+ ), f"Expected 12 cookies in jar, got { len (jar_cookies )} "
5234
+
5235
+ # Verify we have both session-id cookies with different domains
5236
+ session_ids = [c for c in jar_cookies if c .key == "session-id" ]
5237
+ assert (
5238
+ len (session_ids ) == 2
5239
+ ), f"Expected 2 session-id cookies, got { len (session_ids )} "
5240
+
5241
+ # Verify the domains are different
5242
+ session_id_domains = {c ["domain" ] for c in session_ids }
5243
+ assert session_id_domains == {
5244
+ "amazon.it" ,
5245
+ "www.amazon.it" ,
5246
+ }, f"Got domains: { session_id_domains } "
5247
+
5248
+ # Verify we have both session-id-time cookies with different
5249
+ # domains
5250
+ session_id_times = [c for c in jar_cookies if c .key == "session-id-time" ]
5251
+ assert (
5252
+ len (session_id_times ) == 2
5253
+ ), f"Expected 2 session-id-time cookies, got { len (session_id_times )} "
5254
+
5255
+ # Now test that the raw headers were properly preserved
5256
+ assert resp ._raw_cookie_headers is not None
5257
+ assert (
5258
+ len (resp ._raw_cookie_headers ) == 12
5259
+ ), "All raw headers should be preserved"
0 commit comments