Skip to content

Commit

Permalink
Fix critical typo: TURE -> TRUE, add delete cookie tests
Browse files Browse the repository at this point in the history
  • Loading branch information
perklet committed Aug 22, 2023
1 parent 7fc88b7 commit 9fddd18
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
6 changes: 2 additions & 4 deletions curl_cffi/requests/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def parse_bool(s):

@staticmethod
def dump_bool(s):
return "TURE" if s else "FALSE"
return "TRUE" if s else "FALSE"

@classmethod
def from_curl_format(cls, set_cookie_line: bytes):
Expand Down Expand Up @@ -101,9 +101,7 @@ def to_cookiejar_cookie(self) -> Cookie:
value=self.value,
port=None,
port_specified=False,
domain=self.hostname
if self.hostname.startswith(".")
else "." + self.hostname,
domain=self.hostname,
domain_specified=True,
domain_initial_dot=bool(self.hostname.startswith(".")),
path=self.path,
Expand Down
5 changes: 4 additions & 1 deletion curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def _set_curl_options(
c.setopt(CurlOpt.COOKIELIST, "ALL") # remove all the old cookies first.

for morsel in self.cookies.get_cookies_for_curl(req):
# print("Setting", morsel)
# print("Setting", morsel.to_curl_format())
curl.setopt(CurlOpt.COOKIELIST, morsel.to_curl_format())
if cookies:
temp_cookies = Cookies(cookies)
Expand Down Expand Up @@ -379,9 +379,12 @@ def _parse_response(self, curl, buffer, header_buffer):
continue
header_list.append(header_line)
rsp.headers = Headers(header_list)
# print("Set-cookie", rsp.headers["set-cookie"])
morsels = [
CurlMorsel.from_curl_format(l) for l in c.getinfo(CurlInfo.COOKIELIST)
]
# for l in c.getinfo(CurlInfo.COOKIELIST):
# print("Curl Cookies", l.decode())

self.cookies.update_cookies_from_curl(morsels)
rsp.cookies = self.cookies
Expand Down
16 changes: 16 additions & 0 deletions tests/unittest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ async def app(scope, receive, send):
await set_headers(scope, receive, send)
elif scope["path"].startswith("/set_cookies"):
await set_cookies(scope, receive, send)
elif scope["path"].startswith("/delete_cookies"):
await delete_cookies(scope, receive, send)
elif scope["path"].startswith("/set_special_cookies"):
await set_special_cookies(scope, receive, send)
elif scope["path"].startswith("/redirect_301"):
Expand Down Expand Up @@ -303,6 +305,20 @@ async def set_cookies(scope, receive, send):
await send({"type": "http.response.body", "body": b"Hello, world!"})


async def delete_cookies(scope, receive, send):
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [
[b"content-type", b"text/plain"],
[b"set-cookie", b'foo=; Max-Age=0'],
],
}
)
await send({"type": "http.response.body", "body": b"Hello, world!"})


async def set_special_cookies(scope, receive, send):
await send(
{
Expand Down
10 changes: 9 additions & 1 deletion tests/unittest/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def test_session_update_parms(server):


def test_session_preset_cookies(server):
s = requests.Session(cookies={"foo": "bar"}, debug=True)
s = requests.Session(cookies={"foo": "bar"})
# send requests with other cookies
r = s.get(
str(server.url.copy_with(path="/echo_cookies")), cookies={"hello": "world"}
Expand All @@ -259,6 +259,14 @@ def test_session_preset_cookies(server):
assert cookies["foo"] == "notbar"


def test_delete_cookies(server):
s = requests.Session()
s.get(str(server.url.copy_with(path="/set_cookies")))
assert s.cookies["foo"] == "bar"
s.get(str(server.url.copy_with(path="/delete_cookies")))
assert not s.cookies.get("foo")


def test_cookie_domains(server):
s = requests.Session()
s.cookies.set("foo", "bar", domain="example.com")
Expand Down

0 comments on commit 9fddd18

Please sign in to comment.