def _on_request(self, session, request, **kwargs): match = self._find_match(request) # TODO(dcramer): find the correct class for this if match is None: error_msg = 'Connection refused: {0} {1}'.format( request.method, request.url) response = ConnectionError(error_msg) self._calls.add(request, response) raise response if 'body' in match and isinstance(match['body'], Exception): self._calls.add(request, match['body']) raise match['body'] headers = { 'Content-Type': match['content_type'], } if 'callback' in match: # use callback status, r_headers, body = match['callback'](request) if isinstance(body, six.text_type): body = body.encode('utf-8') body = BufferIO(body) headers.update(r_headers) elif 'body' in match: if match['adding_headers']: headers.update(match['adding_headers']) status = match['status'] body = BufferIO(match['body']) response = HTTPResponse( status=status, body=body, headers=headers, preload_content=False, ) adapter = session.get_adapter(request.url) response = adapter.build_response(request, response) if not match.get('stream'): response.content # NOQA try: resp_cookies = Cookies.from_request(response.headers['set-cookie']) response.cookies = cookiejar_from_dict( dict((v.name, v.value) for _, v in resp_cookies.items())) session.cookies = response.cookies except (KeyError, TypeError): pass self._calls.add(request, response) if kwargs.get('allow_redirects') and response.is_redirect: # include redirect resolving logic from requests.sessions.Session keep_kws = ('stream', 'timeout', 'cert', 'proxies') resolve_kwargs = dict([(k, v) for (k, v) in kwargs.items() if k in keep_kws]) # this recurses if response.is_redirect, # but limited by session.max_redirects gen = session.resolve_redirects(response, request, **resolve_kwargs) history = [resp for resp in gen] # Shuffle things around if there's history. if history: # Insert the first (original) request at the start history.insert(0, response) # Get the last request made response = history.pop() response.history = history return response
def _on_request(self, session, request, **kwargs): match = self._find_match(request) # TODO(dcramer): find the correct class for this if match is None: error_msg = 'Connection refused: {0} {1}'.format(request.method, request.url) response = ConnectionError(error_msg) self._calls.add(request, response) raise response if 'body' in match and isinstance(match['body'], Exception): self._calls.add(request, match['body']) raise match['body'] headers = { 'Content-Type': match['content_type'], } if 'callback' in match: # use callback status, r_headers, body = match['callback'](request) if isinstance(body, six.text_type): body = body.encode('utf-8') body = BufferIO(body) headers.update(r_headers) elif 'body' in match: if match['adding_headers']: headers.update(match['adding_headers']) status = match['status'] body = BufferIO(match['body']) response = HTTPResponse( status=status, body=body, headers=headers, preload_content=False, ) adapter = session.get_adapter(request.url) response = adapter.build_response(request, response) if not match.get('stream'): response.content # NOQA try: resp_cookies = Cookies.from_request(response.headers['set-cookie']) response.cookies = cookiejar_from_dict(dict( (v.name, v.value) for _, v in resp_cookies.items() )) session.cookies = response.cookies except (KeyError, TypeError): pass self._calls.add(request, response) if kwargs.get('allow_redirects') and response.is_redirect: # include redirect resolving logic from requests.sessions.Session keep_kws = ('stream', 'timeout', 'cert', 'proxies') resolve_kwargs = dict([(k, v) for (k, v) in kwargs.items() if k in keep_kws]) # this recurses if response.is_redirect, # but limited by session.max_redirects gen = session.resolve_redirects(response, request, **resolve_kwargs) history = [resp for resp in gen] # Shuffle things around if there's history. if history: # Insert the first (original) request at the start history.insert(0, response) # Get the last request made response = history.pop() response.history = history return response