33
33
SECRET_KEY = 'devkey'
34
34
35
35
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
+
36
54
@contextmanager
37
55
def catch_stderr ():
56
+ """Catch stderr in a StringIO"""
38
57
old_stderr = sys .stderr
39
58
sys .stderr = rv = StringIO ()
40
59
try :
@@ -834,46 +853,64 @@ def test_send_file_xsendfile(self):
834
853
835
854
def test_send_file_object (self ):
836
855
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
843
865
844
866
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
852
877
853
878
app .use_x_sendfile = False
854
879
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
863
894
864
895
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
869
903
870
904
def test_attachment (self ):
871
905
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
877
914
878
915
with app .test_request_context ():
879
916
assert options ['filename' ] == 'index.html'
@@ -884,7 +921,8 @@ def test_attachment(self):
884
921
885
922
with app .test_request_context ():
886
923
rv = flask .send_file (StringIO ('Test' ), as_attachment = True ,
887
- attachment_filename = 'index.txt' )
924
+ attachment_filename = 'index.txt' ,
925
+ add_etags = False )
888
926
assert rv .mimetype == 'text/plain'
889
927
value , options = parse_options_header (rv .headers ['Content-Disposition' ])
890
928
assert value == 'attachment'
0 commit comments