33import errno
44import hashlib
55import io
6+ import itertools
67import json
78import os
89import select
1617from urllib3 .util .ssl_ import ssl_wrap_socket as urllib3_ssl_wrap_socket
1718from urllib3 .util .ssl_ import wrap_socket as urllib3_wrap_socket
1819
19- from .compat import (
20- basestring ,
21- byte_type ,
22- decode_from_bytes ,
23- encode_to_bytes ,
24- text_type ,
25- )
20+ from .compat import basestring , byte_type , decode_from_bytes , encode_to_bytes , text_type
2621from .utils import SSL_PROTOCOL , MocketSocketCore , hexdump , hexload , wrap_ssl_socket
2722
2823xxh32 = None
@@ -517,6 +512,13 @@ def get_namespace(cls):
517512 def get_truesocket_recording_dir (cls ):
518513 return cls ._truesocket_recording_dir
519514
515+ @classmethod
516+ def assert_fail_if_entries_not_served (cls ):
517+ """ Mocket checks that all entries have been served at least once. """
518+ assert all (
519+ entry ._served for entry in itertools .chain (* cls ._entries .values ())
520+ ), "Some Mocket entries have not been served"
521+
520522
521523class MocketEntry (object ):
522524 class Response (byte_type ):
@@ -526,8 +528,11 @@ def data(self):
526528
527529 request_cls = str
528530 response_cls = Response
531+ responses = None
532+ _served = None
529533
530534 def __init__ (self , location , responses ):
535+ self ._served = False
531536 self .location = location
532537 self .response_index = 0
533538
@@ -536,19 +541,18 @@ def __init__(self, location, responses):
536541 ):
537542 responses = [responses ]
538543
539- lresponses = []
544+ self . responses = []
540545 for r in responses :
541546 if isinstance (r , BaseException ):
542547 pass
543548 elif not getattr (r , "data" , False ):
544549 if isinstance (r , text_type ):
545550 r = encode_to_bytes (r )
546551 r = self .response_cls (r )
547- lresponses .append (r )
552+ self . responses .append (r )
548553 else :
549554 if not responses :
550- lresponses = [self .response_cls (encode_to_bytes ("" ))]
551- self .responses = lresponses
555+ self .responses = [self .response_cls (encode_to_bytes ("" ))]
552556
553557 def can_handle (self , data ):
554558 return True
@@ -562,6 +566,8 @@ def get_response(self):
562566 if self .response_index < len (self .responses ) - 1 :
563567 self .response_index += 1
564568
569+ self ._served = True
570+
565571 if isinstance (response , BaseException ):
566572 raise response
567573
0 commit comments