Created
February 9, 2012 13:06
-
-
Save DazWorrall/1779861 to your computer and use it in GitHub Desktop.
Testing file upload handling in Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
in upload handler | |
in file close | |
.. | |
---------------------------------------------------------------------- | |
Ran 2 tests in 0.021s | |
OK |
This saved my button
This saved my breath
This saved Nobody
This saved my saved
This saved this.
This saved the Teletubbies.
This saved me from going to the scrum-retro (And my coverage)
This saved my senior design project
This saved my job.
This saved my thesis
This saved the humanity from corona virus. Oh wait not yet.
This saved 15 or more on car insurance
This saved greg
greg
A long time ago this saved a galaxy far far away
This saved me from covid
this saved my wolf
This saved 2020 from being a complete failure.
this saved me from covid (2)
This saved the queen (latifah)
This saved Jonah from the whale.
This saved my dream
This saved.
This saved my hamster.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This... poor guy's inbox.