Bogdan Bogdan - 16 days ago 7
Python Question

Python: test response stream with md5 checksum

I am just getting started with Python and I would like to test the repose of my application by doing an md5sum of the response which is essentially a binary file stream.

test_file.py

import main
import unittest
import hashlib

class MainTest(unittest.TestCase):

def setUp(self):
self.app = main.app.test_client()
...
# This test checks if the app retuns our new firmware correctly
def test_get_firmware_esp_new(self):
rv = self.app.get('/firmware',
environ_base={'HTTP_USER_AGENT': 'test-blabla'})
print rv.response.__dict__
self.assertEqual(hashlib.md5(rv.response).hexdigest(), 'bf8ad256d69fa98b9facca6fb43cb234')


The error that I get is this:

File "test_file.py", line 24, in test_get_firmware_esp_new
self.assertEqual(hashlib.md5(rv.response).hexdigest(), 'bf8ad256d69fa98b9facca6fb43cb234')
TypeError: must be convertible to a buffer, not ClosingIterator


In main.py I have a line which does something like this:

return get_stream_fw(FWupdate)


streamfw.py

from flask import Response, stream_with_context
import requests

def get_stream_fw(name):
url = 'https://media.giphy.com/media/9Sxp3YOKKFEBi/giphy.gif'
req = requests.get(url, stream = True)
return Response(stream_with_context(req.iter_content(chunk_size=1024)), content_type = req.headers["content-type"])


What is the proper way to do a hash of the response which is actually a stream that would not exceed 1MB of data?

Dan Dan
Answer

By reading help(hashlib) I think you can iterate your response something like this:

hs = hashlib.md5()
...
for i in rv.response:
  hs.update(i)

self.assertEqual(hs.hexdigest(), ...)