@@ -51,20 +51,16 @@ def assert_esp_called(self, url, method="POST"):
5151 # This assumes the last (or only) call to requests.Session.request is the API call of interest.
5252 if self .mock_request .call_args is None :
5353 raise AssertionError ("No ESP API was called" )
54- (args , kwargs ) = self .mock_request .call_args
55- try :
56- actual_method = kwargs .get ('method' , None ) or args [1 ]
57- actual_url = kwargs .get ('url' , None ) or args [2 ]
58- except IndexError :
59- raise AssertionError ("API was called without a method or url (?!)" )
60- if actual_method != method :
61- raise AssertionError ("API was not called using %s. (%s was used instead.)"
62- % (method , actual_method ))
63- if not actual_url .endswith (url ):
64- raise AssertionError ("API was not called at %s\n (It was called at %s)"
65- % (url , actual_url ))
66-
67- def get_api_call_arg (self , kwarg , pos , required = True ):
54+ if method is not None :
55+ actual_method = self .get_api_call_arg ('method' )
56+ if actual_method != method :
57+ self .fail ("API was not called using %s. (%s was used instead.)" % (method , actual_method ))
58+ if url is not None :
59+ actual_url = self .get_api_call_arg ('url' )
60+ if not actual_url .endswith (url ):
61+ self .fail ("API was not called at %s\n (It was called at %s)" % (url , actual_url ))
62+
63+ def get_api_call_arg (self , kwarg , required = True ):
6864 """Returns an argument passed to the mock ESP API.
6965
7066 Fails test if API wasn't called.
@@ -73,36 +69,51 @@ def get_api_call_arg(self, kwarg, pos, required=True):
7369 raise AssertionError ("API was not called" )
7470 (args , kwargs ) = self .mock_request .call_args
7571 try :
76- return kwargs .get (kwarg , None ) or args [pos ]
77- except IndexError :
78- if required :
79- raise AssertionError ("API was called without required %s" % kwarg )
80- else :
81- return None
72+ return kwargs [kwarg ]
73+ except KeyError :
74+ pass
75+
76+ try :
77+ # positional arg? This is the order of requests.Session.request params:
78+ pos = ('method' , 'url' , 'params' , 'data' , 'headers' , 'cookies' , 'files' , 'auth' ,
79+ 'timeout' , 'allow_redirects' , 'proxies' , 'hooks' , 'stream' , 'verify' , 'cert' , 'json' ,
80+ ).index (kwarg )
81+ return args [pos ]
82+ except (ValueError , IndexError ):
83+ pass
84+
85+ if required :
86+ self .fail ("API was called without required arg '%s'" % kwarg )
87+ return None
8288
8389 def get_api_call_params (self , required = True ):
8490 """Returns the query params sent to the mock ESP API."""
85- return self .get_api_call_arg ('params' , 3 , required )
91+ return self .get_api_call_arg ('params' , required )
8692
8793 def get_api_call_data (self , required = True ):
8894 """Returns the raw data sent to the mock ESP API."""
89- return self .get_api_call_arg ('data' , 4 , required )
95+ return self .get_api_call_arg ('data' , required )
9096
9197 def get_api_call_json (self , required = True ):
9298 """Returns the data sent to the mock ESP API, json-parsed"""
93- return json .loads (self .get_api_call_data (required ))
99+ # could be either the data param (as json str) or the json param (needing formatting)
100+ value = self .get_api_call_arg ('data' , required = False )
101+ if value is not None :
102+ return json .loads (value )
103+ else :
104+ return self .get_api_call_arg ('json' , required )
94105
95106 def get_api_call_headers (self , required = True ):
96107 """Returns the headers sent to the mock ESP API"""
97- return self .get_api_call_arg ('headers' , 5 , required )
108+ return self .get_api_call_arg ('headers' , required )
98109
99110 def get_api_call_files (self , required = True ):
100111 """Returns the files sent to the mock ESP API"""
101- return self .get_api_call_arg ('files' , 7 , required )
112+ return self .get_api_call_arg ('files' , required )
102113
103114 def get_api_call_auth (self , required = True ):
104115 """Returns the auth sent to the mock ESP API"""
105- return self .get_api_call_arg ('auth' , 8 , required )
116+ return self .get_api_call_arg ('auth' , required )
106117
107118 def assert_esp_not_called (self , msg = None ):
108119 if self .mock_request .called :
0 commit comments