Example #1
0
    def test_send_file_object(self):
        app = flask.Flask(__name__)
        with app.test_request_context():
            f = open(os.path.join(app.root_path, 'static/index.html'))
            rv = flask.send_file(f)
            with app.open_resource('static/index.html') as f:
                assert rv.data == f.read()
            assert rv.mimetype == 'text/html'

        app.use_x_sendfile = True
        with app.test_request_context():
            f = open(os.path.join(app.root_path, 'static/index.html'))
            rv = flask.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')

        app.use_x_sendfile = False
        with app.test_request_context():
            f = StringIO('Test')
            rv = flask.send_file(f)
            assert rv.data == 'Test'
            assert rv.mimetype == 'application/octet-stream'
            f = StringIO('Test')
            rv = flask.send_file(f, mimetype='text/plain')
            assert rv.data == 'Test'
            assert rv.mimetype == 'text/plain'

        app.use_x_sendfile = True
        with app.test_request_context():
            f = StringIO('Test')
            rv = flask.send_file(f)
            assert 'x-sendfile' not in rv.headers
Example #2
0
    def test_send_file_object(self):
        app = flask.Flask(__name__)
        with app.test_request_context():
            f = open(os.path.join(app.root_path, 'static/index.html'))
            rv = flask.send_file(f)
            with app.open_resource('static/index.html') as f:
                assert rv.data == f.read()
            assert rv.mimetype == 'text/html'

        app.use_x_sendfile = True
        with app.test_request_context():
            f = open(os.path.join(app.root_path, 'static/index.html'))
            rv = flask.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')

        app.use_x_sendfile = False
        with app.test_request_context():
            f = StringIO('Test')
            rv = flask.send_file(f)
            assert rv.data == 'Test'
            assert rv.mimetype == 'application/octet-stream'
            f = StringIO('Test')
            rv = flask.send_file(f, mimetype='text/plain')
            assert rv.data == 'Test'
            assert rv.mimetype == 'text/plain'

        app.use_x_sendfile = True
        with app.test_request_context():
            f = StringIO('Test')
            rv = flask.send_file(f)
            assert 'x-sendfile' not in rv.headers
Example #3
0
 def test_send_file_regular(self):
     app = flask.Flask(__name__)
     with app.test_request_context():
         rv = flask.send_file('static/index.html')
         assert rv.direct_passthrough
         assert rv.mimetype == 'text/html'
         with app.open_resource('static/index.html') as f:
             assert rv.data == f.read()
Example #4
0
 def test_send_file_regular(self):
     app = flask.Flask(__name__)
     with app.test_request_context():
         rv = flask.send_file('static/index.html')
         assert rv.direct_passthrough
         assert rv.mimetype == 'text/html'
         with app.open_resource('static/index.html') as f:
             assert rv.data == f.read()
Example #5
0
    def test_send_file_object(self):
        app = flask.Flask(__name__)
        with catch_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, "static/index.html"))
                rv = flask.send_file(f)
                with app.open_resource("static/index.html") as f:
                    assert rv.data == f.read()
                assert rv.mimetype == "text/html"
            # mimetypes + etag
            assert len(captured) == 2

        app.use_x_sendfile = True
        with catch_warnings() as captured:
            with app.test_request_context():
                f = open(os.path.join(app.root_path, "static/index.html"))
                rv = flask.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")
            # mimetypes + etag
            assert len(captured) == 2

        app.use_x_sendfile = False
        with app.test_request_context():
            with catch_warnings() as captured:
                f = StringIO("Test")
                rv = flask.send_file(f)
                assert rv.data == "Test"
                assert rv.mimetype == "application/octet-stream"
            # etags
            assert len(captured) == 1
            with catch_warnings() as captured:
                f = StringIO("Test")
                rv = flask.send_file(f, mimetype="text/plain")
                assert rv.data == "Test"
                assert rv.mimetype == "text/plain"
            # etags
            assert len(captured) == 1

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