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 15, 2021
1 parent 2f655a5 commit 78f8878
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 6 additions & 3 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 @@ -736,7 +736,7 @@ def __init__(
assert isinstance(url, URL)

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 @@ -922,7 +922,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
12 changes: 7 additions & 5 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import contextlib
import gc
import json
import sys
from http.cookies import SimpleCookie
import re
from http.cookies import Morsel
from io import BytesIO
from unittest import mock

Expand Down Expand Up @@ -506,9 +506,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) -> None:
Expand Down

0 comments on commit 78f8878

Please sign in to comment.