177
177
self.responses = []
179
179
self.expecting_body = False
180
# if non-None, this is the list of expected calls, with only the
181
# method name and arguments included. the body might be hard to
182
# compute so is not included
183
self._expected_calls = None
180
184
_SmartClient.__init__(self, FakeMedium(self._calls, fake_medium_base))
186
def add_expected_call(self, call_name, call_args, response_type,
187
response_args, response_body=None):
188
if self._expected_calls is None:
189
self._expected_calls = []
190
self._expected_calls.append((call_name, call_args))
191
self.responses.append((response_types, response_args, response_body))
182
193
def add_success_response(self, *args):
183
194
self.responses.append(('success', args, None))
192
203
self.responses.append(('unknown', verb))
194
205
def _get_next_response(self):
195
response_tuple = self.responses.pop(0)
207
response_tuple = self.responses.pop(0)
208
except IndexError, e:
209
raise AssertionError("%r didn't expect any more calls"
196
211
if response_tuple[0] == 'unknown':
197
212
raise errors.UnknownSmartMethod(response_tuple[1])
198
213
elif response_tuple[0] == 'error':
199
214
raise errors.ErrorFromSmartServer(response_tuple[1])
200
215
return response_tuple
217
def _check_call(self, method, args):
218
if self._expected_calls is None:
219
# the test should be updated to say what it expects
222
next_call = self._expected_calls.pop(0)
224
raise AssertionError("%r didn't expect any more calls "
227
if method != next_call[0] or args != next_call[1]:
228
raise AssertionError("%r expected %r%r "
230
% (next_call[0], next_call[1], method, args,))
202
232
def call(self, method, *args):
233
self._check_call(method, args)
203
234
self._calls.append(('call', method, args))
204
235
return self._get_next_response()[1]
206
237
def call_expecting_body(self, method, *args):
238
self._check_call(method, args)
207
239
self._calls.append(('call_expecting_body', method, args))
208
240
result = self._get_next_response()
209
241
self.expecting_body = True
210
242
return result[1], FakeProtocol(result[2], self)
212
244
def call_with_body_bytes_expecting_body(self, method, args, body):
245
self._check_call(method, args)
213
246
self._calls.append(('call_with_body_bytes_expecting_body', method,
215
248
result = self._get_next_response()