Exemplo n.º 1
0
    def test_send_file_object(self):
        app = flask.Flask(__name__)

        with app.test_request_context():
            with open(os.path.join(app.root_path, 'static/index.html'),
                      mode='rb') as f:
                rv = flask.send_file(f, mimetype='text/html')
                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()

        app.use_x_sendfile = True

        with app.test_request_context():
            with open(os.path.join(app.root_path, 'static/index.html')) as f:
                rv = flask.send_file(f, mimetype='text/html')
                assert rv.mimetype == 'text/html'
                assert 'x-sendfile' not in rv.headers
                rv.close()

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

            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 = flask.send_file(f, attachment_filename=f.name)
            rv.direct_passthrough = False
            assert rv.data == b'Test'
            assert rv.mimetype == 'text/plain'
            rv.close()

            f = StringIO('Test')
            rv = flask.send_file(f, mimetype='text/plain')
            rv.direct_passthrough = False
            assert rv.data == b'Test'
            assert rv.mimetype == 'text/plain'
            rv.close()

        app.use_x_sendfile = True

        with app.test_request_context():
            f = StringIO('Test')
            rv = flask.send_file(f, mimetype='text/html')
            assert 'x-sendfile' not in rv.headers
            rv.close()
Exemplo n.º 2
0
    def test_send_file_object(self):
        app = flask.Flask(__name__)

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

        app.use_x_sendfile = True

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

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

            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 = flask.send_file(f)
            rv.direct_passthrough = False
            assert rv.data == b'Test'
            assert rv.mimetype == 'text/plain'
            rv.close()

            f = StringIO('Test')
            rv = flask.send_file(f, mimetype='text/plain')
            rv.direct_passthrough = False
            assert rv.data == b'Test'
            assert rv.mimetype == 'text/plain'
            rv.close()

        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
            rv.close()
Exemplo n.º 3
0
    def test_send_file_object(self, app, req_ctx):
        with open(os.path.join(app.root_path, "static/index.html"),
                  mode="rb") as f:
            rv = flask.send_file(f, mimetype="text/html")
            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()

        app.use_x_sendfile = True

        with open(os.path.join(app.root_path, "static/index.html")) as f:
            rv = flask.send_file(f, mimetype="text/html")
            assert rv.mimetype == "text/html"
            assert "x-sendfile" not in rv.headers
            rv.close()

        app.use_x_sendfile = False
        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="application/octet-stream")
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "application/octet-stream"
        rv.close()

        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 = flask.send_file(f, attachment_filename=f.name)
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "text/plain"
        rv.close()

        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="text/plain")
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "text/plain"
        rv.close()

        app.use_x_sendfile = True

        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="text/html")
        assert "x-sendfile" not in rv.headers
        rv.close()
Exemplo n.º 4
0
    def test_send_file_object(self, app, req_ctx):
        with open(os.path.join(app.root_path, "static/index.html"), mode="rb") as f:
            rv = flask.send_file(f, mimetype="text/html")
            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()

        app.use_x_sendfile = True

        with open(os.path.join(app.root_path, "static/index.html")) as f:
            rv = flask.send_file(f, mimetype="text/html")
            assert rv.mimetype == "text/html"
            assert "x-sendfile" not in rv.headers
            rv.close()

        app.use_x_sendfile = False
        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="application/octet-stream")
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "application/octet-stream"
        rv.close()

        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 = flask.send_file(f, attachment_filename=f.name)
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "text/plain"
        rv.close()

        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="text/plain")
        rv.direct_passthrough = False
        assert rv.data == b"Test"
        assert rv.mimetype == "text/plain"
        rv.close()

        app.use_x_sendfile = True

        f = StringIO("Test")
        rv = flask.send_file(f, mimetype="text/html")
        assert "x-sendfile" not in rv.headers
        rv.close()
Exemplo n.º 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)
                rv.direct_passthrough = False
                with app.open_resource('static/index.html') as f:
                    self.assert_equal(rv.data, f.read())
                self.assert_equal(rv.mimetype, 'text/html')
                rv.close()
            # mimetypes + etag
            self.assert_equal(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)
                self.assert_equal(rv.mimetype, 'text/html')
                self.assert_in('x-sendfile', rv.headers)
                self.assert_equal(
                    rv.headers['x-sendfile'],
                    os.path.join(app.root_path, 'static/index.html'))
                rv.close()
            # mimetypes + etag
            self.assert_equal(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)
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'application/octet-stream')
                rv.close()
            # etags
            self.assert_equal(len(captured), 1)
            with catch_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 = flask.send_file(f)
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'text/plain')
                rv.close()
            # attachment_filename and etags
            self.assert_equal(len(captured), 3)
            with catch_warnings() as captured:
                f = StringIO('Test')
                rv = flask.send_file(f, mimetype='text/plain')
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'text/plain')
                rv.close()
            # etags
            self.assert_equal(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)
                self.assert_not_in('x-sendfile', rv.headers)
                rv.close()
            # etags
            self.assert_equal(len(captured), 1)
Exemplo n.º 6
0
    def test_send_file_object(self, catch_deprecation_warnings):
        app = flask.Flask(__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 = flask.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 = 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')
                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 = flask.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 = flask.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 = flask.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 = flask.send_file(f)
                assert 'x-sendfile' not in rv.headers
                rv.close()
            # etags
            assert len(captured) == 1
Exemplo n.º 7
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)
                rv.direct_passthrough = False
                with app.open_resource('static/index.html') as f:
                    self.assert_equal(rv.data, f.read())
                self.assert_equal(rv.mimetype, 'text/html')
                rv.close()
            # mimetypes + etag
            self.assert_equal(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)
                self.assert_equal(rv.mimetype, 'text/html')
                self.assert_in('x-sendfile', rv.headers)
                self.assert_equal(rv.headers['x-sendfile'],
                    os.path.join(app.root_path, 'static/index.html'))
                rv.close()
            # mimetypes + etag
            self.assert_equal(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)
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'application/octet-stream')
                rv.close()
            # etags
            self.assert_equal(len(captured), 1)
            with catch_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 = flask.send_file(f)
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'text/plain')
                rv.close()
            # attachment_filename and etags
            self.assert_equal(len(captured), 3)
            with catch_warnings() as captured:
                f = StringIO('Test')
                rv = flask.send_file(f, mimetype='text/plain')
                rv.direct_passthrough = False
                self.assert_equal(rv.data, b'Test')
                self.assert_equal(rv.mimetype, 'text/plain')
                rv.close()
            # etags
            self.assert_equal(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)
                self.assert_not_in('x-sendfile', rv.headers)
                rv.close()
            # etags
            self.assert_equal(len(captured), 1)