Skip to content

Commit

Permalink
Merge pull request #1830 from alefteris/dummy-cookie-jar
Browse files Browse the repository at this point in the history
Add DummyCookieJar helper
  • Loading branch information
fafhrd91 authored Apr 22, 2017
2 parents f9df0e8 + 659550b commit 0ed47a2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
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()

0 comments on commit 0ed47a2

Please sign in to comment.