Skip to content

Commit

Permalink
make_mocked_request now accepts dict as headers (#1073)
Browse files Browse the repository at this point in the history
* make_mocked_request now accepts dict as headers

* Updated docs for make_mocked_request
  • Loading branch information
argaen authored and asvetlov committed Aug 15, 2016
1 parent 4d8ea68 commit 8a37136
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
7 changes: 4 additions & 3 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,9 @@ def make_mocked_request(method, path, headers=None, *,
:param path: str, The URL including *PATH INFO* without the host or scheme
:type path: str
:param headers: CIMultiDict with all request headers
:type headers: multidict.CIMultiDict
:param headers: mapping containing the headers. Can be anything accepted
by the multidict.CIMultiDict constructor.
:type headers: dict, multidict.CIMultiDict, list of pairs
:param version: namedtuple with encoded HTTP version
:type version: aiohttp.protocol.HttpVersion
Expand Down Expand Up @@ -344,7 +345,7 @@ def make_mocked_request(method, path, headers=None, *,
closing = True

if headers:
hdrs = headers
hdrs = CIMultiDict(headers)
raw_hdrs = [
(k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items()]
else:
Expand Down
3 changes: 1 addition & 2 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,13 @@ conditions that hard to reproduce on real server::

from aiohttp import web
from aiohttp.test_utils import make_mocked_request
from multidict import CIMultiDict

def handler(request):
assert request.headers.get('token') == 'x'
return web.Response(body=b'data')

def test_handler():
req = make_mocked_request('GET', '/', headers=CIMultiDict({'token': 'x'}))
req = make_mocked_request('GET', '/', headers={'token': 'x'})
resp = handler(req)
assert resp.body == b'data'

Expand Down
17 changes: 14 additions & 3 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import asyncio

import pytest
from multidict import CIMultiDict, CIMultiDictProxy

import aiohttp
from aiohttp import web
from aiohttp import web, web_reqrep
from aiohttp.test_utils import (AioHTTPTestCase, TestClient, loop_context,
setup_test_loop, teardown_test_loop,
unittest_run_loop)
make_mocked_request, setup_test_loop,
teardown_test_loop, unittest_run_loop)


def _create_example_app(loop):
Expand Down Expand Up @@ -180,3 +181,13 @@ def test_test_client_methods(method, loop, test_client):
def test_test_client_head(loop, test_client):
resp = yield from test_client.head("/")
assert resp.status == 200


@pytest.mark.parametrize(
"headers", [{'token': 'x'}, CIMultiDict({'token': 'x'}), {}])
def test_make_mocked_request(headers):
req = make_mocked_request('GET', '/', headers=headers)
assert req.method == "GET"
assert req.path == "/"
assert isinstance(req, web_reqrep.Request)
assert isinstance(req.headers, CIMultiDictProxy)

0 comments on commit 8a37136

Please sign in to comment.