Пример #1
0
def test_constructor_arguments_over_config(redis_connection):
    app = Flask(__name__)
    app.config.setdefault(C.STRATEGY, "fixed-window-elastic-expiry")
    limiter = Limiter(strategy='moving-window', key_func=get_remote_address)
    limiter.init_app(app)
    app.config.setdefault(C.STORAGE_URL, "redis://localhost:36379")
    assert type(limiter._limiter) == MovingWindowRateLimiter
    limiter = Limiter(storage_uri='memcached://localhost:31211',
                      key_func=get_remote_address)
    limiter.init_app(app)
    assert type(limiter._storage) == MemcachedStorage
Пример #2
0
    def test_multiple_apps(self):
        app1 = Flask(__name__)
        app2 = Flask(__name__)

        limiter = Limiter(default_limits=["1/second"],
                          key_func=get_remote_address)
        limiter.init_app(app1)
        limiter.init_app(app2)

        @app1.route("/ping")
        def ping():
            return "PONG"

        @app1.route("/slowping")
        @limiter.limit("1/minute")
        def slow_ping():
            return "PONG"

        @app2.route("/ping")
        @limiter.limit("2/second")
        def ping_2():
            return "PONG"

        @app2.route("/slowping")
        @limiter.limit("2/minute")
        def slow_ping_2():
            return "PONG"

        with hiro.Timeline().freeze() as timeline:
            with app1.test_client() as cli:
                self.assertEqual(cli.get("/ping").status_code, 200)
                self.assertEqual(cli.get("/ping").status_code, 429)
                timeline.forward(1)
                self.assertEqual(cli.get("/ping").status_code, 200)
                self.assertEqual(cli.get("/slowping").status_code, 200)
                timeline.forward(59)
                self.assertEqual(cli.get("/slowping").status_code, 429)
                timeline.forward(1)
                self.assertEqual(cli.get("/slowping").status_code, 200)
            with app2.test_client() as cli:
                self.assertEqual(cli.get("/ping").status_code, 200)
                self.assertEqual(cli.get("/ping").status_code, 200)
                self.assertEqual(cli.get("/ping").status_code, 429)
                timeline.forward(1)
                self.assertEqual(cli.get("/ping").status_code, 200)
                self.assertEqual(cli.get("/slowping").status_code, 200)
                timeline.forward(59)
                self.assertEqual(cli.get("/slowping").status_code, 200)
                self.assertEqual(cli.get("/slowping").status_code, 429)
                timeline.forward(1)
                self.assertEqual(cli.get("/slowping").status_code, 200)
Пример #3
0
def test_custom_headers_from_setter_and_config():
    app = Flask(__name__)
    app.config.setdefault(C.HEADER_LIMIT, "Limit")
    app.config.setdefault(C.HEADER_REMAINING, "Remaining")
    app.config.setdefault(C.HEADER_RESET, "Reset")
    limiter = Limiter(default_limits=["10/minute"],
                      headers_enabled=True,
                      key_func=get_remote_address)
    limiter._header_mapping[HEADERS.REMAINING] = 'Available'
    limiter.init_app(app)

    @app.route("/t1")
    def t():
        return "test"

    with app.test_client() as cli:
        for i in range(11):
            resp = cli.get("/t1")

        assert resp.headers.get('Limit') == '10'
        assert resp.headers.get('Available') == '0'
        assert resp.headers.get('Reset') is not None
Пример #4
0
# init SeaSurf
seasurf = SeaSurf(app)

# init flask CDN
CDN(app)

# init flask HTMLMIN
HTMLMIN(app)

# init assets environment
assets = Environment(app)
register_filter(BabiliFilter)


class MiniJSONEncoder(JSONEncoder):
    """Minify JSON output."""
    item_separator = ','
    key_separator = ':'

    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()+"Z"
        # Let the base class default method raise the TypeError
        return JSONEncoder.default(self, obj)

app.json_encoder = MiniJSONEncoder

# init rate limiting
limiter = Limiter(key_func=get_ipaddr, storage_uri="memory://", strategy="moving-window")
limiter.init_app(app)