Example #1
0
def test_build_error_handler():
    app = Flak(__name__)

    # Test base case, a URL which results in a BuildError.
    with app.test_context() as cx:
        pytest.raises(BuildError, cx.url_for, 'spam')

    # Verify the error is re-raised if not the current exception.
    try:
        with app.test_context() as cx:
            cx.url_for('spam')
    except BuildError as err:
        error = err
    try:
        raise RuntimeError('Test case where BuildError is not current.')
    except RuntimeError:
        pytest.raises(BuildError, app.handle_url_build_error,
                      None, error, 'spam', {})

    # Test a custom handler.
    def handler(cx, error, endpoint, values):
        # Just a test.
        return '/test_handler/'
    app.url_build_error_handlers.append(handler)
    with app.test_context() as cx:
        assert cx.url_for('spam') == '/test_handler/'
Example #2
0
def test_route_decorator_custom_endpoint():
    app = Flak(__name__)
    app.debug = True

    @app.route('/foo/')
    def foo(cx):
        return cx.request.endpoint

    @app.route('/bar/', endpoint='bar')
    def for_bar(cx):
        return cx.request.endpoint

    @app.route('/bar/123', endpoint='123')
    def for_bar_foo(cx):
        return cx.request.endpoint

    with app.test_context() as cx:
        assert cx.url_for('foo') == '/foo/'
        assert cx.url_for('bar') == '/bar/'
        assert cx.url_for('123') == '/bar/123'

    c = app.test_client()
    assert c.get('/foo/').data == b'foo'
    assert c.get('/bar/').data == b'bar'
    assert c.get('/bar/123').data == b'123'
Example #3
0
def test_build_error_handler_reraise():
    app = Flak(__name__)
    # Test a custom handler which reraises the BuildError
    def handler_raises_build_error(cx, error, endpoint, values):
        raise error
    app.url_build_error_handlers.append(handler_raises_build_error)

    with app.test_context() as cx:
        pytest.raises(BuildError, cx.url_for, 'not.existing')
Example #4
0
    def test_url_for_with_scheme(self):
        app = Flak(__name__)

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

        with app.test_context() as cx:
            assert cx.url_for("index", _external=True, _scheme="https") == "https://localhost/"
Example #5
0
    def test_url_for_with_scheme_not_external(self):
        app = Flak(__name__)

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

        with app.test_context() as cx:
            pytest.raises(ValueError, cx.url_for, "index", _scheme="https")
Example #6
0
def test_jsonify_no_prettyprint():
    app = Flak(__name__)
    app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False})
    with app.test_context() as cx:
        json = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n'
        obj = {"msg": {"submsg": "W00t"},
               "msg2": "foobar"}
        rv = cx.make_response(cx.jsonify(obj), 200)
        assert rv.data == json
Example #7
0
    def test_url_for_with_anchor(self):
        app = Flak(__name__)

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

        with app.test_context() as cx:
            assert cx.url_for("index", _anchor="x y") == "/#x%20y"
Example #8
0
def test_url_generation():
    app = Flak(__name__)

    @app.route('/hello/<name>', methods=['POST'])
    def hello(cx):
        pass
    with app.test_context() as cx:
        assert cx.url_for('hello', name='test x') == '/hello/test%20x'
        assert cx.url_for('hello', name='test x', _external=True) == \
            'http://localhost/hello/test%20x'
Example #9
0
def test_jsonify_prettyprint():
    app = Flak(__name__)
    app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True})
    with app.test_context() as cx:
        compressed = {"msg":{"submsg":"W00t"},"msg2":"foobar"}
        expect =\
            b'{\n  "msg": {\n    "submsg": "W00t"\n  }, \n  "msg2": "foobar"\n}\n'

        rv = cx.make_response(cx.jsonify(compressed), 200)
        assert rv.data == expect
Example #10
0
def test_environ_defaults():
    app = Flak(__name__)
    @app.route('/')
    def index(cx):
        return cx.request.url

    cx = app.test_context()
    assert cx.request.url == 'http://localhost/'
    with app.test_client() as c:
        rv = c.get('/')
        assert rv.data == b'http://localhost/'
Example #11
0
def test_environ_defaults_from_config():
    app = Flak(__name__)
    app.config['SERVER_NAME'] = 'example.com:1234'
    app.config['APPLICATION_ROOT'] = '/foo'
    @app.route('/')
    def index(cx):
        return cx.request.url

    cx = app.test_context()
    assert cx.request.url == 'http://example.com:1234/foo/'
    with app.test_client() as c:
        rv = c.get('/')
        assert rv.data == b'http://example.com:1234/foo/'
Example #12
0
def test_nosubdomain():
    app = Flak(__name__)
    app.config['SERVER_NAME'] = 'example.com'
    @app.route('/<company_id>')
    def view(cx, company_id):
        return company_id

    with app.test_context() as cx:
        url = cx.url_for('view', company_id='xxx')

    with app.test_client() as c:
        response = c.get(url)

    assert 200 == response.status_code
    assert b'xxx' == response.data
Example #13
0
def test_make_response():
    app = Flak(__name__)
    with app.test_context() as cx:
        rv = cx.make_response()
        assert rv.status_code == 200
        assert rv.data == b''
        assert rv.mimetype == 'text/plain'

        rv = cx.make_response('Awesome')
        assert rv.status_code == 200
        assert rv.data == b'Awesome'
        assert rv.mimetype == 'text/plain'

        rv = cx.make_response('W00t', 404)
        assert rv.status_code == 404
        assert rv.data == b'W00t'
        assert rv.mimetype == 'text/plain'
Example #14
0
def test_make_response_with_response_instance():
    app = Flak(__name__)
    with app.test_context() as cx:
        rv = cx.make_response(cx.jsonify({'msg': 'W00t'}), 400)
        assert rv.status_code == 400
        assert rv.data == b'{\n  "msg": "W00t"\n}\n'
        assert rv.mimetype == 'application/json'

        rv = cx.make_response(flak.Response(''), 400)
        assert rv.status_code == 400
        assert rv.data == b''
        assert rv.mimetype == 'text/plain'

        rsp = flak.Response('', headers={'Content-Type': 'text/html'})
        rv = cx.make_response(rsp, 400, [('X-Foo', 'bar')])
        assert rv.status_code == 400
        assert rv.headers['Content-Type'] == 'text/html'
        assert rv.headers['X-Foo'] == 'bar'
Example #15
0
    def test_url_with_method(self):
        from flak.views import MethodView

        app = Flak(__name__)

        class MyView(MethodView):
            def get(self, id=None):
                if id is None:
                    return "List"
                return "Get %d" % id

            def post(self):
                return "Create"

        myview = MyView.as_view("myview")
        app.add_url_rule("/myview/", myview, methods=["GET"])
        app.add_url_rule("/myview/<int:id>", myview, methods=["GET"])
        app.add_url_rule("/myview/create", myview, methods=["POST"])

        with app.test_context() as cx:
            assert cx.url_for("myview", _method="GET") == "/myview/"
            assert cx.url_for("myview", id=42, _method="GET") == "/myview/42"
            assert cx.url_for("myview", _method="POST") == "/myview/create"
Example #16
0
def test_context_has_app():
    app = Flak(__name__)
    with app.test_context() as cx:
        assert cx.app is app
Example #17
0
def test_null_session():
    app = Flak(__name__)
    with app.test_context() as cx:
        assert cx.session.get('missing_key') is None
        cx.session['foo'] = 42
        assert cx.session.get('foo') is None