示例#1
0
    def test_templates_and_static(self):
        from moduleapp import app

        c = app.test_client()

        rv = c.get("/")
        assert rv.data == "Hello from the Frontend"
        rv = c.get("/admin/")
        assert rv.data == "Hello from the Admin"
        rv = c.get("/admin/index2")
        assert rv.data == "Hello from the Admin"
        rv = c.get("/admin/static/test.txt")
        assert rv.data.strip() == "Admin File"
        rv = c.get("/admin/static/css/test.css")
        assert rv.data.strip() == "/* nested file */"

        with app.test_request_context():
            assert flask.url_for("admin.static", filename="test.txt") == "/admin/static/test.txt"

        with app.test_request_context():
            try:
                flask.render_template("missing.html")
            except TemplateNotFound, e:
                assert e.name == "missing.html"
            else:
示例#2
0
    def test_debug_log(self):
        app = flask.Flask(__name__)
        app.debug = True

        @app.route("/")
        def index():
            app.logger.warning("the standard library is dead")
            app.logger.debug("this is a debug statement")
            return ""

        @app.route("/exc")
        def exc():
            1 / 0

        c = app.test_client()

        with catch_stderr() as err:
            c.get("/")
            out = err.getvalue()
            assert "WARNING in flask_tests [" in out
            assert "flask_tests.py" in out
            assert "the standard library is dead" in out
            assert "this is a debug statement" in out

        with catch_stderr() as err:
            try:
                c.get("/exc")
            except ZeroDivisionError:
                pass
            else:
                assert False, "debug log ate the exception"
示例#3
0
    def test_processor_exceptions(self):
        app = flask.Flask(__name__)

        @app.before_request
        def before_request():
            if trigger == "before":
                1 / 0

        @app.after_request
        def after_request(response):
            if trigger == "after":
                1 / 0
            return response

        @app.route("/")
        def index():
            return "Foo"

        @app.errorhandler(500)
        def internal_server_error(e):
            return "Hello Server Error", 500

        for trigger in "before", "after":
            rv = app.test_client().get("/")
            assert rv.status_code == 500
            assert rv.data == "Hello Server Error"
示例#4
0
    def test_processor_exceptions(self):
        app = flask.Flask(__name__)

        @app.before_request
        def before_request():
            if trigger == 'before':
                1 / 0

        @app.after_request
        def after_request(response):
            if trigger == 'after':
                1 / 0
            return response

        @app.route('/')
        def index():
            return 'Foo'

        @app.errorhandler(500)
        def internal_server_error(e):
            return 'Hello Server Error', 500

        for trigger in 'before', 'after':
            rv = app.test_client().get('/')
            assert rv.status_code == 500
            assert rv.data == 'Hello Server Error'
示例#5
0
    def test_templates_and_static(self):
        from moduleapp import app
        c = app.test_client()

        rv = c.get('/')
        assert rv.data == 'Hello from the Frontend'
        rv = c.get('/admin/')
        assert rv.data == 'Hello from the Admin'
        rv = c.get('/admin/index2')
        assert rv.data == 'Hello from the Admin'
        rv = c.get('/admin/static/test.txt')
        assert rv.data.strip() == 'Admin File'
        rv = c.get('/admin/static/css/test.css')
        assert rv.data.strip() == '/* nested file */'

        with app.test_request_context():
            assert flask.url_for('admin.static', filename='test.txt') \
                == '/admin/static/test.txt'

        with app.test_request_context():
            try:
                flask.render_template('missing.html')
            except TemplateNotFound, e:
                assert e.name == 'missing.html'
            else:
示例#6
0
    def test_debug_log(self):
        app = flask.Flask(__name__)
        app.debug = True

        @app.route('/')
        def index():
            app.logger.warning('the standard library is dead')
            app.logger.debug('this is a debug statement')
            return ''

        @app.route('/exc')
        def exc():
            1/0
        c = app.test_client()

        with catch_stderr() as err:
            c.get('/')
            out = err.getvalue()
            assert 'WARNING in flask_tests [' in out
            assert 'flask_tests.py' in out
            assert 'the standard library is dead' in out
            assert 'this is a debug statement' in out

        with catch_stderr() as err:
            try:
                c.get('/exc')
            except ZeroDivisionError:
                pass
            else:
                assert False, 'debug log ate the exception'
