Skip to content

Commit 2c8f597

Browse files
committed
extra/cookies - cleanups
1 parent 4aa8031 commit 2c8f597

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

aiopenapi3/extra/cookies.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ def __init__(self, ctx: aiopenapi3.plugin.Message.Context) -> None:
2121
self.ctx = ctx
2222

2323
def add_unredirected_header(self, key: str, value: str) -> None:
24-
if key.lower() == "cookie":
25-
name, _, value = value.partition("=")
26-
self.ctx.cookies[name] = value
27-
else:
28-
self.ctx.headers[key] = value
24+
assert key.lower() == "cookie"
25+
name, _, value = value.partition("=")
26+
self.ctx.cookies[name] = value
2927

3028
class _Response:
3129
"""
@@ -45,22 +43,23 @@ def info(self) -> email.message.Message:
4543
def __init__(
4644
self, cookiejar: http.cookiejar.CookieJar = None, policy: Literal["jar", "securitySchemes"] = "jar"
4745
) -> None:
48-
self.cookiejar = cookiejar or http.cookiejar.CookieJar()
49-
self.policy = policy
50-
self.schemes: dict[str, str] = None
46+
self.cookiejar: http.cookiejar.CookieJar = cookiejar or http.cookiejar.CookieJar()
47+
self.policy: Literal["jar", "securitySchemes"] = policy
48+
self.schemes: dict[str, str] = dict()
49+
50+
if policy not in ["jar", "securitySchemes"]:
51+
raise ValueError(f"policy {self.policy} is not a valid policy")
52+
5153
super().__init__()
5254

5355
def initialized(self, ctx: "aiopenapi3.plugin.Init.Context") -> "aiopenapi3.plugin.Init.Context":
54-
if self.policy in ["securitySchemes", "jar"]:
55-
self.schemes = {
56-
v.root.name: k
57-
for k, v in filter(
58-
lambda x: (x[1].root.type.lower(), x[1].root.in_) == ("apikey", "cookie"),
59-
self.api.components.securitySchemes.items(),
60-
)
61-
}
62-
else:
63-
raise ValueError(f"policy {self.policy} is not a valid policy")
56+
self.schemes = {
57+
v.root.name: k
58+
for k, v in filter(
59+
lambda x: (x[1].root.type.lower(), x[1].root.in_) == ("apikey", "cookie"),
60+
self.api.components.securitySchemes.items(),
61+
)
62+
}
6463
return ctx
6564

6665
def received(self, ctx: "aiopenapi3.plugin.Message.Context") -> "aiopenapi3.plugin.Message.Context":

tests/extra_test.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,4 @@ def test_cookies(httpx_mock, with_extra_cookie, cookie):
239239

240240
def test_cookies_policy(with_extra_cookie):
241241
with pytest.raises(ValueError, match="policy … is not a valid policy"):
242-
OpenAPI(
243-
"http://127.0.0.1/api.yaml",
244-
with_extra_cookie,
245-
session_factory=httpx.Client,
246-
plugins=[Cookies(policy="…")],
247-
)
242+
Cookies(policy="…")

0 commit comments

Comments
 (0)