Exemplo n.º 1
0
    def test_attachment(self, catch_deprecation_warnings):
        app = keyes.Keyes(__name__)
        with catch_deprecation_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, 'static/index.html'))
                rv = keyes.send_file(f, as_attachment=True)
                value, options = parse_options_header(rv.headers['Content-Disposition'])
                assert value == 'attachment'
                rv.close()
            # mimetypes + etag
            assert len(captured) == 2

        with app.test_request_context():
            assert options['filename'] == 'index.html'
            rv = keyes.send_file('static/index.html', as_attachment=True)
            value, options = parse_options_header(rv.headers['Content-Disposition'])
            assert value == 'attachment'
            assert options['filename'] == 'index.html'
            rv.close()

        with app.test_request_context():
            rv = keyes.send_file(StringIO('Test'), as_attachment=True,
                                 attachment_filename='index.txt',
                                 add_etags=False)
            assert rv.mimetype == 'text/plain'
            value, options = parse_options_header(rv.headers['Content-Disposition'])
            assert value == 'attachment'
            assert options['filename'] == 'index.txt'
            rv.close()
Exemplo n.º 2
0
    def test_attachment(self, catch_deprecation_warnings):
        app = keyes.Keyes(__name__)
        with catch_deprecation_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, 'static/index.html'))
                rv = keyes.send_file(f, as_attachment=True)
                value, options = parse_options_header(
                    rv.headers['Content-Disposition'])
                assert value == 'attachment'
                rv.close()
            # mimetypes + etag
            assert len(captured) == 2

        with app.test_request_context():
            assert options['filename'] == 'index.html'
            rv = keyes.send_file('static/index.html', as_attachment=True)
            value, options = parse_options_header(
                rv.headers['Content-Disposition'])
            assert value == 'attachment'
            assert options['filename'] == 'index.html'
            rv.close()

        with app.test_request_context():
            rv = keyes.send_file(StringIO('Test'),
                                 as_attachment=True,
                                 attachment_filename='index.txt',
                                 add_etags=False)
            assert rv.mimetype == 'text/plain'
            value, options = parse_options_header(
                rv.headers['Content-Disposition'])
            assert value == 'attachment'
            assert options['filename'] == 'index.txt'
            rv.close()
Exemplo n.º 3
0
    def test_static_file(self):
        app = keyes.Keyes(__name__)
        # default cache timeout is 12 hours
        with app.test_request_context():
            # Test with static file handler.
            rv = app.send_static_file('index.html')
            cc = parse_cache_control_header(rv.headers['Cache-Control'])
            assert cc.max_age == 12 * 60 * 60
            rv.close()
            # Test again with direct use of send_file utility.
            rv = keyes.send_file('static/index.html')
            cc = parse_cache_control_header(rv.headers['Cache-Control'])
            assert cc.max_age == 12 * 60 * 60
            rv.close()
        app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
        with app.test_request_context():
            # Test with static file handler.
            rv = app.send_static_file('index.html')
            cc = parse_cache_control_header(rv.headers['Cache-Control'])
            assert cc.max_age == 3600
            rv.close()
            # Test again with direct use of send_file utility.
            rv = keyes.send_file('static/index.html')
            cc = parse_cache_control_header(rv.headers['Cache-Control'])
            assert cc.max_age == 3600
            rv.close()

        class StaticFileApp(keyes.Keyes):
            def get_send_file_max_age(self, filename):
                return 10

        app = StaticFileApp(__name__)
        with app.test_request_context():
            # Test with static file handler.
            rv = app.send_static_file('index.html')
            cc = parse_cache_control_header(rv.headers['Cache-Control'])
            assert cc.max_age == 10
            rv.close()
            # Test again with direct use of send_file utility.
            rv = keyes.send_file('static/index.html')
            cc = parse_cache_control_header(rv.headers['Cache-Control'])
            assert cc.max_age == 10
            rv.close()
