File size: 11,147 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import enum

import pytest
from multidict import MultiDict

from yarl import URL

# with_query


def test_with_query():
    url = URL("http://example.com")
    assert str(url.with_query({"a": "1"})) == "http://example.com/?a=1"


def test_update_query():
    url = URL("http://example.com/")
    assert str(url.update_query({"a": "1"})) == "http://example.com/?a=1"
    assert str(URL("test").update_query(a=1)) == "test?a=1"

    url = URL("http://example.com/?foo=bar")
    expected_url = URL("http://example.com/?foo=bar&baz=foo")

    assert url.update_query({"baz": "foo"}) == expected_url
    assert url.update_query(baz="foo") == expected_url
    assert url.update_query("baz=foo") == expected_url


def test_update_query_with_args_and_kwargs():
    url = URL("http://example.com/")

    with pytest.raises(ValueError):
        url.update_query("a", foo="bar")


def test_update_query_with_multiple_args():
    url = URL("http://example.com/")

    with pytest.raises(ValueError):
        url.update_query("a", "b")


def test_update_query_with_none_arg():
    url = URL("http://example.com/?foo=bar&baz=foo")
    expected_url = URL("http://example.com/")
    assert url.update_query(None) == expected_url


def test_update_query_with_empty_dict():
    url = URL("http://example.com/?foo=bar&baz=foo")
    assert url.update_query({}) == url


def test_with_query_list_of_pairs():
    url = URL("http://example.com")
    assert str(url.with_query([("a", "1")])) == "http://example.com/?a=1"


def test_with_query_list_non_pairs():
    url = URL("http://example.com")
    with pytest.raises(ValueError):
        url.with_query(["a=1", "b=2", "c=3"])


def test_with_query_kwargs():
    url = URL("http://example.com")
    q = url.with_query(query="1", query2="1").query
    assert q == dict(query="1", query2="1")


def test_with_query_kwargs_and_args_are_mutually_exclusive():
    url = URL("http://example.com")
    with pytest.raises(ValueError):
        url.with_query({"a": "2", "b": "4"}, a="1")


def test_with_query_only_single_arg_is_supported():
    url = URL("http://example.com")
    u1 = url.with_query(b=3)
    u2 = URL("http://example.com/?b=3")
    assert u1 == u2
    with pytest.raises(ValueError):
        url.with_query("a=1", "a=b")


def test_with_query_empty_dict():
    url = URL("http://example.com/?a=b")
    new_url = url.with_query({})
    assert new_url.query_string == ""
    assert str(new_url) == "http://example.com/"


def test_with_query_empty_str():
    url = URL("http://example.com/?a=b")
    assert str(url.with_query("")) == "http://example.com/"


def test_with_query_empty_value():
    url = URL("http://example.com/")
    assert str(url.with_query({"a": ""})) == "http://example.com/?a="


def test_with_query_str():
    url = URL("http://example.com")
    assert str(url.with_query("a=1&b=2")) == "http://example.com/?a=1&b=2"


def test_with_query_str_non_ascii_and_spaces():
    url = URL("http://example.com")
    url2 = url.with_query("a=1 2&b=знач")
    assert url2.raw_query_string == "a=1+2&b=%D0%B7%D0%BD%D0%B0%D1%87"
    assert url2.query_string == "a=1 2&b=знач"


def test_with_query_int():
    url = URL("http://example.com")
    assert url.with_query({"a": 1}) == URL("http://example.com/?a=1")


def test_with_query_kwargs_int():
    url = URL("http://example.com")
    assert url.with_query(b=2) == URL("http://example.com/?b=2")


def test_with_query_list_int():
    url = URL("http://example.com")
    assert str(url.with_query([("a", 1)])) == "http://example.com/?a=1"


@pytest.mark.parametrize(
    ("query", "expected"),
    [
        pytest.param({"a": []}, "", id="empty list"),
        pytest.param({"a": ()}, "", id="empty tuple"),
        pytest.param({"a": [1]}, "/?a=1", id="single list"),
        pytest.param({"a": (1,)}, "/?a=1", id="single tuple"),
        pytest.param({"a": [1, 2]}, "/?a=1&a=2", id="list"),
        pytest.param({"a": (1, 2)}, "/?a=1&a=2", id="tuple"),
        pytest.param({"a[]": [1, 2]}, "/?a%5B%5D=1&a%5B%5D=2", id="key with braces"),
        pytest.param({"&": [1, 2]}, "/?%26=1&%26=2", id="quote key"),
        pytest.param({"a": ["1", 2]}, "/?a=1&a=2", id="mixed types"),
        pytest.param({"&": ["=", 2]}, "/?%26=%3D&%26=2", id="quote key and value"),
        pytest.param({"a": 1, "b": [2, 3]}, "/?a=1&b=2&b=3", id="single then list"),
        pytest.param({"a": [1, 2], "b": 3}, "/?a=1&a=2&b=3", id="list then single"),
        pytest.param({"a": ["1&a=2", 3]}, "/?a=1%26a%3D2&a=3", id="ampersand then int"),
        pytest.param({"a": [1, "2&a=3"]}, "/?a=1&a=2%26a%3D3", id="int then ampersand"),
    ],
)
def test_with_query_sequence(query, expected):
    url = URL("http://example.com")
    expected = "http://example.com{expected}".format_map(locals())
    assert str(url.with_query(query)) == expected


@pytest.mark.parametrize(
    "query",
    [
        pytest.param({"a": [[1]]}, id="nested"),
        pytest.param([("a", [1, 2])], id="tuple list"),
    ],
)
def test_with_query_sequence_invalid_use(query):
    url = URL("http://example.com")
    with pytest.raises(TypeError, match="Invalid variable type"):
        url.with_query(query)


class _CStr(str):
    pass


class _EmptyStrEr:
    def __str__(self):
        return ""  # pragma: no cover  # <-- this should never happen


class _CInt(int, _EmptyStrEr):
    pass


class _CFloat(float, _EmptyStrEr):
    pass


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        pytest.param("1", "1", id="str"),
        pytest.param(_CStr("1"), "1", id="custom str"),
        pytest.param(1, "1", id="int"),
        pytest.param(_CInt(1), "1", id="custom int"),
        pytest.param(1.1, "1.1", id="float"),
        pytest.param(_CFloat(1.1), "1.1", id="custom float"),
    ],
)
def test_with_query_valid_type(value, expected):
    url = URL("http://example.com")
    expected = "http://example.com/?a={expected}".format_map(locals())
    assert str(url.with_query({"a": value})) == expected


@pytest.mark.parametrize(
    ("value", "exc_type"),
    [
        pytest.param(True, TypeError, id="bool"),
        pytest.param(None, TypeError, id="none"),
        pytest.param(float("inf"), ValueError, id="non-finite float"),
        pytest.param(float("nan"), ValueError, id="NaN float"),
    ],
)
def test_with_query_invalid_type(value, exc_type):
    url = URL("http://example.com")
    with pytest.raises(exc_type):
        url.with_query({"a": value})


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        pytest.param("1", "1", id="str"),
        pytest.param(_CStr("1"), "1", id="custom str"),
        pytest.param(1, "1", id="int"),
        pytest.param(_CInt(1), "1", id="custom int"),
        pytest.param(1.1, "1.1", id="float"),
        pytest.param(_CFloat(1.1), "1.1", id="custom float"),
    ],
)
def test_with_query_list_valid_type(value, expected):
    url = URL("http://example.com")
    expected = "http://example.com/?a={expected}".format_map(locals())
    assert str(url.with_query([("a", value)])) == expected


@pytest.mark.parametrize(
    ("value"), [pytest.param(True, id="bool"), pytest.param(None, id="none")]
)
def test_with_query_list_invalid_type(value):
    url = URL("http://example.com")
    with pytest.raises(TypeError):
        url.with_query([("a", value)])


def test_with_int_enum():
    class IntEnum(int, enum.Enum):
        A = 1

    url = URL("http://example.com/path")
    url2 = url.with_query(a=IntEnum.A)
    assert str(url2) == "http://example.com/path?a=1"


