예제 #1
0
파일: test_basic.py 프로젝트: avdd/flak
def test_preserve_remembers_exception():
    app = Flak(__name__)
    app.debug = True
    errors = []

    @app.route('/fail')
    def fail_func(cx):
        1 // 0

    @app.route('/success')
    def success_func(cx):
        return 'Okay'

    @app.teardown
    def teardown_handler(cx, exc):
        errors.append(exc)

    c = app.test_client()

    # After this failure we did not yet call the teardown handler
    with pytest.raises(ZeroDivisionError):
        c.get('/fail')
    #assert errors == []
    assert len(errors) == 1

    # But this request triggers it, and it's an error
    c.get('/success')
    assert len(errors) == 2

    # At this point another request does nothing.
    c.get('/success')
    assert len(errors) == 3
    assert errors[1] is None
예제 #2
0
def test_error_handler_no_match():
    app = Flak(__name__)

    class CustomException(Exception):
        pass

    @app.errorhandler(CustomException)
    def custom_exception_handler(cx, e):
        assert isinstance(e, CustomException)
        return 'custom'

    @app.errorhandler(500)
    def handle_500(cx, e):
        return type(e).__name__

    @app.route('/custom')
    def custom_test(cx):
        raise CustomException()

    @app.route('/keyerror')
    def key_error(cx):
        raise KeyError()

    c = app.test_client()

    assert c.get('/custom').data == b'custom'
    assert c.get('/keyerror').data == b'KeyError'
예제 #3
0
파일: test_basic.py 프로젝트: avdd/flak
def test_url_processors():
    app = Flak(__name__)

    @app.url_defaults
    def add_language_code(cx, endpoint, values):
        if cx.lang_code is not None and \
           app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
            values.setdefault('lang_code', cx.lang_code)

    @app.url_value_preprocessor
    def pull_lang_code(cx, endpoint, values):
        cx.lang_code = values.pop('lang_code', None)

    @app.route('/<lang_code>/')
    def index(cx):
        return cx.url_for('about')

    @app.route('/<lang_code>/about')
    def about(cx):
        return cx.url_for('something_else')

    @app.route('/foo')
    def something_else(cx):
        return cx.url_for('about', lang_code='en')

    c = app.test_client()

    assert c.get('/de/').data == b'/de/about'
    assert c.get('/de/about').data == b'/foo'
    assert c.get('/foo').data == b'/en/about'
예제 #4
0
파일: test_testing.py 프로젝트: avdd/flak
def test_reuse_client():
    app = Flak(__name__)
    c = app.test_client()
    with c:
        assert c.get('/').status_code == 404
    with c:
        assert c.get('/').status_code == 404
예제 #5
0
파일: test_helpers.py 프로젝트: avdd/flak
    def test_streaming_with_context_and_custom_close(self):
        app = Flak(__name__)
        called = []

        class Wrapper(object):
            def __init__(self, gen):
                self._gen = gen

            def __iter__(self):
                return self

            def close(self):
                called.append(42)

            def __next__(self):
                return next(self._gen)

            next = __next__

        @app.route("/")
        def index(cx):
            def generate():
                yield "Hello "
                yield cx.request.args["name"]
                yield "!"

            gen = cx.close_with_generator(Wrapper(generate()))
            return flak.Response(gen)

        c = app.test_client()
        rv = c.get("/?name=World")
        assert rv.data == b"Hello World!"
        assert called == [42]
예제 #6
0
파일: test_basic.py 프로젝트: avdd/flak
def test_request_preprocessing_early_return():
    app = Flak(__name__)
    evts = []

    @app.before_request
    def before_request1(cx):
        evts.append(1)

    @app.before_request
    def before_request2(cx):
        evts.append(2)
        return "hello"

    @app.before_request
    def before_request3(cx):
        evts.append(3)
        return "bye"

    @app.route('/')
    def index(cx):
        evts.append('index')
        return "damnit"

    rv = app.test_client().get('/').data.strip()
    assert rv == b'hello'
    assert evts == [1, 2]
