-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_ratelimit.py
126 lines (104 loc) · 3.55 KB
/
test_ratelimit.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#
# Test ratelimit fields
#
import http_sfv
def test_policy():
policies = [
"1000;w=3600,5000;w=86400",
"100;w=60",
"10;w=1, 50;w=60, 1000;w=3600, 5000;w=86400",
"10;w=1;burst=1000, 1000;w=3600",
]
for p in policies:
l = http_sfv.List()
l.parse(p.encode())
for i in l:
assert i.value
assert "w" in i.params
print("value: ", i.value, "params:", dict(i.params))
def test_all_in_one():
values = [
(
"limit=10, remaining=10, reset=4,"
"""policy=(10;w=1 50;w=60 1000;w=3600 5000;w=86400)"""
),
]
for v in values:
d = http_sfv.Dictionary()
d.parse(v.encode())
for k, v in d.items():
print("parameter:", k, "value:", v)
assert k
assert v
if hasattr(v, "__iter__"):
for i in v:
assert i.value
assert "w" in i.params
print("value: ", i.value, "params:", dict(i.params))
raise NotImplementedError
def find_quota_policy(policies, limit):
for policy in policies:
if policy.value == limit:
return policy.params
return {}
def parse_fields(headers):
# 27 µs ± 499 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
limit = http_sfv.Item()
limit.parse(headers["limit"].encode())
policies = http_sfv.List()
policies.parse(headers["policy"].encode())
quota_policy = find_quota_policy(policies, limit.value)
return limit, policies, quota_policy
def parse_fields_token(headers):
# 24.2 µs ± 506 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
limit = http_sfv.Item()
limit.parse(headers["limit"].encode())
policies = http_sfv.Dictionary()
policies.parse(headers["policy"].encode())
quota_policy = policies[f"q{limit.value}"]
return limit, policies, quota_policy.params
def parse_fields_int(headers):
# 24.5 µs ± 297 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
limit = int(headers["limit"])
policies = http_sfv.List()
policies.parse(headers["policy"].encode())
quota_policy = find_quota_policy(policies, limit)
return limit, policies, quota_policy
def test_get_policy():
# To retreive the policy, I need to iterate over the list.
headers = dict(
policy=(
"""10;w=1,"""
"""50;w=60,"""
"""1000;w=3600;comment="foo", """
"""5000;w=86400"""
),
limit="""1000""",
)
limit, policies, quota_policy = parse_fields(headers)
assert limit.value == 1000
for policy in policies:
print("parameter:", policy.value, "value:", policy.params["w"])
def test_get_policy_tokenized():
# To retreive the policy, I need to iterate over the list.
headers = dict(
policy=(
"""q10;w=1,"""
"""q50;w=60,"""
"""q1000;w=3600;comment="foo", """
"""q5000;w=86400"""
),
limit="""1000""",
)
limit = http_sfv.Item()
limit.parse(headers["limit"].encode())
assert limit.value == "q1000"
policies = http_sfv.List()
policies.parse(headers["policy"].encode())
for policy in policies:
print("parameter:", policy.value, "value:", policy.params["w"])
# I could create a dictionary out of the list.
policies_dict = {policy.value: policy.params for policy in policies}
print("policies:", policies_dict)
print("current policy:", policies_dict[limit.value])
raise NotImplementedError