def test_with_float_enum():
    class FloatEnum(float, enum.Enum):
        A = 1.1

    url = URL("http://example.com/path")
    url2 = url.with_query(a=FloatEnum.A)
    assert str(url2) == "http://example.com/path?a=1.1"


def test_with_query_multidict():
    url = URL("http://example.com/path")
    q = MultiDict([("a", "b"), ("c", "d")])
    assert str(url.with_query(q)) == "http://example.com/path?a=b&c=d"


def test_with_multidict_with_spaces_and_non_ascii():
    url = URL("http://example.com")
    url2 = url.with_query({"a b": "ю б"})
    assert url2.raw_query_string == "a+b=%D1%8E+%D0%B1"


def test_with_query_multidict_with_unsafe():
    url = URL("http://example.com/path")
    url2 = url.with_query({"a+b": "?=+&;"})
    assert url2.raw_query_string == "a%2Bb=?%3D%2B%26%3B"
    assert url2.query_string == "a%2Bb=?%3D%2B%26%3B"
    assert url2.query == {"a+b": "?=+&;"}


def test_with_query_None():
    url = URL("http://example.com/path?a=b")
    assert url.with_query(None).query_string == ""


def test_with_query_bad_type():
    url = URL("http://example.com")
    with pytest.raises(TypeError):
        url.with_query(123)


def test_with_query_bytes():
    url = URL("http://example.com")
    with pytest.raises(TypeError):
        url.with_query(b"123")


def test_with_query_bytearray():
    url = URL("http://example.com")
    with pytest.raises(TypeError):
        url.with_query(bytearray(b"123"))


def test_with_query_memoryview():
    url = URL("http://example.com")
    with pytest.raises(TypeError):
        url.with_query(memoryview(b"123"))


@pytest.mark.parametrize(
    ("query", "expected"),
    [
        pytest.param([("key", "1;2;3")], "?key=1%3B2%3B3", id="tuple list semicolon"),
        pytest.param({"key": "1;2;3"}, "?key=1%3B2%3B3", id="mapping semicolon"),
        pytest.param([("key", "1&a=2")], "?key=1%26a%3D2", id="tuple list ampersand"),
        pytest.param({"key": "1&a=2"}, "?key=1%26a%3D2", id="mapping ampersand"),
        pytest.param([("&", "=")], "?%26=%3D", id="tuple list quote key"),
        pytest.param({"&": "="}, "?%26=%3D", id="mapping quote key"),
        pytest.param(
            [("a[]", "3")],
            "?a%5B%5D=3",
            id="quote one key braces",
        ),
        pytest.param(
            [("a[]", "3"), ("a[]", "4")],
            "?a%5B%5D=3&a%5B%5D=4",
            id="quote many key braces",
        ),
    ],
)
def test_with_query_params(query, expected):
    url = URL("http://example.com/get")
    url2 = url.with_query(query)
    assert str(url2) == ("http://example.com/get" + expected)


def test_with_query_only():
    url = URL()
    url2 = url.with_query(key="value")
    assert str(url2) == "?key=value"


def test_with_query_complex_url():
    target_url = "http://example.com/?game=bulls+%26+cows"
    url = URL("/redir").with_query({"t": target_url})
    assert url.query["t"] == target_url


def test_update_query_multiple_keys():
    url = URL("http://example.com/path?a=1&a=2")
    u2 = url.update_query([("a", "3"), ("a", "4")])

    assert str(u2) == "http://example.com/path?a=3&a=4"


# mod operator


def test_update_query_with_mod_operator():
    url = URL("http://example.com/")
    assert str(url % {"a": "1"}) == "http://example.com/?a=1"
    assert str(url % [("a", "1")]) == "http://example.com/?a=1"
    assert str(url % "a=1&b=2") == "http://example.com/?a=1&b=2"
    assert str(url % {"a": "1"} % {"b": "2"}) == "http://example.com/?a=1&b=2"
    assert str(url % {"a": "1"} % {"a": "3", "b": "2"}) == "http://example.com/?a=3&b=2"
    assert str(url / "foo" % {"a": "1"}) == "http://example.com/foo?a=1"