Yr.no: New aiohttp client needs params to form websession URL (#4634)
* Yr.no: New aiohttp client needs params to form websession URL * Support params in aiohttp mocking
This commit is contained in:
parent
bde7176b3c
commit
c6c8cd4f51
2 changed files with 19 additions and 9 deletions
|
@ -5,6 +5,7 @@ import functools
|
|||
import json as _json
|
||||
from unittest import mock
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
import yarl
|
||||
|
||||
|
||||
class AiohttpClientMocker:
|
||||
|
@ -20,7 +21,8 @@ class AiohttpClientMocker:
|
|||
status=200,
|
||||
text=None,
|
||||
content=None,
|
||||
json=None):
|
||||
json=None,
|
||||
params=None):
|
||||
"""Mock a request."""
|
||||
if json:
|
||||
text = _json.dumps(json)
|
||||
|
@ -28,6 +30,8 @@ class AiohttpClientMocker:
|
|||
content = text.encode('utf-8')
|
||||
if content is None:
|
||||
content = b''
|
||||
if params:
|
||||
url = str(yarl.URL(url).with_query(params))
|
||||
|
||||
self._mocks.append(AiohttpClientMockResponse(
|
||||
method, url, status, content))
|
||||
|
@ -58,11 +62,11 @@ class AiohttpClientMocker:
|
|||
return len(self.mock_calls)
|
||||
|
||||
@asyncio.coroutine
|
||||
def match_request(self, method, url, *, auth=None): \
|
||||
def match_request(self, method, url, *, auth=None, params=None): \
|
||||
# pylint: disable=unused-variable
|
||||
"""Match a request against pre-registered requests."""
|
||||
for response in self._mocks:
|
||||
if response.match_request(method, url):
|
||||
if response.match_request(method, url, params):
|
||||
self.mock_calls.append((method, url))
|
||||
return response
|
||||
|
||||
|
@ -82,11 +86,14 @@ class AiohttpClientMockResponse:
|
|||
self.status = status
|
||||
self.response = response
|
||||
|
||||
def match_request(self, method, url):
|
||||
def match_request(self, method, url, params=None):
|
||||
"""Test if response answers request."""
|
||||
if method.lower() != self.method.lower():
|
||||
return False
|
||||
|
||||
if params:
|
||||
url = str(yarl.URL(url).with_query(params))
|
||||
|
||||
# regular expression matching
|
||||
if self._url_parts is None:
|
||||
return self._url.search(url) is not None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue