Skip to content

Commit dbe6109

Browse files
authored
Multi register() (#159)
* Add failing test. * `Entry.collect` let us know if the response should be consumed or not. * Bump version.
1 parent 3e05f95 commit dbe6109

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

mocket/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

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

5-
__version__ = "3.10.0"
5+
__version__ = "3.10.1"

mocket/mocket.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,19 @@ def sendall(self, data, entry=None, *args, **kwargs):
253253
entry = self.get_entry(data)
254254

255255
if entry:
256-
entry.collect(data)
257-
response = entry.get_response()
256+
consume_response = entry.collect(data)
257+
if consume_response is not False:
258+
response = entry.get_response()
259+
else:
260+
response = None
258261
else:
259262
response = self.true_sendall(data, *args, **kwargs)
260263

261-
self.fd.seek(0)
262-
self.fd.write(response)
263-
self.fd.truncate()
264-
self.fd.seek(0)
264+
if response is not None:
265+
self.fd.seek(0)
266+
self.fd.write(response)
267+
self.fd.truncate()
268+
self.fd.seek(0)
265269

266270
def read(self, buffersize):
267271
return self.fd.read(buffersize)
@@ -539,6 +543,7 @@ class Response(byte_type):
539543
def data(self):
540544
return self
541545

546+
response_index = 0
542547
request_cls = str
543548
response_cls = Response
544549
responses = None
@@ -547,7 +552,6 @@ def data(self):
547552
def __init__(self, location, responses):
548553
self._served = False
549554
self.location = location
550-
self.response_index = 0
551555

552556
if not isinstance(responses, collections_abc.Iterable) or isinstance(
553557
responses, basestring

mocket/mockhttp.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,20 @@ def __init__(self, uri, method, responses, match_querystring=True):
149149
self._match_querystring = match_querystring
150150

151151
def collect(self, data):
152+
consume_response = True
153+
152154
decoded_data = decode_from_bytes(data)
153155
if not decoded_data.startswith(Entry.METHODS):
154156
Mocket.remove_last_request()
155157
self._sent_data += data
158+
consume_response = False
156159
else:
157160
self._sent_data = data
161+
158162
super(Entry, self).collect(self._sent_data)
159163

164+
return consume_response
165+
160166
def can_handle(self, data):
161167
r"""
162168
>>> e = Entry('http://www.github.com/?bar=foo&foobar', Entry.GET, (Response(b'<html/>'),))
@@ -170,10 +176,8 @@ def can_handle(self, data):
170176
requestline, _ = decode_from_bytes(data).split(CRLF, 1)
171177
method, path, version = self._parse_requestline(requestline)
172178
except ValueError:
173-
try:
174-
return self == Mocket._last_entry
175-
except AttributeError:
176-
return False
179+
return self is getattr(Mocket, "_last_entry", None)
180+
177181
uri = urlsplit(path)
178182
can_handle = uri.path == self.path and method == self.method
179183
if self._match_querystring:
@@ -240,5 +244,8 @@ def single_register(
240244
)
241245

242246
cls.register(
243-
method, uri, response, match_querystring=match_querystring,
247+
method,
248+
uri,
249+
response,
250+
match_querystring=match_querystring,
244251
)

tests/main/test_http.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,30 @@ def test_does_not_fail_because_all_entries_are_served(self):
382382
requests.get(url)
383383
requests.get(second_url)
384384
Mocket.assert_fail_if_entries_not_served()
385+
386+
@mocketize
387+
def test_multi_register(self):
388+
url = "http://foobar.com/path"
389+
Entry.register(
390+
Entry.POST,
391+
url,
392+
Response(body='{"foo":"bar0"}', status=200),
393+
Response(body='{"foo":"bar1"}', status=201),
394+
Response(body='{"foo":"bar2"}', status=202),
395+
)
396+
397+
response = requests.post(url, json={"test": 0})
398+
self.assertEqual(response.status_code, 200)
399+
self.assertEqual(response.json(), {"foo": "bar0"})
400+
401+
response = requests.post(url, json={"test": 1})
402+
self.assertEqual(response.status_code, 201)
403+
self.assertEqual(response.json(), {"foo": "bar1"})
404+
405+
response = requests.post(url, json={"test": 2})
406+
self.assertEqual(response.status_code, 202)
407+
self.assertEqual(response.json(), {"foo": "bar2"})
408+
409+
response = requests.post(url, json={"test": 22})
410+
self.assertEqual(response.status_code, 202)
411+
self.assertEqual(response.json(), {"foo": "bar2"})

0 commit comments

Comments
 (0)