Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DummyCookieJar helper #1830

Merged
merged 1 commit into from
Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Changes

- Add `proxy_from_env` to `ClientRequest` to read from environment variables. #1791

- Add DummyCookieJar helper. #1830


2.0.7 (2017-04-12)
------------------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Sviatoslav Bulbakha
Taha Jahangir
Taras Voinarovskyi
Terence Honles
Thanos Lefteris
Thomas Grainger
Tolga Tezel
Vaibhav Sagar
Expand Down
30 changes: 29 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from async_timeout import timeout

from . import hdrs
from .abc import AbstractCookieJar

try:
from asyncio import ensure_future
Expand All @@ -39,7 +40,7 @@


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


sentinel = object()
Expand Down Expand Up @@ -762,3 +763,30 @@ 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
3 changes: 3 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ The client session supports the context manager protocol for self closing.
One example is not processing cookies at all when working in
proxy mode.

If no cookie processing is needed, a :class:`aiohttp.helpers.DummyCookieJar`
instance can be provided.

.. versionadded:: 0.22

:param callable json_serialize: Json `serializer` function. (:func:`json.dumps` by default)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest import mock

import pytest
from yarl import URL

from aiohttp import helpers

Expand Down Expand Up @@ -533,3 +534,15 @@ 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()