Skip to content

Instantly share code, notes, and snippets.

@Ryanb58
Forked from DazWorrall/Output
Created May 16, 2017 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryanb58/078329d6d4ac36fbc74065a578b53e8c to your computer and use it in GitHub Desktop.
Save Ryanb58/078329d6d4ac36fbc74065a578b53e8c to your computer and use it in GitHub Desktop.
Testing file upload handling in Flask
#!/usr/bin/env python
from flask import Flask, Request, request
from StringIO import StringIO
import unittest
RESULT = False
class TestFileFail(unittest.TestCase):
def test_1(self):
class FileObj(StringIO):
def close(self):
print 'in file close'
global RESULT
RESULT = True
class MyRequest(Request):
def _get_file_stream(*args, **kwargs):
return FileObj()
app = Flask(__name__)
app.debug = True
app.request_class = MyRequest
@app.route("/upload", methods=['POST'])
def upload():
f = request.files['file']
print 'in upload handler'
self.assertIsInstance(
f.stream,
FileObj,
)
# Note I've monkeypatched werkzeug.datastructures.FileStorage
# so it wont squash exceptions
f.close()
#f.stream.close()
return 'ok'
client = app.test_client()
resp = client.post(
'/upload',
data = {
'file': (StringIO('my file contents'), 'hello world.txt'),
}
)
self.assertEqual(
'ok',
resp.data,
)
global RESULT
self.assertTrue(RESULT)
def test_2(self):
pass
if __name__ == '__main__':
unittest.main()
in upload handler
in file close
..
----------------------------------------------------------------------
Ran 2 tests in 0.021s
OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment