def request(self, method, url, payload): """ Called for each Chargebee request Adds the request content to MockEnvironment.requests Matches the next expected request and returns the associated content Returns a (json_body_string, http_status) tuple """ from chargebee.compat import urlsplit, parse_qs, json url_obj = urlsplit(url) try: url_path = url_obj.path.split('/api/%s' % self.API_VERSION)[1] except IndexError: raise ValueError("Request URL (%s) didn't match endpoint %s" % (url, self.api_endpoint)) # un-encode the body/querystring payload payload = parse_qs(payload or url_obj.query, keep_blank_values=True) # save to MockEnvironment.requests self.requests.append(MockEnvironment.Request(method, url_path, payload)) # check the next expected request try: req_path, req_method, resp_body, resp_status = self.expecting.pop( 0) except IndexError: raise AssertionError("No more mock requests left: %s" % url_path) # Match the URL path if isinstance(req_path, re._pattern_type): # url_path is a regex if not req_path.match(url_path): raise AssertionError("Request URL %s does not match %s" % (url_path, req_path.pattern)) elif req_path != url_path: # url_path is a string raise AssertionError("Request URL %s != %s" % (url_path, req_path)) # Match any request method if req_method and method != req_method.upper(): raise AssertionError("Request Method %s != %s (%s)" % (method, req_method, url_path)) # Return the response if callable(resp_body): # response_body is a callable, use it to generate the response return resp_body(url_path, payload, method) elif isinstance(resp_body, (dict, list, tuple)): # response body is a dict/sequence, JSON-encode it return (json.dumps(resp_body), resp_status) else: # string body return (resp_body, resp_status)
def __str__(self): return json.dumps(self.values, indent=4)
def __str__(self): return json.dumps(self._response, indent=4)