Exemplo n.º 4
0
 def test_send_file_regular(self):
     app = keyes.Keyes(__name__)
     with app.test_request_context():
         rv = keyes.send_file('static/index.html')
         assert rv.direct_passthrough
         assert rv.mimetype == 'text/html'
         with app.open_resource('static/index.html') as f:
             rv.direct_passthrough = False
             assert rv.data == f.read()
         rv.close()
Exemplo n.º 5
0
 def test_send_file_regular(self):
     app = keyes.Keyes(__name__)
     with app.test_request_context():
         rv = keyes.send_file('static/index.html')
         assert rv.direct_passthrough
         assert rv.mimetype == 'text/html'
         with app.open_resource('static/index.html') as f:
             rv.direct_passthrough = False
             assert rv.data == f.read()
         rv.close()
Exemplo n.º 6
0
 def test_send_file_xsendfile(self):
     app = keyes.Keyes(__name__)
     app.use_x_sendfile = True
     with app.test_request_context():
         rv = keyes.send_file('static/index.html')
         assert rv.direct_passthrough
         assert 'x-sendfile' in rv.headers
         assert rv.headers['x-sendfile'] == \
             os.path.join(app.root_path, 'static/index.html')
         assert rv.mimetype == 'text/html'
         rv.close()
Exemplo n.º 7
0
 def test_send_file_xsendfile(self):
     app = keyes.Keyes(__name__)
     app.use_x_sendfile = True
     with app.test_request_context():
         rv = keyes.send_file('static/index.html')
         assert rv.direct_passthrough
         assert 'x-sendfile' in rv.headers
         assert rv.headers['x-sendfile'] == \
             os.path.join(app.root_path, 'static/index.html')
         assert rv.mimetype == 'text/html'
         rv.close()
Exemplo n.º 8
0
 def test_static_file(self):
     app = keyes.Keyes(__name__)
     # default cache timeout is 12 hours
     with app.test_request_context():
         # Test with static file handler.
         rv = app.send_static_file('index.html')
         cc = parse_cache_control_header(rv.headers['Cache-Control'])
         assert cc.max_age == 12 * 60 * 60
         rv.close()
         # Test again with direct use of send_file utility.
         rv = keyes.send_file('static/index.html')
         cc = parse_cache_control_header(rv.headers['Cache-Control'])
         assert cc.max_age == 12 * 60 * 60
         rv.close()
     app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
     with app.test_request_context():
         # Test with static file handler.
         rv = app.send_static_file('index.html')
         cc = parse_cache_control_header(rv.headers['Cache-Control'])
         assert cc.max_age == 3600
         rv.close()
         # Test again with direct use of send_file utility.
         rv = keyes.send_file('static/index.html')
         cc = parse_cache_control_header(rv.headers['Cache-Control'])
         assert cc.max_age == 3600
         rv.close()
     class StaticFileApp(keyes.Keyes):
         def get_send_file_max_age(self, filename):
             return 10
     app = StaticFileApp(__name__)
     with app.test_request_context():
         # Test with static file handler.
         rv = app.send_static_file('index.html')
         cc = parse_cache_control_header(rv.headers['Cache-Control'])
         assert cc.max_age == 10
         rv.close()
         # Test again with direct use of send_file utility.
         rv = keyes.send_file('static/index.html')
         cc = parse_cache_control_header(rv.headers['Cache-Control'])
         assert cc.max_age == 10
         rv.close()