示例#7
0
    def test_templates_and_static(self):
        from moduleapp import app
        c = app.test_client()

        rv = c.get('/')
        assert rv.data == 'Hello from the Frontend'
        rv = c.get('/admin/')
        assert rv.data == 'Hello from the Admin'
        rv = c.get('/admin/index2')
        assert rv.data == 'Hello from the Admin'
        rv = c.get('/admin/static/test.txt')
        assert rv.data.strip() == 'Admin File'
        rv = c.get('/admin/static/css/test.css')
        assert rv.data.strip() == '/* nested file */'

        with app.test_request_context():
            assert flask.url_for('admin.static', filename='test.txt') \
                == '/admin/static/test.txt'

        with app.test_request_context():
            try:
                flask.render_template('missing.html')
            except TemplateNotFound, e:
                assert e.name == 'missing.html'
            else:
示例#8
0
    def test_debug_log(self):
        app = flask.Flask(__name__)
        app.debug = True

        @app.route('/')
        def index():
            app.logger.warning('the standard library is dead')
            app.logger.debug('this is a debug statement')
            return ''

        @app.route('/exc')
        def exc():
            1/0
        c = app.test_client()

        with catch_stderr() as err:
            c.get('/')
            out = err.getvalue()
            assert 'WARNING in flask_tests [' in out
            assert 'flask_tests.py' in out
            assert 'the standard library is dead' in out
            assert 'this is a debug statement' in out

        with catch_stderr() as err:
            try:
                c.get('/exc')
            except ZeroDivisionError:
                pass
            else:
                assert False, 'debug log ate the exception'
示例#9
0
 def test_module_static_path_subdomain(self):
     app = flask.Flask(__name__)
     app.config['SERVER_NAME'] = 'example.com'
     from subdomaintestmodule import mod
     app.register_module(mod)
     c = app.test_client()
     rv = c.get('/static/hello.txt', 'http://foo.example.com/')
     assert rv.data.strip() == 'Hello Subdomain'
示例#10
0
 def test_module_static_path_subdomain(self):
     app = flask.Flask(__name__)
     app.config['SERVER_NAME'] = 'example.com'
     from subdomaintestmodule import mod
     app.register_module(mod)
     c = app.test_client()
     rv = c.get('/static/hello.txt', 'http://foo.example.com/')
     assert rv.data.strip() == 'Hello Subdomain'
示例#11
0
    def test_module_static_path_subdomain(self):
        app = flask.Flask(__name__)
        app.config["SERVER_NAME"] = "example.com"
        from subdomaintestmodule import mod

        app.register_module(mod)
        c = app.test_client()
        rv = c.get("/static/hello.txt", "http://foo.example.com/")
        assert rv.data.strip() == "Hello Subdomain"
示例#12
0
    def test_subdomain_matching(self):
        app = flask.Flask(__name__)
        app.config['SERVER_NAME'] = 'localhost'
        @app.route('/', subdomain='<user>')
        def index(user):
            return 'index for %s' % user

        c = app.test_client()
        rv = c.get('/', 'http://mitsuhiko.localhost/')
        assert rv.data == 'index for mitsuhiko'
示例#13
0
    def test_subdomain_matching(self):
        app = flask.Flask(__name__)
        app.config['SERVER_NAME'] = 'localhost'
        @app.route('/', subdomain='<user>')
        def index(user):
            return 'index for %s' % user

        c = app.test_client()
        rv = c.get('/', 'http://mitsuhiko.localhost/')
        assert rv.data == 'index for mitsuhiko'
示例#14
0
    def test_template_rendered(self):
        app = flask.Flask(__name__)

        @app.route('/')
        def index():
            return flask.render_template('simple_template.html', whiskey=42)

        recorded = []
        def record(sender, template, context):
            recorded.append((template, context))

        flask.template_rendered.connect(record, app)
        try:
            app.test_client().get('/')
            assert len(recorded) == 1
            template, context = recorded[0]
            assert template.name == 'simple_template.html'
            assert context['whiskey'] == 42
        finally:
            flask.template_rendered.disconnect(record, app)
