diff --git a/AUTHORS.rst b/AUTHORS.rst index 6e017c9a91..0080bfd75d 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -129,7 +129,6 @@ Patches and Suggestions - Dave Shawley - James Clarke (`@jam `_) - Kevin Burke -- Flavio Curella - David Pursehouse (`@dpursehouse `_) - Jon Parise (`@jparise `_) - Alexander Karpinsky (`@homm86 `_) @@ -193,3 +192,4 @@ Patches and Suggestions - Sylvain MariƩ (`@smarie `_) - Hod Bin Noon (`@hodbn `_) - Mike Fiedler (`@miketheman `_) +- Linkai Zheng (`@Konano `_) diff --git a/src/requests/cookies.py b/src/requests/cookies.py index f69d0cda9e..8e619bb0b3 100644 --- a/src/requests/cookies.py +++ b/src/requests/cookies.py @@ -346,15 +346,6 @@ def __delitem__(self, name): """ remove_cookie_by_name(self, name) - def set_cookie(self, cookie, *args, **kwargs): - if ( - hasattr(cookie.value, "startswith") - and cookie.value.startswith('"') - and cookie.value.endswith('"') - ): - cookie.value = cookie.value.replace('\\"', "") - return super().set_cookie(cookie, *args, **kwargs) - def update(self, other): """Updates this jar with cookies from another CookieJar or dict-like""" if isinstance(other, cookielib.CookieJar): diff --git a/tests/test_requests.py b/tests/test_requests.py index d8fbb23688..fcc5c08543 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -394,8 +394,23 @@ def test_cookie_removed_on_expire(self, httpbin): def test_cookie_quote_wrapped(self, httpbin): s = requests.session() - s.get(httpbin('cookies/set?foo="bar:baz"')) - assert s.cookies["foo"] == '"bar:baz"' + r = s.get(httpbin('cookies/set?foo="bar:baz"')) + assert s.cookies["foo"] == '"\\"bar:baz\\""' + assert r.request.headers["Cookie"] == 'foo="\\"bar:baz\\""' + assert r.json()["cookies"]["foo"] == '"bar:baz"' + + def test_cookie_with_json_value(self, httpbin): + s = requests.session() + r = s.get(httpbin('cookies/set?foo={"bar":"baz"}')) + assert s.cookies["foo"] == '"{\\"bar\\":\\"baz\\"}"' + assert r.request.headers["Cookie"] == 'foo="{\\"bar\\":\\"baz\\"}"' + assert r.json()["cookies"]["foo"] == '{"bar":"baz"}' + + def test_param_cookies_with_json_value(self, httpbin): + s = requests.session() + r = s.get(httpbin("cookies"), cookies={"foo": '"{\\"bar\\":\\"baz\\"}"'}) + assert r.request.headers["Cookie"] == 'foo="{\\"bar\\":\\"baz\\"}"' + assert r.json()["cookies"]["foo"] == '{"bar":"baz"}' def test_cookie_persists_via_api(self, httpbin): s = requests.session()