def deserialize_prepared_request(serialized): p = PreparedRequest() p.body = serialized['body'] p.headers = CaseInsensitiveDict(serialized['headers']) p.method = serialized['method'] p.url = serialized['uri'] return p
def send(self, request: PreparedRequest, stream=False, timeout=None, verify=True, cert=None, proxies=None): request.url = request.url.decode("utf-8") if isinstance( request.url, bytes) else request.url # Check URL, ask the fake Icinga to handle it self.check_path(request.url) resp = self.icinga.handle(request) # Create response, emulate equests.adapters.HTTPAdapter behavior a bit response = Response() response.status_code = resp.get("status_code", None) response.headers = CaseInsensitiveDict(resp.get("headers", {})) response.encoding = get_encoding_from_headers(response.headers) response.reason = resp.get("reason", None) response.url = request.url # Already decoded above response.request = request response.raw = StreamableBytesIO(resp.get("body", "").encode("utf-8")) # Cookie jar is not mocked, as Icinga doesn't use cookies # response.connection is not mocked, because it's not a response attribute by default # Call settings hook with the settings used for this request (to make testing with these easier) self.settings_hook(stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies) return response
def copy_request(request): """Copy a Requests PreparedRequest.""" new_request = PreparedRequest() new_request.method = request.method new_request.url = request.url new_request.body = request.body new_request.hooks = request.hooks new_request.headers = request.headers.copy() return new_request
def deserialize_prepared_request(serialized): p = PreparedRequest() p._cookies = RequestsCookieJar() body = serialized['body'] if isinstance(body, dict): original_body = body.get('string') p.body = original_body or base64.b64decode( body.get('base64_string', '').encode()) else: p.body = body h = [(k, from_list(v)) for k, v in serialized['headers'].items()] p.headers = CaseInsensitiveDict(h) p.method = serialized['method'] p.url = serialized['uri'] return p