Skip to content

Commit

Permalink
New mocket method for checking if all entries have been served (#137)
Browse files Browse the repository at this point in the history
* Mocket method for checking if all entries have been served at least once.
  • Loading branch information
mindflayer committed Jan 11, 2021
1 parent 9bb7c4b commit b64c5df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
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.9.35"
__version__ = "3.9.36"
28 changes: 17 additions & 11 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import errno
import hashlib
import io
import itertools
import json
import os
import select
Expand All @@ -16,13 +17,7 @@
from urllib3.util.ssl_ import ssl_wrap_socket as urllib3_ssl_wrap_socket
from urllib3.util.ssl_ import wrap_socket as urllib3_wrap_socket

from .compat import (
basestring,
byte_type,
decode_from_bytes,
encode_to_bytes,
text_type,
)
from .compat import basestring, byte_type, decode_from_bytes, encode_to_bytes, text_type
from .utils import SSL_PROTOCOL, MocketSocketCore, hexdump, hexload, wrap_ssl_socket

xxh32 = None
Expand Down Expand Up @@ -517,6 +512,13 @@ def get_namespace(cls):
def get_truesocket_recording_dir(cls):
return cls._truesocket_recording_dir

@classmethod
def assert_fail_if_entries_not_served(cls):
""" Mocket checks that all entries have been served at least once. """
assert all(
entry._served for entry in itertools.chain(*cls._entries.values())
), "Some Mocket entries have not been served"


class MocketEntry(object):
class Response(byte_type):
Expand All @@ -526,8 +528,11 @@ def data(self):

request_cls = str
response_cls = Response
responses = None
_served = None

def __init__(self, location, responses):
self._served = False
self.location = location
self.response_index = 0

Expand All @@ -536,19 +541,18 @@ def __init__(self, location, responses):
):
responses = [responses]

lresponses = []
self.responses = []
for r in responses:
if isinstance(r, BaseException):
pass
elif not getattr(r, "data", False):
if isinstance(r, text_type):
r = encode_to_bytes(r)
r = self.response_cls(r)
lresponses.append(r)
self.responses.append(r)
else:
if not responses:
lresponses = [self.response_cls(encode_to_bytes(""))]
self.responses = lresponses
self.responses = [self.response_cls(encode_to_bytes(""))]

def can_handle(self, data):
return True
Expand All @@ -562,6 +566,8 @@ def get_response(self):
if self.response_index < len(self.responses) - 1:
self.response_index += 1

self._served = True

if isinstance(response, BaseException):
raise response

Expand Down
19 changes: 19 additions & 0 deletions tests/main/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,22 @@ def test_sockets(self):

# Proof that worked.
self.assertEqual(Mocket.last_request().body, '{"hello": "world"}')

@mocketize
def test_fail_because_entry_not_served(self):
url = "http://github.com/fluidicon.png"
Entry.single_register(Entry.GET, url)
Entry.single_register(Entry.GET, "http://github.com/fluidicon.jpg")
requests.get(url)
with self.assertRaises(AssertionError):
Mocket.assert_fail_if_entries_not_served()

@mocketize
def test_does_not_fail_because_all_entries_are_served(self):
url = "http://github.com/fluidicon.png"
second_url = "http://github.com/fluidicon.jpg"
Entry.single_register(Entry.GET, url)
Entry.single_register(Entry.GET, second_url)
requests.get(url)
requests.get(second_url)
Mocket.assert_fail_if_entries_not_served()

0 comments on commit b64c5df

Please sign in to comment.