Bogdan Bogdan - 1 year ago 90
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.

import main
import unittest
import hashlib

class MainTest(unittest.TestCase):

def setUp(self): =
# This test checks if the app retuns our new firmware correctly
def test_get_firmware_esp_new(self):
rv ='/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 "", 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 I have a line which does something like this:

return get_stream_fw(FWupdate)

from flask import Response, stream_with_context
import requests

def get_stream_fw(name):
url = ''
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 Source

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

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

self.assertEqual(hs.hexdigest(), ...)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download