Skip to content

Commit

Permalink
Fix #2242: Move DummyCookieJar into cookiejar.py (#2244)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov authored Sep 5, 2017
1 parent 3afb09e commit d4c1237
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 48 deletions.
5 changes: 3 additions & 2 deletions aiohttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
WSMsgType, WSCloseCode, WSMessage, WebSocketError) # noqa
from .streams import * # noqa
from .multipart import * # noqa
from .cookiejar import CookieJar # noqa
from .cookiejar import * # noqa
from .payload import * # noqa
from .payload_streamer import * # noqa
from .resolver import * # noqa
Expand All @@ -23,6 +23,7 @@


__all__ = (client.__all__ + # noqa
cookiejar.__all__ + # noqa
formdata.__all__ + # noqa
helpers.__all__ + # noqa
multipart.__all__ + # noqa
Expand All @@ -31,5 +32,5 @@
streams.__all__ + # noqa
('hdrs', 'HttpVersion', 'HttpVersion10', 'HttpVersion11',
'WSMsgType', 'WSCloseCode',
'WebSocketError', 'WSMessage', 'CookieJar',
'WebSocketError', 'WSMessage',
) + workers)
30 changes: 30 additions & 0 deletions aiohttp/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from .helpers import SimpleCookie, is_ip_address


__all__ = ('CookieJar', 'DummyCookieJar')


class CookieJar(AbstractCookieJar):
"""Implements cookie storage adhering to RFC 6265."""

Expand Down Expand Up @@ -304,3 +307,30 @@ def _parse_date(cls, date_str):
return datetime.datetime(year, month, day,
hour, minute, second,
tzinfo=datetime.timezone.utc)


class DummyCookieJar(AbstractCookieJar):
"""Implements a dummy cookie storage.
It can be used with the ClientSession when no cookie processing is needed.
"""

def __init__(self, *, loop=None):
super().__init__(loop=loop)

def __iter__(self):
while False:
yield None

def __len__(self):
return 0

def clear(self):
pass

def update_cookies(self, cookies, response_url=None):
pass

def filter_cookies(self, request_url):
return None
30 changes: 1 addition & 29 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from async_timeout import timeout

from . import hdrs
from .abc import AbstractCookieJar


try:
Expand All @@ -40,7 +39,7 @@


__all__ = ('BasicAuth', 'create_future', 'parse_mimetype',
'Timeout', 'ensure_future', 'noop', 'DummyCookieJar')
'Timeout', 'ensure_future', 'noop')


sentinel = object()
Expand Down Expand Up @@ -794,30 +793,3 @@ def content_length(self, *, _CONTENT_LENGTH=hdrs.CONTENT_LENGTH):
return None
else:
return int(l)


class DummyCookieJar(AbstractCookieJar):
"""Implements a dummy cookie storage.
It can be used with the ClientSession when no cookie processing is needed.
"""

def __init__(self, *, loop=None):
super().__init__(loop=loop)

def __iter__(self):
while False:
yield None

def __len__(self):
return 0

def clear(self):
pass

def update_cookies(self, cookies, response_url=None):
pass

def filter_cookies(self, request_url):
return None
1 change: 1 addition & 0 deletions changes/2242.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move DummyCookieJar into cookiejar.py
17 changes: 15 additions & 2 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,21 @@ cookies. It should be done by passing `unsafe=True` to
:class:`aiohttp.CookieJar` constructor::


jar = aiohttp.CookieJar(unsafe=True)
session = aiohttp.ClientSession(cookie_jar=jar)
jar = aiohttp.CookieJar(unsafe=True)
session = aiohttp.ClientSession(cookie_jar=jar)


.. _aiohttp-client-dummy-cookie-jar:

Dummy Cookie Jar
----------------

Sometimes cookie processing is not desirable. For this purpose it's
possible to pass :class:`aiohttp.DummyCookieJar` instance into client
session::

jar = aiohttp.DummyCookieJar()
session = aiohttp.ClientSession(cookie_jar=jar)


Connectors
Expand Down
15 changes: 14 additions & 1 deletion docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ BasicAuth
CookieJar
^^^^^^^^^

.. class:: CookieJar(unsafe=False, loop=None)
.. class:: CookieJar(*, unsafe=False, loop=None)

The cookie jar instance is available as :attr:`ClientSession.cookie_jar`.

Expand Down Expand Up @@ -1416,6 +1416,19 @@ CookieJar
imported, :class:`str` or :class:`pathlib.Path` instance.



.. class:: DummyCookieJar(*, loop=None)

Dummy cookie jar which does not store cookies but ignores them.

Could be useful e.g. for web crawlers to iterate over Internet
without blowing up with saved cookies information.

To install dummy cookie jar pass it into session instance::

jar = aiohttp.DummyCookieJar()
session = aiohttp.ClientSession(cookie_jar=DummyCookieJar())

Client exceptions
-----------------

Expand Down
14 changes: 13 additions & 1 deletion tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
from yarl import URL

from aiohttp import CookieJar
from aiohttp import CookieJar, DummyCookieJar
from aiohttp.helpers import SimpleCookie


Expand Down Expand Up @@ -596,3 +596,15 @@ def test_cookie_not_expired_when_added_after_removal(self):

# Assert that there is a cookie.
assert len(jar) == 1


def test_dummy_cookie_jar(loop):
cookie = SimpleCookie('foo=bar; Domain=example.com;')
dummy_jar = DummyCookieJar(loop=loop)
assert len(dummy_jar) == 0
dummy_jar.update_cookies(cookie)
assert len(dummy_jar) == 0
with pytest.raises(StopIteration):
next(iter(dummy_jar))
assert dummy_jar.filter_cookies(URL("http://example.com/")) is None
dummy_jar.clear()
13 changes: 0 additions & 13 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from unittest import mock

import pytest
from yarl import URL

from aiohttp import helpers

Expand Down Expand Up @@ -547,15 +546,3 @@ def test_set_content_disposition_bad_param():
with pytest.raises(ValueError):
helpers.content_disposition_header('inline',
**{'foo\x00bar': 'baz'})


def test_dummy_cookie_jar(loop):
cookie = helpers.SimpleCookie('foo=bar; Domain=example.com;')
dummy_jar = helpers.DummyCookieJar(loop=loop)
assert len(dummy_jar) == 0
dummy_jar.update_cookies(cookie)
assert len(dummy_jar) == 0
with pytest.raises(StopIteration):
next(iter(dummy_jar))
assert dummy_jar.filter_cookies(URL("http://example.com/")) is None
dummy_jar.clear()

0 comments on commit d4c1237

Please sign in to comment.