Skip to content

Sending >=500 KB file with test client causes warning #1785

Description

@Asa-Nisi-Masa

Expected Behavior

An endpoint with a POST method is created, along with a unittest to test its behavior. I create two dummy files (you can include your own files) one with size 499 KB, one with size 500 KB. I pass both files to the endpoint in my unittest (by running python3 -m unittest example.py)

python3.6 -m venv venv
venv/bin/pip install flask
import unittest
from flask import Flask

app = Flask(__name__)

@app.route("/", methods=["POST"])
def hello():
  return "hello world"

# create dummy files
with open("./small.txt", "wb") as f:
    f.write(b"0" * 1024 * 499)

with open("./big.txt", "wb") as f:
    f.write(b"0" * 1024 * 500)

# test the endpoint
class ApiTest(unittest.TestCase):
  def test_all(self):
    data = (
      ("hello world", "./big.txt"), # warning
      ("hello world", "./small.txt"), # no warning
    )

    for expected, path in data:
      with open(path, "rb") as file, app.test_client() as cl:
        response = cl.post("/", data={"file":file}).get_data().decode("utf-8")
        
      self.assertEqual(expected, response)
venv/bin/python3.6 -m unittest example.py

Actual Behavior

POST-ing the bigger file (>=500 KB) produces a ResourceWarning
Console output:

/home/myname/.local/lib/python3.6/site-packages/flask/testing.py:244: ResourceWarning: unclosed file <_io.BufferedRandom name=4>
  top = _request_ctx_stack.top
.
----------------------------------------------------------------------
Ran 1 test in 0.005s

OK

Changing from f.write(b"0" * 1024 * 500) to f.write(b"0" * 1024 * 499) the output is

.
----------------------------------------------------------------------
Ran 1 test in 0.004s

OK

Environment

  • Python version: 3.6.9
  • Flask version: 1.1.1
  • Werkzeug version: 1.0.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions