title: Testing the data sent by Flask's send_file()
body: <p>I have a <a href="https://palletsprojects.com/p/flask/" rel="nofollow noreferrer">Flask</a> view that generates an Excel file (using <a href="https://openpyxl.readthedocs.io/en/stable/" rel="nofollow noreferrer">openpyxl</a>) from some data and it's returned to the user using <code>send_file()</code>. A <em>very</em> simplified version:</p>
  
  <pre class="lang-py prettyprint-override"><code>import io
  from flask import send_file
  from openpyxl.workbook import Workbook
  
  @app.route("/download/&lt;int:id&gt;")
  def file_download(id):
  
      wb = Workbook()
      # Add sheets and data to the workbook here.
  
      file = io.BytesIO()
      wb.save(file)
      file.seek(0)
  
      return send_file(file, attachment_filename=f"{id}.xlsx", as_attachment=True)
  </code></pre>
  
  <p>This works fine -- the file downloads and is a valid Excel file. But I'm not sure how to test the file download. So far I have something like this (using <a href="https://pytest.org/en/latest/" rel="nofollow noreferrer">pytest</a>):</p>
  
  <pre class="lang-py prettyprint-override"><code>def test_file_download(test_client):
      response = test_client.get("/download/123")
      assert response.status_code == 200
      assert response.content_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  </code></pre>
  
  <p>Which passes, but I'd like to test that the (a) the filename used is as expected and (b) that the file... exists? Is an Excel file?</p>
  
  <p>I can access <code>response.get_data()</code>, which is a <code>bytes</code> object, but I'm not sure what to do with it.</p>