예제 #7
0
파일: test_basic.py 프로젝트: avdd/flak
def test_http_error_subclass_handling():
    class ForbiddenSubclass(Forbidden):
        pass

    app = Flak(__name__)

    @app.errorhandler(ForbiddenSubclass)
    def handle_forbidden_subclass(cx, e):
        assert isinstance(e, ForbiddenSubclass)
        return 'banana'

    @app.errorhandler(403)
    def handle_forbidden_subclass(cx, e):
        assert not isinstance(e, ForbiddenSubclass)
        assert isinstance(e, Forbidden)
        return 'apple'

    @app.route('/1')
    def index1(cx):
        raise ForbiddenSubclass()

    @app.route('/2')
    def index2(cx):
        flak.abort(403)

    @app.route('/3')
    def index3(cx):
        raise Forbidden()

    c = app.test_client()
    assert c.get('/1').data == b'banana'
    assert c.get('/2').data == b'apple'
    assert c.get('/3').data == b'apple'
예제 #8
0
파일: test_basic.py 프로젝트: avdd/flak
def test_session_special_types():
    app = Flak(__name__)
    app.secret_key = 'development-key'
    now = datetime.utcnow().replace(microsecond=0)
    the_uuid = uuid.uuid4()

    @app.after_request
    def modify_session(cx, response):
        cx.session['u'] = the_uuid
        cx.session['dt'] = now
        cx.session['b'] = b'\xff'
        cx.session['t'] = (1, 2, 3)
        return response

    @app.route('/')
    def dump_session_contents(cx):
        return pickle.dumps(dict(cx.session))

    c = app.test_client()
    c.get('/')
    rv = pickle.loads(c.get('/').data)
    assert rv['dt'] == now
    assert rv['u'] == the_uuid
    assert rv['b'] == b'\xff'
    assert type(rv['b']) == bytes
    assert rv['t'] == (1, 2, 3)
예제 #9
0
파일: test_basic.py 프로젝트: avdd/flak
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/'
예제 #10
0
파일: test_helpers.py 프로젝트: avdd/flak
    def test_json_customization(self):
        class X(object):
            def __init__(self, val):
                self.val = val

        class MyEncoder(flak.json.JSONEncoder):
            def default(self, o):
                if isinstance(o, X):
                    return "<%d>" % o.val
                return flak.json.JSONEncoder.default(self, o)

        class MyDecoder(flak.json.JSONDecoder):
            def __init__(self, *args, **kwargs):
                kwargs.setdefault("object_hook", self.object_hook)
                flak.json.JSONDecoder.__init__(self, *args, **kwargs)

            def object_hook(self, obj):
                if len(obj) == 1 and "_foo" in obj:
                    return X(obj["_foo"])
                return obj

        app = Flak(__name__)
        app.json_encoder = MyEncoder
        app.json_decoder = MyDecoder

        @app.route("/", methods=["POST"])
        def index(cx):
            return cx.dumps(cx.get_json()["x"])

        c = app.test_client()
        rv = c.post("/", data=json.dumps({"x": {"_foo": 42}}), content_type="application/json")
        assert rv.data == b'"<42>"'
예제 #11
0
파일: test_helpers.py 프로젝트: avdd/flak
 def test_logger_cache(self):
     app = Flak(__name__)
     logger1 = app.logger
     assert app.logger is logger1
     assert logger1.name == __name__
     app.logger_name = __name__ + "/test_logger_cache"
     assert app.logger is not logger1
예제 #12
0
파일: test_helpers.py 프로젝트: avdd/flak
    def test_processor_exceptions(self):
        app = Flak(__name__)
        app.config["LOGGER_HANDLER_POLICY"] = "never"

        @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(cx, e):
            return "Hello Server Error", 500

        for trigger in "before", "after":
            rv = app.test_client().get("/")
            assert rv.status_code == 500
            assert rv.data == b"Hello Server Error"
