How to check send_file flag

I have an application with small flasks that takes some images to upload and converts them to multi-page tiff. Nothing special.

But how can I check the upload of multiple files and upload the file?

My test client:

class RestTestCase(unittest.TestCase): def setUp(self): self.dir = os.path.dirname(__file__) rest = imp.load_source('rest', self.dir + '/../rest.py') rest.app.config['TESTING'] = True self.app = rest.app.test_client() def runTest(self): with open(self.dir + '/img/img1.jpg', 'rb') as img1: img1StringIO = StringIO(img1.read()) response = self.app.post('/convert', content_type='multipart/form-data', data={'photo': (img1StringIO, 'img1.jpg')}, follow_redirects=True) assert True if __name__ == "__main__": unittest.main() 

The application sends back the file with

 return send_file(result, mimetype='image/tiff', \ as_attachment=True) 

I want to read the file sent in response and compare it with another file. How to get a file from the response object?

+6
source share
1 answer

I think maybe the confusion here is that response is a Response object, not the data loaded by the mail request. This is because the HTTP response has other attributes that can often be useful for searching, for example, the returned HTTP status code, the mime type of the response, etc. The attribute names for accessing them are listed in the link above.

The response object has the data attribute, so response.data will contain the data downloaded from the server. The documents I'm linked to indicate that data will be out of date soon, but instead use the get_data() method, but the test guide still uses the data. Test your own system to see what works. If you want to check the data circuit,

 def runTest(self): with open(self.dir + '/img/img1.jpg', 'rb') as img1: img1StringIO = StringIO(img1.read()) response = self.app.post('/convert', content_type='multipart/form-data', data={'photo': (img1StringIO, 'img1.jpg')}, follow_redirects=True) img1StringIO.seek(0) assert response.data == imgStringIO.read() 
+7
source

Source: https://habr.com/ru/post/976725/


All Articles