Exemplo n.º 9
0
    def test_send_file_object(self, catch_deprecation_warnings):
        app = keyes.Keyes(__name__)
        with catch_deprecation_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, 'static/index.html'),
                         mode='rb')
                rv = keyes.send_file(f)
                rv.direct_passthrough = False
                with app.open_resource('static/index.html') as f:
                    assert rv.data == f.read()
                assert rv.mimetype == 'text/html'
                rv.close()
            # mimetypes + etag
            assert len(captured) == 2

        app.use_x_sendfile = True
        with catch_deprecation_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, 'static/index.html'))
                rv = keyes.send_file(f)
                assert rv.mimetype == 'text/html'
                assert 'x-sendfile' in rv.headers
                assert rv.headers['x-sendfile'] == \
                    os.path.join(app.root_path, 'static/index.html')
                rv.close()
            # mimetypes + etag
            assert len(captured) == 2

        app.use_x_sendfile = False
        with app.test_request_context():
            with catch_deprecation_warnings() as captured:
                f = StringIO('Test')
                rv = keyes.send_file(f)
                rv.direct_passthrough = False
                assert rv.data == b'Test'
                assert rv.mimetype == 'application/octet-stream'
                rv.close()
            # etags
            assert len(captured) == 1
            with catch_deprecation_warnings() as captured:

                class PyStringIO(object):
                    def __init__(self, *args, **kwargs):
                        self._io = StringIO(*args, **kwargs)

                    def __getattr__(self, name):
                        return getattr(self._io, name)

                f = PyStringIO('Test')
                f.name = 'test.txt'
                rv = keyes.send_file(f)
                rv.direct_passthrough = False
                assert rv.data == b'Test'
                assert rv.mimetype == 'text/plain'
                rv.close()
            # attachment_filename and etags
            assert len(captured) == 3
            with catch_deprecation_warnings() as captured:
                f = StringIO('Test')
                rv = keyes.send_file(f, mimetype='text/plain')
                rv.direct_passthrough = False
                assert rv.data == b'Test'
                assert rv.mimetype == 'text/plain'
                rv.close()
            # etags
            assert len(captured) == 1

        app.use_x_sendfile = True
        with catch_deprecation_warnings() as captured:
            with app.test_request_context():
                f = StringIO('Test')
                rv = keyes.send_file(f)
                assert 'x-sendfile' not in rv.headers
                rv.close()
            # etags
            assert len(captured) == 1
Exemplo n.º 10
0
    def test_send_file_object(self, catch_deprecation_warnings):
        app = keyes.Keyes(__name__)
        with catch_deprecation_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, 'static/index.html'), mode='rb')
                rv = keyes.send_file(f)
                rv.direct_passthrough = False
                with app.open_resource('static/index.html') as f:
                    assert rv.data == f.read()
                assert rv.mimetype == 'text/html'
                rv.close()
            # mimetypes + etag
            assert len(captured) == 2

        app.use_x_sendfile = True
        with catch_deprecation_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, 'static/index.html'))
                rv = keyes.send_file(f)
                assert rv.mimetype == 'text/html'
                assert 'x-sendfile' in rv.headers
                assert rv.headers['x-sendfile'] == \
                    os.path.join(app.root_path, 'static/index.html')
                rv.close()
            # mimetypes + etag
            assert len(captured) == 2

        app.use_x_sendfile = False
        with app.test_request_context():
            with catch_deprecation_warnings() as captured:
                f = StringIO('Test')
                rv = keyes.send_file(f)
                rv.direct_passthrough = False
                assert rv.data == b'Test'
                assert rv.mimetype == 'application/octet-stream'
                rv.close()
            # etags
            assert len(captured) == 1
            with catch_deprecation_warnings() as captured:
                class PyStringIO(object):
                    def __init__(self, *args, **kwargs):
                        self._io = StringIO(*args, **kwargs)
                    def __getattr__(self, name):
                        return getattr(self._io, name)
                f = PyStringIO('Test')
                f.name = 'test.txt'
                rv = keyes.send_file(f)
                rv.direct_passthrough = False
                assert rv.data == b'Test'
                assert rv.mimetype == 'text/plain'
                rv.close()
            # attachment_filename and etags
            assert len(captured) == 3
            with catch_deprecation_warnings() as captured:
                f = StringIO('Test')
                rv = keyes.send_file(f, mimetype='text/plain')
                rv.direct_passthrough = False
                assert rv.data == b'Test'
                assert rv.mimetype == 'text/plain'
                rv.close()
            # etags
            assert len(captured) == 1

        app.use_x_sendfile = True
        with catch_deprecation_warnings() as captured:
            with app.test_request_context():
                f = StringIO('Test')
                rv = keyes.send_file(f)
                assert 'x-sendfile' not in rv.headers
                rv.close()
            # etags
            assert len(captured) == 1