Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__all__ = ("mocketize", "Mocket", "MocketEntry", "Mocketizer")

__version__ = "3.10.0"
__version__ = "3.10.1"
18 changes: 11 additions & 7 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,19 @@ def sendall(self, data, entry=None, *args, **kwargs):
entry = self.get_entry(data)

if entry:
entry.collect(data)
response = entry.get_response()
consume_response = entry.collect(data)
if consume_response is not False:
response = entry.get_response()
else:
response = None
else:
response = self.true_sendall(data, *args, **kwargs)

self.fd.seek(0)
self.fd.write(response)
self.fd.truncate()
self.fd.seek(0)
if response is not None:
self.fd.seek(0)
self.fd.write(response)
self.fd.truncate()
self.fd.seek(0)

def read(self, buffersize):
return self.fd.read(buffersize)
Expand Down Expand Up @@ -539,6 +543,7 @@ class Response(byte_type):
def data(self):
return self

response_index = 0
request_cls = str
response_cls = Response
responses = None
Expand All @@ -547,7 +552,6 @@ def data(self):
def __init__(self, location, responses):
self._served = False
self.location = location
self.response_index = 0

if not isinstance(responses, collections_abc.Iterable) or isinstance(
responses, basestring
Expand Down
17 changes: 12 additions & 5 deletions mocket/mockhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ def __init__(self, uri, method, responses, match_querystring=True):
self._match_querystring = match_querystring

def collect(self, data):
consume_response = True

decoded_data = decode_from_bytes(data)
if not decoded_data.startswith(Entry.METHODS):
Mocket.remove_last_request()
self._sent_data += data
consume_response = False
else:
self._sent_data = data

super(Entry, self).collect(self._sent_data)

return consume_response

def can_handle(self, data):
r"""
>>> e = Entry('http://www.github.com/?bar=foo&foobar', Entry.GET, (Response(b'<html/>'),))
Expand All @@ -170,10 +176,8 @@ def can_handle(self, data):
requestline, _ = decode_from_bytes(data).split(CRLF, 1)
method, path, version = self._parse_requestline(requestline)
except ValueError:
try:
return self == Mocket._last_entry
except AttributeError:
return False
return self is getattr(Mocket, "_last_entry", None)

uri = urlsplit(path)
can_handle = uri.path == self.path and method == self.method
if self._match_querystring:
Expand Down Expand Up @@ -240,5 +244,8 @@ def single_register(
)

cls.register(
method, uri, response, match_querystring=match_querystring,
method,
uri,
response,
match_querystring=match_querystring,
)
27 changes: 27 additions & 0 deletions tests/main/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,30 @@ def test_does_not_fail_because_all_entries_are_served(self):
requests.get(url)
requests.get(second_url)
Mocket.assert_fail_if_entries_not_served()

@mocketize
def test_multi_register(self):
url = "http://foobar.com/path"
Entry.register(
Entry.POST,
url,
Response(body='{"foo":"bar0"}', status=200),
Response(body='{"foo":"bar1"}', status=201),
Response(body='{"foo":"bar2"}', status=202),
)

response = requests.post(url, json={"test": 0})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"foo": "bar0"})

response = requests.post(url, json={"test": 1})
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), {"foo": "bar1"})

response = requests.post(url, json={"test": 2})
self.assertEqual(response.status_code, 202)
self.assertEqual(response.json(), {"foo": "bar2"})

response = requests.post(url, json={"test": 22})
self.assertEqual(response.status_code, 202)
self.assertEqual(response.json(), {"foo": "bar2"})