示例#15
0
    def test_template_rendered(self):
        app = flask.Flask(__name__)

        @app.route('/')
        def index():
            return flask.render_template('simple_template.html', whiskey=42)

        recorded = []
        def record(sender, template, context):
            recorded.append((template, context))

        flask.template_rendered.connect(record, app)
        try:
            app.test_client().get('/')
            assert len(recorded) == 1
            template, context = recorded[0]
            assert template.name == 'simple_template.html'
            assert context['whiskey'] == 42
        finally:
            flask.template_rendered.disconnect(record, app)
示例#16
0
    def test_subdomain_matching(self):
        app = flask.Flask(__name__)
        app.config["SERVER_NAME"] = "localhost"

        @app.route("/", subdomain="<user>")
        def index(user):
            return "index for %s" % user

        c = app.test_client()
        rv = c.get("/", "http://mitsuhiko.localhost/")
        assert rv.data == "index for mitsuhiko"
示例#17
0
 def test_static_path_without_url_prefix(self):
     app = flask.Flask(__name__)
     app.config['SERVER_NAME'] = 'example.com'
     from testmodule import mod
     app.register_module(mod)
     c = app.test_client()
     f = 'hello.txt'
     rv = c.get('/static/' + f, 'http://example.com/')
     assert rv.data.strip() == 'Hello Maindomain'
     with app.test_request_context(base_url='http://example.com'):
         assert flask.url_for('static', filename=f) == '/static/' + f
         assert flask.url_for('static', filename=f, _external=True) \
             == 'http://example.com/static/' + f
示例#18
0
    def test_basic_support(self):
        app = flask.Flask(__name__)
        app.config['SERVER_NAME'] = 'localhost'
        @app.route('/')
        def normal_index():
            return 'normal index'
        @app.route('/', subdomain='test')
        def test_index():
            return 'test index'

        c = app.test_client()
        rv = c.get('/', 'http://localhost/')
        assert rv.data == 'normal index'

        rv = c.get('/', 'http://test.localhost/')
        assert rv.data == 'test index'
示例#19
0
    def test_basic_support(self):
        app = flask.Flask(__name__)
        app.config['SERVER_NAME'] = 'localhost'
        @app.route('/')
        def normal_index():
            return 'normal index'
        @app.route('/', subdomain='test')
        def test_index():
            return 'test index'

        c = app.test_client()
        rv = c.get('/', 'http://localhost/')
        assert rv.data == 'normal index'

        rv = c.get('/', 'http://test.localhost/')
        assert rv.data == 'test index'
示例#20
0
    def test_request_exception_signal(self):
        app = flask.Flask(__name__)
        recorded = []

        @app.route('/')
        def index():
            1/0

        def record(sender, exception):
            recorded.append(exception)

        flask.got_request_exception.connect(record, app)
        try:
            assert app.test_client().get('/').status_code == 500
            assert len(recorded) == 1
            assert isinstance(recorded[0], ZeroDivisionError)
        finally:
            flask.got_request_exception.disconnect(record, app)
示例#21
0
    def test_basic_support(self):
        app = flask.Flask(__name__)
        app.config["SERVER_NAME"] = "localhost"

        @app.route("/")
        def normal_index():
            return "normal index"

        @app.route("/", subdomain="test")
        def test_index():
            return "test index"

        c = app.test_client()
        rv = c.get("/", "http://localhost/")
        assert rv.data == "normal index"

        rv = c.get("/", "http://test.localhost/")
        assert rv.data == "test index"
示例#22
0
    def test_request_exception_signal(self):
        app = flask.Flask(__name__)
        recorded = []

        @app.route('/')
        def index():
            1/0

        def record(sender, exception):
            recorded.append(exception)

        flask.got_request_exception.connect(record, app)
        try:
            assert app.test_client().get('/').status_code == 500
            assert len(recorded) == 1
            assert isinstance(recorded[0], ZeroDivisionError)
        finally:
            flask.got_request_exception.disconnect(record, app)
