Skip to content

Commit

Permalink
more correct type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph committed Nov 30, 2023
1 parent c390ae9 commit 48ef3b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/twisted/web/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,9 +1317,9 @@ def info(self) -> _InfoType:
@implementer(IAgent)
class CookieAgent:
"""
L{CookieAgent} extends the basic L{Agent} to add RFC-compliant
handling of HTTP cookies. Cookies are written to and extracted
from a C{cookielib.CookieJar} instance.
L{CookieAgent} extends the basic L{Agent} to add RFC-compliant handling of
HTTP cookies. Cookies are written to and extracted from a L{CookieJar}
instance.
The same cookie jar instance will be used for any requests through this
agent, mutating it whenever a I{Set-Cookie} header appears in a response.
Expand Down
42 changes: 24 additions & 18 deletions src/twisted/web/test/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ def test_responseIncludesRequest(self):
self.assertIsInstance(req, Request)

resp = client.Response._construct(
(b"HTTP", 1, 1), 200, b"OK", client.Headers({}), None, req
(b"HTTP", 1, 1), 200, b"OK", Headers({}), None, req
)
res.callback(resp)

Expand Down Expand Up @@ -1313,7 +1313,7 @@ def test_requestMissingAbsoluteURI(self):
"""
L{Request.absoluteURI} is L{None} if L{Request._parsedURI} is L{None}.
"""
request = client.Request(b"FOO", b"/", client.Headers(), None)
request = client.Request(b"FOO", b"/", Headers(), None)
self.assertIdentical(request.absoluteURI, None)

def test_endpointFactory(self):
Expand Down Expand Up @@ -1368,7 +1368,7 @@ def attemptRequestWithMaliciousMethod(self, method):
"""
agent = client.Agent(self.createReactor())
uri = b"http://twisted.invalid"
agent.request(method, uri, client.Headers(), None)
agent.request(method, uri, Headers(), None)


class AgentURIInjectionTests(
Expand All @@ -1388,7 +1388,7 @@ def attemptRequestWithMaliciousURI(self, uri):
"""
agent = client.Agent(self.createReactor())
method = b"GET"
agent.request(method, uri, client.Headers(), None)
agent.request(method, uri, Headers(), None)


@skipIf(not sslPresent, "SSL not present, cannot run SSL tests.")
Expand Down Expand Up @@ -1795,9 +1795,7 @@ def newProtocol():
return defer.succeed(protocol)

bodyProducer = object()
request = client.Request(
b"FOO", b"/", client.Headers(), bodyProducer, persistent=True
)
request = client.Request(b"FOO", b"/", Headers(), bodyProducer, persistent=True)
newProtocol()
protocol = protocols[0]
retrier = client._RetryingHTTP11ClientProtocol(protocol, newProtocol)
Expand Down Expand Up @@ -1936,7 +1934,9 @@ class CookieTestsMixin:
Mixin for unit tests dealing with cookies.
"""

def addCookies(self, cookieJar, uri, cookies):
def addCookies(
self, cookieJar: CookieJar, uri: bytes, cookies: list[bytes]
) -> tuple[client._FakeStdlibRequest, client._FakeStdlibResponse]:
"""
Add a cookie to a cookie jar.
"""
Expand All @@ -1945,7 +1945,7 @@ def addCookies(self, cookieJar, uri, cookies):
(b"HTTP", 1, 1),
200,
b"OK",
client.Headers({b"Set-Cookie": cookies}),
Headers({b"Set-Cookie": cookies}),
None,
)
)
Expand All @@ -1961,7 +1961,9 @@ class CookieJarTests(TestCase, CookieTestsMixin):
instances.
"""

def makeCookieJar(self):
def makeCookieJar(
self,
) -> tuple[CookieJar, tuple[client._FakeStdlibRequest, client._FakeStdlibResponse]]:
"""
@return: a L{CookieJar} with some sample cookies
"""
Expand All @@ -1973,10 +1975,11 @@ def makeCookieJar(self):
)
return cookieJar, reqres

def test_extractCookies(self):
def test_extractCookies(self) -> None:
"""
L{CookieJar.extract_cookies} extracts cookie information from
fake urllib2 response instances.
L{CookieJar.extract_cookies} extracts cookie information from our
stdlib-compatibility wrappers, L{client._FakeStdlibRequest} and
L{client._FakeStdlibResponse}.
"""
jar = self.makeCookieJar()[0]
cookies = {c.name: c for c in jar}
Expand All @@ -1997,17 +2000,20 @@ def test_extractCookies(self):
self.assertEqual(cookie.comment, "goodbye")
self.assertIdentical(cookie.get_nonstandard_attr("cow"), None)

def test_sendCookie(self):
def test_sendCookie(self) -> None:
"""
L{CookieJar.add_cookie_header} adds a cookie header to a fake
urllib2 request instance.
L{CookieJar.add_cookie_header} adds a cookie header to a Twisted
request via our .
"""
jar, (request, response) = self.makeCookieJar()

self.assertIdentical(request.get_header("Cookie", None), None)

jar.add_cookie_header(request)
self.assertEqual(request.get_header("Cookie", None), "foo=1; bar=2")
self.assertEqual(
list(request._twistedHeaders.getAllRawHeaders()),
[(b"Cookie", [b"foo=1; bar=2"])],
)


class CookieAgentTests(
Expand Down Expand Up @@ -2057,7 +2063,7 @@ def _checkCookie(ignored):
(b"HTTP", 1, 1),
200,
b"OK",
client.Headers(
Headers(
{
b"Set-Cookie": [
b"foo=1",
Expand Down

0 comments on commit 48ef3b6

Please sign in to comment.