forked from litestar-org/fast-query-parsers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parse_qsl.py
More file actions
68 lines (55 loc) · 2.03 KB
/
Copy pathtest_parse_qsl.py
File metadata and controls
68 lines (55 loc) · 2.03 KB
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
from __future__ import annotations
from urllib.parse import parse_qsl, urlencode
import pytest
from fast_query_parsers import parse_query_string
@pytest.mark.parametrize(
("qs", "expected"),
[
("", []),
("&", []),
("&&", []),
("=", [("", "")]),
("=a", [("", "a")]),
("a", [("a", "")]),
("a=", [("a", "")]),
("&a=b", [("a", "b")]),
("a=a+b&b=b+c", [("a", "a b"), ("b", "b c")]),
("a=1&a=2", [("a", "1"), ("a", "2")]),
(";a=b", [(";a", "b")]),
("a=a+b;b=b+c", [("a", "a b;b=b c")]),
],
)
def test_parse_qsl_standard_separator(qs: str, expected: list[tuple[str, str]]) -> None:
result = parse_query_string(qs.encode(), "&")
assert result == parse_qsl(qs, keep_blank_values=True) == expected
@pytest.mark.parametrize(
("qs", "expected"),
[
(";", []),
(";;", []),
(";a=b", [("a", "b")]),
("a=a+b;b=b+c", [("a", "a b"), ("b", "b c")]),
("a=1;a=2", [("a", "1"), ("a", "2")]),
],
)
def test_parse_qsl_semicolon_separator(qs: str, expected: list[tuple[str, str]]) -> None:
result = parse_query_string(qs.encode(), ";")
assert result == parse_qsl(qs, separator=";", keep_blank_values=True) == expected
@pytest.mark.parametrize(
"values",
[
(("first", "x@test.com"), ("second", "aaa")),
(("first", "&@A.ac"), ("second", "aaa")),
(("first", "a@A.ac&"), ("second", "aaa")),
(("first", "a@A&.ac"), ("second", "aaa")),
],
)
def test_query_parsing_of_escaped_values(values: tuple[tuple[str, str], tuple[str, str]]) -> None:
url_encoded = urlencode(values)
assert parse_query_string(url_encoded.encode(), "&") == list(values)
def test_parses_non_ascii_text() -> None:
assert parse_query_string("arabic_text=اختبار اللغة العربية".encode(), "&") == [
("arabic_text", "اختبار اللغة العربية"),
]
def test_kwargs() -> None:
assert parse_query_string(qs=b"foo=bar", separator="&") == [("foo", "bar")]