예제 #13
0
파일: test_basic.py 프로젝트: avdd/flak
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'
예제 #14
0
파일: test_basic.py 프로젝트: avdd/flak
def test_session_expiration():
    permanent = True
    app = Flak(__name__)
    app.secret_key = 'testkey'

    @app.route('/')
    def index(cx):
        cx.session['test'] = 42
        cx.session.permanent = permanent
        return ''

    @app.route('/test')
    def test(cx):
        return text_type(cx.session.permanent)

    client = app.test_client()
    rv = client.get('/')
    assert 'set-cookie' in rv.headers
    match = re.search(r'\bexpires=([^;]+)(?i)', rv.headers['set-cookie'])
    expires = parse_date(match.group())
    expected = datetime.utcnow() + app.permanent_session_lifetime
    assert expires.year == expected.year
    assert expires.month == expected.month
    assert expires.day == expected.day

    rv = client.get('/test')
    assert rv.data == b'True'

    permanent = False
    rv = app.test_client().get('/')
    assert 'set-cookie' in rv.headers
    match = re.search(r'\bexpires=([^;]+)', rv.headers['set-cookie'])
    assert match is None
예제 #15
0
파일: test_testing.py 프로젝트: avdd/flak
def test_test_client_context_binding():
    app = Flak(__name__)
    app.config['LOGGER_HANDLER_POLICY'] = 'never'
    @app.route('/')
    def index(cx):
        cx.globals.value = 42
        return 'Hello World!'

    @app.route('/other')
    def other(cx):
        cx.globals.value = 23
        1 // 0

    with app.test_client() as c:
        resp = c.get('/')
        cx = c.captured_context
        assert cx.globals.value == 42
        assert resp.data == b'Hello World!'
        assert resp.status_code == 200

        resp = c.get('/other')
        assert b'Internal Server Error' in resp.data
        assert resp.status_code == 500
        assert c.captured_context is not cx
        cx = c.captured_context
        assert cx.globals.value == 23
예제 #16
0
파일: test_testing.py 프로젝트: avdd/flak
def test_redirect_keep_session():
    app = Flak(__name__)
    app.secret_key = 'testing'

    @app.route('/', methods=['GET', 'POST'])
    def index(cx):
        if cx.request.method == 'POST':
            return flak.redirect('/getsession')
        cx.session['data'] = 'foo'
        return 'index'

    @app.route('/getsession')
    def get_session(cx):
        return cx.session.get('data', '<missing>')

    with app.test_client() as c:
        rv = c.get('/getsession')
        assert rv.data == b'<missing>'

        rv = c.get('/')
        assert rv.data == b'index'
        rv = c.post('/', data={}, follow_redirects=True)
        assert rv.data == b'foo'
        rv = c.get('/getsession')
        assert rv.data == b'foo'
