Skip to content

Commit

Permalink
fix startswith error
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuckowski committed May 18, 2017
1 parent 6ec5c75 commit 867782f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 48 deletions.
26 changes: 11 additions & 15 deletions aioresponses/core.py
@@ -1,21 +1,16 @@
# -*- coding: utf-8 -*-

import asyncio
import collections
import json

from functools import wraps
from multidict import CIMultiDict

try:
from typing import Dict, Tuple
except ImportError:
Dict = dict
Tuple = tuple
from typing import Dict, Tuple
from unittest.mock import patch
from urllib.parse import urlparse, parse_qsl, urlencode

import collections
from aiohttp import hdrs, ClientResponse, ClientConnectionError, client
from functools import wraps
from multidict import CIMultiDict

from .compat import URL


Expand Down Expand Up @@ -133,8 +128,8 @@ def add(self, url: str, method: str = hdrs.METH_GET, status: int = 200,
body: str = '',
exception: 'Exception' = None,
content_type: str = 'application/json',
payload: Dict=None,
headers: Dict=None) -> None:
payload: Dict = None,
headers: Dict = None) -> None:
self._responses.append(UrlResponse(
url,
method=method,
Expand Down Expand Up @@ -166,9 +161,10 @@ def _request_mock(self, orig_self: client.ClientSession,
**kwargs: Dict) -> 'ClientResponse':
"""Return mocked response object or raise connection error."""
for prefix in self._passthrough:
if url.startswith(prefix):
return self.patcher.temp_original(
orig_self, method, url, *args, **kwargs)
if str(url).startswith(prefix):
return (yield from self.patcher.temp_original(
orig_self, method, url, *args, **kwargs
))

response = self.match(method, url)
if response is None:
Expand Down
18 changes: 9 additions & 9 deletions requirements-dev.txt
@@ -1,11 +1,11 @@
pip
bumpversion
wheel
watchdog
flake8
tox
coverage
Sphinx
nose2
cov-core
ddt
watchdog==0.8.3
flake8==3.3.0
tox==2.7.0
coverage==4.4.1
Sphinx==1.5.6
nose2==0.6.5
cov-core==1.15.0
ddt==1.1.1
typing
13 changes: 0 additions & 13 deletions setup.cfg
Expand Up @@ -25,19 +25,6 @@ all_files = 1
build-dir = docs/build
source-dir = docs/source

[bumpversion]
current_version = 0.1.0
commit = True
tag = True

[bumpversion:file:setup.py]
search = version='{current_version}'
replace = version='{new_version}'

[bumpversion:file:aioresponses/__init__.py]
search = __version__ = '{current_version}'
replace = __version__ = '{new_version}'

[bdist_wheel]
universal = 1

Expand Down
32 changes: 22 additions & 10 deletions tests/test_aioresponses.py
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
import asyncio
from unittest.case import TestCase
from unittest.mock import patch, Mock
from unittest.mock import patch

from aiohttp import hdrs
from aiohttp.client import ClientSession
from aiohttp.client_reqrep import ClientResponse

from aioresponses.compat import URL

try:
from aiohttp.errors import ClientConnectionError, HttpProcessingError
except ImportError:
Expand Down Expand Up @@ -136,13 +138,23 @@ def test_multiple_requests(self):
self.assertIn(key, m.requests)
self.assertEqual(len(m.requests[key]), 3)
self.assertEqual(m.requests[key][0].args, tuple())
self.assertEqual(m.requests[key][0].kwargs, {'allow_redirects': True})
self.assertEqual(m.requests[key][0].kwargs,
{'allow_redirects': True})

def test_passthrough(self):
self.session._request = mocked = Mock()
mocked.side_effect = asyncio.coroutine(
lambda method, url, *args, **kwargs: None)
with aioresponses(passthrough=['http://example.com']) as m:
resp = self.loop.run_until_complete(self.session.get(self.url))
mocked.assert_called_once_with(
'GET', 'http://example.com/api', allow_redirects=True)
def test_address_as_instance_of_url_combined_with_pass_through(self):
external_api = 'http://google.com'

@asyncio.coroutine
def doit():
api_resp = yield from self.session.get(self.url)
# we have to hit actual url,
# otherwise we do not test pass through option properly
ext_rep = yield from self.session.get(URL(external_api))
return api_resp, ext_rep

with aioresponses(passthrough=[external_api]) as m:
m.get(self.url, status=200)
api, ext = self.loop.run_until_complete(doit())

self.assertEqual(api.status, 200)
self.assertEqual(ext.status, 200)
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -2,7 +2,7 @@
envlist =
flake8,
coverage,
{py34,py35}-aiohttp{10,11,12,master}
{py34,py35}-aiohttp{10,11,12,13,20,master}
skipsdist=True

[testenv:flake8]
Expand Down

0 comments on commit 867782f

Please sign in to comment.