Skip to content

Commit

Permalink
Add tests for special cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
perklet committed Aug 6, 2023
1 parent a5088f8 commit 4cf6dcb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import warnings
from enum import Enum
from http.cookiejar import Cookie
from http.cookies import SimpleCookie
from functools import partialmethod
from io import BytesIO
from json import dumps
Expand Down Expand Up @@ -415,7 +414,7 @@ def _set_curl_options(
interface = interface or self.interface
if interface:
c.setopt(CurlOpt.INTERFACE, interface.encode())

return Request(url, h, method), buffer, header_buffer

def _parse_response(self, curl, buffer, header_buffer):
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("/set_special_cookies"):
await set_special_cookies(scope, receive, send)
elif scope["path"].startswith("/redirect_301"):
await redirect_301(scope, receive, send)
elif scope["path"].startswith("/redirect_then_echo_cookies"):
Expand Down Expand Up @@ -301,6 +303,20 @@ async def set_cookies(scope, receive, send):
await send({"type": "http.response.body", "body": b"Hello, world!"})


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


async def redirect_301(scope, receive, send):
await send(
{"type": "http.response.start", "status": 301, "headers": [[b"location", b"/"]]}
Expand Down
8 changes: 8 additions & 0 deletions tests/unittest/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ def test_cookies_after_redirect(server):
assert r.json()["foo"] == "bar"


def test_cookies_with_special_chars(server):
s = requests.Session(debug=True)
r = s.get(str(server.url.copy_with(path="/set_special_cookies")))
assert s.cookies["foo"] == "bar space"
r = s.get(str(server.url.copy_with(path="/echo_cookies")))
assert r.json()["foo"] == "bar space"


# https://github.com/yifeikong/curl_cffi/issues/39
def test_post_body_cleaned(server):
s = requests.Session()
Expand Down

0 comments on commit 4cf6dcb

Please sign in to comment.