示例#23
0
    def test_exception_logging(self):
        out = StringIO()
        app = flask.Flask(__name__)
        app.logger_name = 'flask_tests/test_exception_logging'
        app.logger.addHandler(StreamHandler(out))

        @app.route('/')
        def index():
            1/0

        rv = app.test_client().get('/')
        assert rv.status_code == 500
        assert 'Internal Server Error' in rv.data

        err = out.getvalue()
        assert 'Exception on / [GET]' in err
        assert 'Traceback (most recent call last):' in err
        assert '1/0' in err
        assert 'ZeroDivisionError:' in err
示例#24
0
    def test_exception_logging(self):
        out = StringIO()
        app = flask.Flask(__name__)
        app.logger_name = 'flask_tests/test_exception_logging'
        app.logger.addHandler(StreamHandler(out))

        @app.route('/')
        def index():
            1/0

        rv = app.test_client().get('/')
        assert rv.status_code == 500
        assert 'Internal Server Error' in rv.data

        err = out.getvalue()
        assert 'Exception on / [GET]' in err
        assert 'Traceback (most recent call last):' in err
        assert '1/0' in err
        assert 'ZeroDivisionError:' in err
示例#25
0
    def test_exception_logging(self):
        out = StringIO()
        app = flask.Flask(__name__)
        app.logger_name = "flask_tests/test_exception_logging"
        app.logger.addHandler(StreamHandler(out))

        @app.route("/")
        def index():
            1 / 0

        rv = app.test_client().get("/")
        assert rv.status_code == 500
        assert "Internal Server Error" in rv.data

        err = out.getvalue()
        assert "Exception on / [GET]" in err
        assert "Traceback (most recent call last):" in err
        assert "1/0" in err
        assert "ZeroDivisionError:" in err
示例#26
0
    def test_module_subdomain_support(self):
        app = flask.Flask(__name__)
        mod = flask.Module(__name__, 'test', subdomain='testing')
        app.config['SERVER_NAME'] = 'localhost'

        @mod.route('/test')
        def test():
            return 'Test'

        @mod.route('/outside', subdomain='xtesting')
        def bar():
            return 'Outside'

        app.register_module(mod)

        c = app.test_client()
        rv = c.get('/test', 'http://testing.localhost/')
        assert rv.data == 'Test'
        rv = c.get('/outside', 'http://xtesting.localhost/')
        assert rv.data == 'Outside'
示例#27
0
    def test_module_subdomain_support(self):
        app = flask.Flask(__name__)
        mod = flask.Module(__name__, 'test', subdomain='testing')
        app.config['SERVER_NAME'] = 'localhost'

        @mod.route('/test')
        def test():
            return 'Test'

        @mod.route('/outside', subdomain='xtesting')
        def bar():
            return 'Outside'

        app.register_module(mod)

        c = app.test_client()
        rv = c.get('/test', 'http://testing.localhost/')
        assert rv.data == 'Test'
        rv = c.get('/outside', 'http://xtesting.localhost/')
        assert rv.data == 'Outside'
示例#28
0
    def test_module_subdomain_support(self):
        app = flask.Flask(__name__)
        mod = flask.Module(__name__, "test", subdomain="testing")
        app.config["SERVER_NAME"] = "localhost"

        @mod.route("/test")
        def test():
            return "Test"

        @mod.route("/outside", subdomain="xtesting")
        def bar():
            return "Outside"

        app.register_module(mod)

        c = app.test_client()
        rv = c.get("/test", "http://testing.localhost/")
        assert rv.data == "Test"
        rv = c.get("/outside", "http://xtesting.localhost/")
        assert rv.data == "Outside"
示例#29
0
    def test_endpoint_decorator(self):
        from werkzeug.routing import Submount, Rule
        from flask import Module

        app = flask.Flask(__name__)
        app.url_map.add(Submount("/foo", [Rule("/bar", endpoint="bar"), Rule("/", endpoint="index")]))
        module = Module(__name__, __name__)

        @module.endpoint("bar")
        def bar():
            return "bar"

        @module.endpoint("index")
        def index():
            return "index"

        app.register_module(module)

        c = app.test_client()
        assert c.get("/foo/").data == "index"
        assert c.get("/foo/bar").data == "bar"
