Skip to content

Commit

Permalink
url_match
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerza committed Nov 2, 2016
1 parent 07f5e7a commit 1c999d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
14 changes: 12 additions & 2 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ reports=no
# locally-disabled - it spams too much
# duplicate-code - unavoidable
# cyclic-import - doesn't test if both import on load
# abstract-class-little-used - Prevents from setting right foundation
# abstract-class-little-used - prevents from setting right foundation
# abstract-class-not-used - is flaky, should not show up but does
# unused-argument - generic callbacks and setup methods create a lot of warnings
# global-statement - used for the on-demand requirement installation
# redefined-variable-type - this is Python, we're duck typing!
# too-many-* - are not enforced for the sake of readability
# too-few-* - same as too-many-*

disable=
locally-disabled,
duplicate-code,
Expand All @@ -19,7 +22,14 @@ disable=
unused-argument,
global-statement,
redefined-variable-type,
unused-variable
too-many-arguments,
too-many-branches,
too-many-instance-attributes,
too-many-locals,
too-many-public-methods,
too-many-return-statements,
too-many-statements,
too-few-public-methods,

[EXCEPTIONS]
overgeneral-exceptions=Exception,HomeAssistantError
35 changes: 27 additions & 8 deletions tests/test_util/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import functools
import json as _json
from unittest import mock
from urllib.parse import urlparse
from urllib.parse import urlparse, parse_qs


class AiohttpClientMocker:
Expand Down Expand Up @@ -77,8 +77,8 @@ def __init__(self, method, url, status, response):
"""Initialize a fake response."""
self.method = method
self._url = url
self._url_parts = urlparse(url.lower()) \
if isinstance(url, str) else None
self._url_parts = (None if hasattr(url, 'search')
else urlparse(url.lower()))
self.status = status
self.response = response

Expand All @@ -87,11 +87,30 @@ def match_request(self, method, url):
if method.lower() != self.method.lower():
return False

# Reuse Mock request's match function
# pylint: disable=protected-access, attribute-defined-outside-init
from requests_mock.adapter import _Matcher
self._complete_qs = False
return _Matcher._match_url(self, urlparse(url.lower()))
# regular expression matching
if self._url_parts is None:
return self._url.search(url) is not None

req = urlparse(url.lower())

if self._url_parts.scheme and req.scheme != self._url_parts.scheme:
return False
if self._url_parts.netloc and req.netloc != self._url_parts.netloc:
return False
if (req.path or '/') != (self._url_parts.path or '/'):
return False

# Ensure all query components in matcher are present in the request
request_qs = parse_qs(req.query)
matcher_qs = parse_qs(self._url_parts.query)
for key, vals in matcher_qs.items():
for val in vals:
try:
request_qs.get(key, []).remove(val)
except ValueError:
return False

return True

@asyncio.coroutine
def read(self):
Expand Down

0 comments on commit 1c999d3

Please sign in to comment.