예제 #17
0
파일: test_basic.py 프로젝트: avdd/flak
def test_response_creation():
    app = Flak(__name__)

    @app.route('/unicode')
    def from_unicode(cx):
        return u'Hällo Wörld'

    @app.route('/string')
    def from_string(cx):
        return u'Hällo Wörld'.encode('utf-8')

    @app.route('/args')
    def from_tuple(cx):
        return 'Meh', 400, {
            'X-Foo': 'Testing',
            'Content-Type': 'text/plain; charset=utf-8'
        }

    @app.route('/two_args')
    def from_two_args_tuple(cx):
        return 'Hello', {
            'X-Foo': 'Test',
            'Content-Type': 'text/plain; charset=utf-8'
        }

    @app.route('/args_status')
    def from_status_tuple(cx):
        return 'Hi, status!', 400

    @app.route('/args_header')
    def from_response_instance_status_tuple(cx):
        return flak.Response('Hello world', 404), {
            "X-Foo": "Bar",
            "X-Bar": "Foo"
        }

    c = app.test_client()
    assert c.get('/unicode').data == u'Hällo Wörld'.encode('utf-8')
    assert c.get('/string').data == u'Hällo Wörld'.encode('utf-8')
    rv = c.get('/args')
    assert rv.data == b'Meh'
    assert rv.headers['X-Foo'] == 'Testing'
    assert rv.status_code == 400
    assert rv.mimetype == 'text/plain'
    rv = c.get('/two_args')
    assert rv.data == b'Hello'
    assert rv.headers['X-Foo'] == 'Test'
    assert rv.status_code == 200
    assert rv.mimetype == 'text/plain'
    rv = c.get('/args_status')
    assert rv.data == b'Hi, status!'
    assert rv.status_code == 400
    assert rv.mimetype == 'text/plain'
    rv = c.get('/args_header')
    assert rv.data == b'Hello world'
    assert rv.headers['X-Foo'] == 'Bar'
    assert rv.headers['X-Bar'] == 'Foo'
    assert rv.status_code == 404
예제 #18
0
파일: test_basic.py 프로젝트: avdd/flak
def test_options_work():
    app = Flak(__name__)

    @app.route('/', methods=['GET', 'POST'])
    def index(cx):
        return 'Hello World'
    rv = app.test_client().open('/', method='OPTIONS')
    assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST']
    assert rv.data == b''
예제 #19
0
파일: test_helpers.py 프로젝트: avdd/flak
    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")
예제 #20
0
파일: test_helpers.py 프로젝트: avdd/flak
    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/"
예제 #21
0
파일: test_helpers.py 프로젝트: avdd/flak
    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"
예제 #22
0
파일: test_basic.py 프로젝트: avdd/flak
def test_g_iteration_protocol():
    app = Flak(__name__)

    with app.new_context() as cx:
        cx.globals.foo = 23
        cx.globals.bar = 42
        assert 'foo' in cx.globals
        assert 'foos' not in cx.globals
        assert sorted(cx.globals) == ['bar', 'foo']
예제 #23
0
파일: test_basic.py 프로젝트: avdd/flak
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
예제 #24
0
파일: test_basic.py 프로젝트: avdd/flak
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')
예제 #25
0
파일: test_basic.py 프로젝트: avdd/flak
def test_get_method_on_g():
    app = Flak(__name__)

    with app.new_context() as cx:
        assert cx.globals.get('x') is None
        assert cx.globals.get('x', 11) == 11
        cx.globals.x = 42
        assert cx.globals.get('x') == 42
        assert cx.globals.x == 42
예제 #26
0
파일: test_testing.py 프로젝트: avdd/flak
def test_session_transaction_needs_cookies():
    app = Flak(__name__)
    c = app.test_client(use_cookies=False)
    try:
        with c.session() as s:
            pass
    except RuntimeError as e:
        assert 'cookies' in str(e)
    else:
        assert False, 'Expected runtime error'
예제 #27
0
파일: test_basic.py 프로젝트: avdd/flak
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
예제 #28
0
파일: test_basic.py 프로젝트: avdd/flak
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'
예제 #29
0
파일: test_helpers.py 프로젝트: avdd/flak
    def test_json_bad_requests(self):
        app = Flak(__name__)

        @app.route("/json", methods=["POST"])
        def return_json(cx):
            return cx.jsonify(foo=text_type(cx.get_json()))

        c = app.test_client()
        rv = c.post("/json", data="malformed", content_type="application/json")
        assert rv.status_code == 400
예제 #30
0
파일: test_helpers.py 프로젝트: avdd/flak
    def test_json_custom_mimetypes(self):
        app = Flak(__name__)

        @app.route("/json", methods=["POST"])
        def return_json(cx):
            return cx.get_json()

        c = app.test_client()
        rv = c.post("/json", data='"foo"', content_type="application/x+json")
        assert rv.data == b"foo"