3333SECRET_KEY = 'devkey'
3434
3535
36+ @contextmanager
37+ def catch_warnings ():
38+ """Catch warnings in a with block in a list"""
39+ import warnings
40+ filters = warnings .filters
41+ warnings .filters = filters [:]
42+ old_showwarning = warnings .showwarning
43+ log = []
44+ def showwarning (message , category , filename , lineno , file = None , line = None ):
45+ log .append (locals ())
46+ try :
47+ warnings .showwarning = showwarning
48+ yield log
49+ finally :
50+ warnings .filters = filters
51+ warnings .showwarning = old_showwarning
52+
53+
3654@contextmanager
3755def catch_stderr ():
56+ """Catch stderr in a StringIO"""
3857 old_stderr = sys .stderr
3958 sys .stderr = rv = StringIO ()
4059 try :
@@ -834,46 +853,64 @@ def test_send_file_xsendfile(self):
834853
835854 def test_send_file_object (self ):
836855 app = flask .Flask (__name__ )
837- with app .test_request_context ():
838- f = open (os .path .join (app .root_path , 'static/index.html' ))
839- rv = flask .send_file (f )
840- with app .open_resource ('static/index.html' ) as f :
841- assert rv .data == f .read ()
842- assert rv .mimetype == 'text/html'
856+ with catch_warnings () as captured :
857+ with app .test_request_context ():
858+ f = open (os .path .join (app .root_path , 'static/index.html' ))
859+ rv = flask .send_file (f )
860+ with app .open_resource ('static/index.html' ) as f :
861+ assert rv .data == f .read ()
862+ assert rv .mimetype == 'text/html'
863+ # mimetypes + etag
864+ assert len (captured ) == 2
843865
844866 app .use_x_sendfile = True
845- with app .test_request_context ():
846- f = open (os .path .join (app .root_path , 'static/index.html' ))
847- rv = flask .send_file (f )
848- assert rv .mimetype == 'text/html'
849- assert 'x-sendfile' in rv .headers
850- assert rv .headers ['x-sendfile' ] == \
851- os .path .join (app .root_path , 'static/index.html' )
867+ with catch_warnings () as captured :
868+ with app .test_request_context ():
869+ f = open (os .path .join (app .root_path , 'static/index.html' ))
870+ rv = flask .send_file (f )
871+ assert rv .mimetype == 'text/html'
872+ assert 'x-sendfile' in rv .headers
873+ assert rv .headers ['x-sendfile' ] == \
874+ os .path .join (app .root_path , 'static/index.html' )
875+ # mimetypes + etag
876+ assert len (captured ) == 2
852877
853878 app .use_x_sendfile = False
854879 with app .test_request_context ():
855- f = StringIO ('Test' )
856- rv = flask .send_file (f )
857- assert rv .data == 'Test'
858- assert rv .mimetype == 'application/octet-stream'
859- f = StringIO ('Test' )
860- rv = flask .send_file (f , mimetype = 'text/plain' )
861- assert rv .data == 'Test'
862- assert rv .mimetype == 'text/plain'
880+ with catch_warnings () as captured :
881+ f = StringIO ('Test' )
882+ rv = flask .send_file (f )
883+ assert rv .data == 'Test'
884+ assert rv .mimetype == 'application/octet-stream'
885+ # etags
886+ assert len (captured ) == 1
887+ with catch_warnings () as captured :
888+ f = StringIO ('Test' )
889+ rv = flask .send_file (f , mimetype = 'text/plain' )
890+ assert rv .data == 'Test'
891+ assert rv .mimetype == 'text/plain'
892+ # etags
893+ assert len (captured ) == 1
863894
864895 app .use_x_sendfile = True
865- with app .test_request_context ():
866- f = StringIO ('Test' )
867- rv = flask .send_file (f )
868- assert 'x-sendfile' not in rv .headers
896+ with catch_warnings () as captured :
897+ with app .test_request_context ():
898+ f = StringIO ('Test' )
899+ rv = flask .send_file (f )
900+ assert 'x-sendfile' not in rv .headers
901+ # etags
902+ assert len (captured ) == 1
869903
870904 def test_attachment (self ):
871905 app = flask .Flask (__name__ )
872- with app .test_request_context ():
873- f = open (os .path .join (app .root_path , 'static/index.html' ))
874- rv = flask .send_file (f , as_attachment = True )
875- value , options = parse_options_header (rv .headers ['Content-Disposition' ])
876- assert value == 'attachment'
906+ with catch_warnings () as captured :
907+ with app .test_request_context ():
908+ f = open (os .path .join (app .root_path , 'static/index.html' ))
909+ rv = flask .send_file (f , as_attachment = True )
910+ value , options = parse_options_header (rv .headers ['Content-Disposition' ])
911+ assert value == 'attachment'
912+ # mimetypes + etag
913+ assert len (captured ) == 2
877914
878915 with app .test_request_context ():
879916 assert options ['filename' ] == 'index.html'
@@ -884,7 +921,8 @@ def test_attachment(self):
884921
885922 with app .test_request_context ():
886923 rv = flask .send_file (StringIO ('Test' ), as_attachment = True ,
887- attachment_filename = 'index.txt' )
924+ attachment_filename = 'index.txt' ,
925+ add_etags = False )
888926 assert rv .mimetype == 'text/plain'
889927 value , options = parse_options_header (rv .headers ['Content-Disposition' ])
890928 assert value == 'attachment'
0 commit comments