Skip to content

Commit

Permalink
Handle response with multiple same name cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Feb 14, 2021
1 parent c4c18c5 commit 743371d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 6 additions & 3 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import traceback
import warnings
from hashlib import md5, sha1, sha256
from http.cookies import CookieError, Morsel, SimpleCookie
from http.cookies import BaseCookie, CookieError, Morsel, SimpleCookie
from types import MappingProxyType, TracebackType
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -680,7 +680,7 @@ def __init__(
super().__init__()

self.method = method
self.cookies = SimpleCookie() # type: SimpleCookie[str]
self.cookies = [] # type: List[Tuple[str, BaseCookie[str]]]

self._real_url = url
self._url = url.with_fragment(None)
Expand Down Expand Up @@ -860,7 +860,10 @@ async def start(self, connection: "Connection") -> "ClientResponse":
# cookies
for hdr in self.headers.getall(hdrs.SET_COOKIE, ()):
try:
self.cookies.load(hdr)
new_cookie = SimpleCookie()
new_cookie.load(hdr)
new_cookie_list = list(new_cookie.items())
self.cookies = self.cookies + new_cookie_list # type: ignore
except CookieError as exc:
client_logger.warning("Can not load response cookies: %s", exc)
return self
Expand Down
10 changes: 6 additions & 4 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import gc
import json
import sys
from http.cookies import SimpleCookie
from http.cookies import Morsel
from io import BytesIO
from typing import Any
from unittest import mock
Expand Down Expand Up @@ -502,9 +502,11 @@ async def handler(request):
# 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"
assert isinstance(resp_cookies, list)
assert isinstance(resp_cookies[0], tuple)
assert isinstance(resp_cookies[0][1], Morsel)
assert "response" == resp_cookies[0][0]
assert "resp_value" == resp_cookies[0][1].coded_value


async def test_session_default_version(loop: Any) -> None:
Expand Down

0 comments on commit 743371d

Please sign in to comment.