示例#30
0
 def test_processor_exceptions(self):
     app = flask.Flask(__name__)
     @app.before_request
     def before_request():
         if trigger == 'before':
             1/0
     @app.after_request
     def after_request(response):
         if trigger == 'after':
             1/0
         return response
     @app.route('/')
     def index():
         return 'Foo'
     @app.errorhandler(500)
     def internal_server_error(e):
         return 'Hello Server Error', 500
     for trigger in 'before', 'after':
         rv = app.test_client().get('/')
         assert rv.status_code == 500
         assert rv.data == 'Hello Server Error'
示例#31
0
    def test_request_signals(self):
        app = flask.Flask(__name__)
        calls = []

        def before_request_signal(sender):
            calls.append('before-signal')

        def after_request_signal(sender, response):
            assert response.data == 'stuff'
            calls.append('after-signal')

        @app.before_request
        def before_request_handler():
            calls.append('before-handler')

        @app.after_request
        def after_request_handler(response):
            calls.append('after-handler')
            response.data = 'stuff'
            return response

        @app.route('/')
        def index():
            calls.append('handler')
            return 'ignored anyway'

        flask.request_started.connect(before_request_signal, app)
        flask.request_finished.connect(after_request_signal, app)

        try:
            rv = app.test_client().get('/')
            assert rv.data == 'stuff'

            assert calls == [
                'before-signal', 'before-handler', 'handler', 'after-handler',
                'after-signal'
            ]
        finally:
            flask.request_started.disconnect(before_request_signal, app)
            flask.request_finished.disconnect(after_request_signal, app)
示例#32
0
    def test_request_signals(self):
        app = flask.Flask(__name__)
        calls = []

        def before_request_signal(sender):
            calls.append('before-signal')

        def after_request_signal(sender, response):
            assert response.data == 'stuff'
            calls.append('after-signal')

        @app.before_request
        def before_request_handler():
            calls.append('before-handler')

        @app.after_request
        def after_request_handler(response):
            calls.append('after-handler')
            response.data = 'stuff'
            return response

        @app.route('/')
        def index():
            calls.append('handler')
            return 'ignored anyway'

        flask.request_started.connect(before_request_signal, app)
        flask.request_finished.connect(after_request_signal, app)

        try:
            rv = app.test_client().get('/')
            assert rv.data == 'stuff'

            assert calls == ['before-signal', 'before-handler',
                             'handler', 'after-handler',
                             'after-signal']
        finally:
            flask.request_started.disconnect(before_request_signal, app)
            flask.request_finished.disconnect(after_request_signal, app)
示例#33
0
    def test_request_signals(self):
        app = flask.Flask(__name__)
        calls = []

        def before_request_signal(sender):
            calls.append("before-signal")

        def after_request_signal(sender, response):
            assert response.data == "stuff"
            calls.append("after-signal")

        @app.before_request
        def before_request_handler():
            calls.append("before-handler")

        @app.after_request
        def after_request_handler(response):
            calls.append("after-handler")
            response.data = "stuff"
            return response

        @app.route("/")
        def index():
            calls.append("handler")
            return "ignored anyway"

        flask.request_started.connect(before_request_signal, app)
        flask.request_finished.connect(after_request_signal, app)

        try:
            rv = app.test_client().get("/")
            assert rv.data == "stuff"

            assert calls == ["before-signal", "before-handler", "handler", "after-handler", "after-signal"]
        finally:
            flask.request_started.disconnect(before_request_signal, app)
            flask.request_finished.disconnect(after_request_signal, app)
示例#34
0
    def test_endpoint_decorator(self):
        from werkzeug.routing import Submount, Rule
        from flask import Module

        app = flask.Flask(__name__)
        app.url_map.add(Submount('/foo', [
            Rule('/bar', endpoint='bar'),
            Rule('/', endpoint='index')
        ]))
        module = Module(__name__, __name__)

        @module.endpoint('bar')
        def bar():
            return 'bar'

        @module.endpoint('index')
        def index():
            return 'index'

        app.register_module(module)

        c = app.test_client()
        assert c.get('/foo/').data == 'index'
        assert c.get('/foo/bar').data == 'bar'