File: test_storage.py

package info (click to toggle)
flask-limiter 3.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,264 kB
  • sloc: python: 6,432; makefile: 165; sh: 67
file content (69 lines) | stat: -rw-r--r-- 1,665 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from __future__ import annotations

import hiro
import pytest


@pytest.fixture(autouse=True)
def setup(redis_connection, memcached_connection, mongo_connection):
    redis_connection.flushall()
    memcached_connection.flush_all()


@pytest.mark.parametrize(
    "storage_uri",
    [
        "memcached://localhost:31211",
        "redis://localhost:46379",
        "mongodb://localhost:47017",
    ],
)
def test_fixed_window(extension_factory, storage_uri):
    app, limiter = extension_factory(
        application_limits=["2/minute"],
        storage_uri=storage_uri,
        strategy="fixed-window",
    )

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

    @app.route("/t2")
    def t2():
        return "route2"

    with hiro.Timeline().freeze():
        with app.test_client() as cli:
            assert 200 == cli.get("/t1").status_code
            assert 200 == cli.get("/t2").status_code
            assert 429 == cli.get("/t1").status_code


@pytest.mark.parametrize(
    "storage_uri",
    [
        "redis://localhost:46379",
        "mongodb://localhost:47017",
    ],
)
def test_moving_window(extension_factory, storage_uri):
    app, limiter = extension_factory(
        application_limits=["2/minute"],
        storage_uri=storage_uri,
        strategy="moving-window",
    )

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

    @app.route("/t2")
    def t2():
        return "route2"

    with hiro.Timeline().freeze():
        with app.test_client() as cli:
            assert 200 == cli.get("/t1").status_code
            assert 200 == cli.get("/t2").status_code
            assert 429 == cli.get("/t1").status_code