python - Using requests.adapters.HTTPAdapter for testing -


I'm trying to test some request code by rejecting my session object TestAdapter. Actually, my code Looks like:

  URL = 'http: // blahblahblah' class test adapter (request.adapters.HTTPAdapter): def __init __ (auto, response): self._response = response Super Test adapter, self) .__ init (__) Send DRF (self, request, * args, ** kwargs): return self.build_response (request, self._response) resp = urllib3 \ .HTTPResponse (body = json.dumps ( {'Result': Results}), position = 200, header = {'content-type': 'application / json'}) adapter = test adapter (resp) session = request Session () session.mount (url, adapter) response = session.post (url) response.json ()  

However, this experiment raises an error in the depth of URLLib3:

  Error traceback (most recent call final): file "test / test_web_cache_client.py", line 40, in test_valid_reponse response.json () file "/home/wilner/.virtualenvs/hub/local /lib/python2.7/site-packages/requests/ Models.py ", line 778, Jason does not have self.encoding and lane (own content)> 3: file "/home/wilner/.virtualenvs/hub/local/lib/python2.7/site-packages/requests/models.py", line 724, content in self._content = bytes (). Join (myself. Iter_content (CONTENT_CHUNK_SIZE)) or bytes () file "/home/wilner/.virtualenvs/hub/local/lib/python2.7/site-packages/requests/models.py", line 653, in itself To make the part. Raw.stream (chunk_size, decode_content = true): file "/home/wilner/.virtualenvs/hub/local/lib/python2.7/site-packages/urllib3/response.py", line 255, while not in the stream Is_fp_closed is not (self._fp): The file "/home/wilner/.virtualenvs/hub/local/lib/python2.7/site-packages/urllib3/util/response.py", line 22, is_fp_closed in ValueError (" Unable to determine whether the FP is closed. ") ValueError: Unable to determine whether the FP is closed.  

I can certainly find another way to test this stuff, but it seems that it should work. What am i doing wrong

is a library that wants to do for you. The way I do it in Betamax, it can be found (and can be redistributed for the next part):

  def add_urllib3_response (sort, response): If 'base64_string' is in serial ['body']: body = Io.BytesIO (base64.b64decode (serialized ['body'] ['base64_string']. Encode ()): body = body_io (** serialized ['body ']) H = HTTPResponse (body, position = response.status_code, headers = response.headers, preload_content = False, original_response = MockHTTPResponse (response.headers)) response.raw = h  

The important thing here is that body HTTPResponse to code> (the first parameter) is an io.bytesIO object in every case. If you make sure that you are using bytes, like, json.dumps ({'result': results}). Transcript ('utf-8') to io.BytesIO .


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -