Skip to content

Commit

Permalink
feat(#33): proxy mock defintions into mock.Request
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Jan 28, 2017
1 parent 5c55f27 commit 2c6a16d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pook/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def unmatched_requests():
mode has been enabled.
Returns:
tuple: unmatched intercepted requests.
list: unmatched intercepted requests.
"""
return _engine.unmatched_requests()

Expand Down
4 changes: 2 additions & 2 deletions pook/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ def unmatched_requests(self):
has been enabled.
Returns:
tuple: unmatched intercepted requests.
list: unmatched intercepted requests.
"""
return (mock for mock in self.unmatched_reqs)
return [mock for mock in self.unmatched_reqs]

def unmatched(self):
"""
Expand Down
20 changes: 18 additions & 2 deletions pook/mock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
import functools
from furl import furl
from inspect import isfunction, ismethod

from .decorators import fluent
from .response import Response
from .constants import TYPES
Expand Down Expand Up @@ -112,7 +114,7 @@ def __init__(self, request=None, response=None, **kw):
# Store request-response mock matched calls
self._calls = []
# Stores the input request instance
self._request = request
self._request = request or Request()
# Stores the response mock instance
self._response = response or Response()
# Stores the mock matcher engine used for outgoing traffic matching
Expand All @@ -128,7 +130,7 @@ def __init__(self, request=None, response=None, **kw):
# Triggers instance methods based on argument names
trigger_methods(self, kw)

# Trigger request fields, if present
# Trigger matchers based on predefined request object, if needed
if request:
_trigger_request(self, request)

Expand All @@ -146,6 +148,7 @@ def url(self, url):
Returns:
self: current Mock instance.
"""
self._request.url = url
self.add_matcher(matcher('URLMatcher', url))

@fluent
Expand All @@ -160,6 +163,7 @@ def method(self, method):
Returns:
self: current Mock instance.
"""
self._request.method = method
self.add_matcher(matcher('MethodMatcher', method))

@fluent
Expand All @@ -175,6 +179,9 @@ def path(self, path):
Returns:
self: current Mock instance.
"""
url = furl(self._request.rawurl)
url.path = path
self._request.url = url.url
self.add_matcher(matcher('PathMatcher', path))

@fluent
Expand All @@ -191,6 +198,7 @@ def header(self, name, value):
self: current Mock instance.
"""
headers = {name: value}
self._request.headers = headers
self.add_matcher(matcher('HeadersMatcher', headers))

@fluent
Expand All @@ -208,6 +216,7 @@ def headers(self, headers=None, **kw):
self: current Mock instance.
"""
headers = kw if kw else headers
self._request.headers = headers
self.add_matcher(matcher('HeadersMatcher', headers))

@fluent
Expand Down Expand Up @@ -304,6 +313,7 @@ def content(self, value):
self: current Mock instance.
"""
header = {'Content-Type': TYPES.get(value, value)}
self._request.headers = header
self.add_matcher(matcher('HeadersMatcher', header))

@fluent
Expand Down Expand Up @@ -344,6 +354,9 @@ def params(self, params):
Returns:
self: current Mock instance.
"""
url = furl(self._request.rawurl)
url = url.add(params)
self._request.url = url.url
self.add_matcher(matcher('QueryMatcher', params))

@fluent
Expand All @@ -359,6 +372,7 @@ def body(self, body):
Returns:
self: current Mock instance.
"""
self._request.body = body
self.add_matcher(matcher('BodyMatcher', body))

@fluent
Expand All @@ -376,6 +390,7 @@ def json(self, json):
Returns:
self: current Mock instance.
"""
self._request.json = json
self.add_matcher(matcher('JSONMatcher', json))

@fluent
Expand All @@ -402,6 +417,7 @@ def xml(self, xml):
Returns:
self: current Mock instance.
"""
self._request.xml = xml
self.add_matcher(matcher('XMLMatcher', xml))

@fluent
Expand Down
8 changes: 8 additions & 0 deletions pook/request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys
import json as _json

from .headers import HTTPHeaderDict
from .helpers import trigger_methods
from .matchers.url import protoregex

if sys.version_info < (3,): # Python 2
from urlparse import urlparse, parse_qs, urlunparse
Expand Down Expand Up @@ -79,8 +81,14 @@ def extra(self, extra):
def url(self):
return self._url

@property
def rawurl(self):
return urlunparse(self._url)

@url.setter
def url(self, url):
if not protoregex.match(url):
url = 'http://{}'.format(url)
self._url = urlparse(url)
self._query = parse_qs(self._url.query)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jsonschema~=2.5.1
xmltodict~=0.10.2
furl~=0.5.6

0 comments on commit 2c6a16d

Please sign in to comment.