Skip to content

Instantly share code, notes, and snippets.

@DazWorrall
Created February 9, 2012 13:06
Show Gist options
  • Star 84 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save DazWorrall/1779861 to your computer and use it in GitHub Desktop.
Save DazWorrall/1779861 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
@joegasewicz
Copy link

joegasewicz commented Nov 24, 2018

if anyone is using flask with Cloudinary and testing for integration, you'll need a real image, so you could do this:

      file = os.path.join("./tests/assets/test_asset.jpg")
      rv = client.put(
            "/api/v1/user/1",
            data={
                "avatar_image": (file, "test_asset.jpg")
                },
            headers=headers
       )

@envp
Copy link

envp commented Feb 25, 2019

This saved me from myself

@BrianFehrle
Copy link

This saved me from Thanos

@shmuli9
Copy link

shmuli9 commented Apr 17, 2019

This saved Daniels awful code...

@danielsamet
Copy link

danielsamet commented Apr 17, 2019

This saved me from killing shmuli

@paulegradie
Copy link

This... poor guy's inbox.

@poudel
Copy link

poudel commented Jun 25, 2019

This saved my button

@kmazi
Copy link

kmazi commented Jul 3, 2019

This saved my breath

@codrex
Copy link

codrex commented Jul 3, 2019

This saved Nobody

@oladimillion
Copy link

This saved my saved

@vonabarak
Copy link

This saved this.

@Edgar-Lozano
Copy link

This saved the Teletubbies.

@alexd2580
Copy link

This saved me from going to the scrum-retro (And my coverage)

@shmam
Copy link

shmam commented Feb 26, 2020

This saved my senior design project

@santalvarez
Copy link

This saved my job.

@ccardas
Copy link

ccardas commented Mar 13, 2020

This saved my thesis

@jakegeno
Copy link

This saved the humanity from corona virus. Oh wait not yet.

@fusmanii
Copy link

This saved 15 or more on car insurance

@davidRichards39
Copy link

This saved greg

@abhistack
Copy link

greg

@jahin07
Copy link

jahin07 commented May 8, 2020

A long time ago this saved a galaxy far far away

@po5i
Copy link

po5i commented Aug 12, 2020

This saved me from covid

@finomayato
Copy link

this saved my wolf

@betodealmeida
Copy link

This saved 2020 from being a complete failure.

@aprilahijriyan
Copy link

aprilahijriyan commented Mar 25, 2021

this saved me from covid (2)

@ifeLawal
Copy link

This saved the queen (latifah)

@judeapana
Copy link

This saved Jonah from the whale.

@vsonpa6
Copy link

vsonpa6 commented Nov 25, 2021

This saved my dream

@b-dont
Copy link

b-dont commented Mar 13, 2024

This saved.

@destroy-data
Copy link

This saved my hamster.

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