eggfetch-python 0.1.4

Python sync and asyncio bindings for the eggfetch HTTP engine (Rust core via PyO3; Python users install from PyPI)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
"""Comprehensive tests for URL and QueryParams."""

import pytest

from eggfetch.compat.httpx import URL, QueryParams


# ── URL construction ────────────────────────────────────────────────────

class TestURLConstruction:
    def test_from_string(self):
        url = URL("https://example.com/path")
        assert str(url) == "https://example.com/path"

    def test_from_bytes(self):
        url = URL(b"https://example.com/path")
        assert url.host == "example.com"

    def test_from_existing_url(self):
        a = URL("https://example.com/path")
        b = URL(a)
        assert a is b  # identity return for URL input

    def test_none_yields_http_prefix(self):
        url = URL(None)
        assert url._raw == ""

    def test_relative_string(self):
        url = URL("/path/to/resource")
        assert url.path == "/path/to/resource"

    def test_empty_string(self):
        url = URL("")
        assert url._raw == ""


# ── URL properties ──────────────────────────────────────────────────────

class TestURLProperties:
    def test_scheme(self):
        assert URL("https://example.com").scheme == "https"
        assert URL("http://example.com").scheme == "http"

    def test_host(self):
        assert URL("https://example.com").host == "example.com"
        assert URL("https://Example.COM").host == "example.com"

    def test_port_explicit(self):
        assert URL("https://example.com:8443").port == 8443

    def test_port_default_http(self):
        assert URL("http://example.com:80").port == 80

    def test_port_default_https(self):
        assert URL("https://example.com:443").port == 443

    def test_port_none_when_omitted(self):
        assert URL("https://example.com").port is None

    def test_path(self):
        assert URL("https://example.com/a/b/c").path == "/a/b/c"

    def test_path_empty(self):
        assert URL("https://example.com").path == ""

    def test_query(self):
        url = URL("https://example.com?q=1&r=2")
        assert url.query == b"q=1&r=2"

    def test_query_empty(self):
        assert URL("https://example.com").query == b""

    def test_fragment(self):
        assert URL("https://example.com/path#section").fragment == "section"

    def test_fragment_empty(self):
        assert URL("https://example.com").fragment == ""

    def test_username(self):
        assert URL("https://user@example.com").username == "user"

    def test_password(self):
        assert URL("https://user:pass@example.com").password == "pass"

    def test_netloc(self):
        url = URL("https://example.com:8443/path")
        assert url.netloc == b"example.com:8443"

    def test_userinfo_with_password(self):
        url = URL("https://user:pass@example.com")
        assert url.userinfo == b"user:pass"

    def test_userinfo_without_password(self):
        url = URL("https://user@example.com")
        assert url.userinfo == b"user"

    def test_userinfo_empty(self):
        url = URL("https://example.com")
        assert url.userinfo == b""

    def test_raw_host(self):
        assert URL("https://example.com").raw_host == b"example.com"

    def test_raw_host_none(self):
        assert URL("").raw_host is None

    def test_raw_path(self):
        assert URL("https://example.com/a/b").raw_path == b"/a/b"

    def test_raw_path_empty_defaults_slash(self):
        assert URL("https://example.com").raw_path == b"/"

    def test_raw_scheme(self):
        assert URL("https://example.com").raw_scheme == b"https"


# ── Absolute / relative ────────────────────────────────────────────────

class TestURLAbsoluteRelative:
    def test_is_absolute_url(self):
        assert URL("https://example.com").is_absolute_url is True
        assert URL("http://example.com/path").is_absolute_url is True

    def test_is_relative_url(self):
        assert URL("/path").is_relative_url is True

    def test_absolute_not_relative(self):
        url = URL("https://example.com")
        assert url.is_absolute_url is True
        assert url.is_relative_url is False

    def test_relative_not_absolute(self):
        url = URL("/path")
        assert url.is_relative_url is True
        assert url.is_absolute_url is False


# ── copy_with / param helpers ──────────────────────────────────────────

class TestURLCopyWith:
    def test_copy_with_params(self):
        url = URL("https://example.com/path")
        new = url.copy_with(params={"q": "1"})
        assert str(new) == "https://example.com/path?q=1"

    def test_copy_set_param(self):
        url = URL("https://example.com?a=1&b=2")
        new = url.copy_set_param("a", "9")
        assert "a=9" in str(new)
        assert "b=2" in str(new)

    def test_copy_remove_param(self):
        url = URL("https://example.com?a=1&b=2")
        new = url.copy_remove_param("a")
        assert "a=" not in str(new)
        assert "b=2" in str(new)

    def test_copy_merge_params(self):
        url = URL("https://example.com?a=1")
        new = url.copy_merge_params({"b": "2"})
        assert "a=1" in str(new)
        assert "b=2" in str(new)

    def test_copy_add_param(self):
        url = URL("https://example.com?a=1")
        new = url.copy_add_param("b", "2")
        assert "a=1" in str(new)
        assert "b=2" in str(new)


# ── URL join ────────────────────────────────────────────────────────────

class TestURLJoin:
    def test_join_relative(self):
        base = URL("https://example.com/a/b")
        joined = base.join(URL("c/d"))
        # RFC 3986 reference resolution, not string concatenation.
        assert str(joined) == "https://example.com/a/c/d"

    def test_join_absolute_path(self):
        base = URL("http://example.com/foo/bar")
        assert str(base.join(URL("/new/path"))) == "http://example.com/new/path"

    def test_join_protocol_relative(self):
        base = URL("http://example.com/foo/")
        assert str(base.join(URL("//other.com/x"))) == "http://other.com/x"

    def test_join_queryparams_obj(self):
        from eggfetch.compat.httpx import QueryParams
        url = URL("https://example.com/path")
        qp = QueryParams({"q": "test"})
        new = url.copy_with(params=qp)
        assert "q=test" in str(new)


# ── Default port stripping ─────────────────────────────────────────────

class TestURLDefaultPort:
    def test_http_80_stripped(self):
        url = URL("http://example.com:80/path")
        assert ":80" not in str(url)
        assert str(url) == "http://example.com/path"

    def test_https_443_stripped(self):
        url = URL("https://example.com:443/path")
        assert ":443" not in str(url)
        assert str(url) == "https://example.com/path"

    def test_non_default_port_kept(self):
        url = URL("http://example.com:8080/path")
        assert ":8080" in str(url)


# ── Credential redaction in repr ───────────────────────────────────────

class TestURLRepr:
    def test_password_redacted(self):
        url = URL("https://user:secret@example.com")
        r = repr(url)
        assert "secret" not in r
        assert "***" in r

    def test_no_password_no_redaction(self):
        url = URL("https://user@example.com")
        assert "user" in repr(url)


# ── IPv6 ────────────────────────────────────────────────────────────────

class TestURLIPv6:
    def test_ipv6_host(self):
        url = URL("https://[::1]:8443/path")
        assert url.host == "::1"
        assert url.port == 8443


# ── Unicode hosts ──────────────────────────────────────────────────────

class TestURLUnicode:
    def test_unicode_host(self):
        url = URL("https://münchen.de/path")
        assert "münchen" in (url.host or "")


# ── QueryParams construction ──────────────────────────────────────────

class TestQueryParamsConstruction:
    def test_from_dict(self):
        qp = QueryParams({"a": "1", "b": "2"})
        assert qp["a"] == "1"
        assert qp["b"] == "2"

    def test_from_list_of_tuples(self):
        qp = QueryParams([("a", "1"), ("b", "2")])
        assert qp["a"] == "1"
        assert qp["b"] == "2"

    def test_from_string(self):
        qp = QueryParams("a=1&b=2")
        assert qp["a"] == "1"
        assert qp["b"] == "2"

    def test_from_queryparams(self):
        original = QueryParams({"a": "1"})
        copy = QueryParams(original)
        assert copy["a"] == "1"
        assert copy is not original

    def test_from_none(self):
        qp = QueryParams(None)
        assert len(qp) == 0

    def test_invalid_type_raises(self):
        with pytest.raises(TypeError):
            QueryParams(123)

    def test_empty_string(self):
        qp = QueryParams("")
        assert len(qp) == 0


# ── QueryParams accessors ─────────────────────────────────────────────

class TestQueryParamsAccess:
    def test_get_returns_last(self):
        qp = QueryParams([("a", "1"), ("a", "2")])
        assert qp.get("a") == "2"

    def test_get_default(self):
        qp = QueryParams()
        assert qp.get("missing") is None
        assert qp.get("missing", "fallback") == "fallback"

    def test_get_list(self):
        qp = QueryParams([("a", "1"), ("a", "2"), ("b", "3")])
        assert qp.get_list("a") == ["1", "2"]
        assert qp.get_list("b") == ["3"]
        assert qp.get_list("missing") == []

    def test_multi_items(self):
        qp = QueryParams([("a", "1"), ("a", "2")])
        assert qp.multi_items() == [("a", "1"), ("a", "2")]

    def test_keys(self):
        qp = QueryParams([("a", "1"), ("a", "2"), ("b", "3")])
        assert qp.keys() == ["a", "b"]

    def test_values(self):
        qp = QueryParams([("a", "1"), ("a", "2"), ("b", "3")])
        assert qp.values() == ["1", "3"]

    def test_items(self):
        qp = QueryParams([("a", "1"), ("a", "2"), ("b", "3")])
        assert qp.items() == [("a", "1"), ("b", "3")]


# ── QueryParams mutation ───────────────────────────────────────────────

class TestQueryParamsMutation:
    def test_add(self):
        qp = QueryParams({"a": "1"})
        qp.add("b", "2")
        assert qp.multi_items() == [("a", "1"), ("b", "2")]

    def test_set(self):
        qp = QueryParams({"a": "1", "b": "2"})
        qp.set("a", "9")
        assert qp["a"] == "9"

    def test_remove(self):
        qp = QueryParams({"a": "1", "b": "2"})
        qp.remove("a")
        assert "a" not in qp

    def test_update_dict(self):
        qp = QueryParams({"a": "1"})
        qp.update({"a": "9", "b": "2"})
        assert qp["a"] == "9"
        assert qp["b"] == "2"

    def test_update_queryparams(self):
        qp = QueryParams({"a": "1"})
        qp.update(QueryParams({"a": "9", "b": "2"}))
        assert qp["a"] == "9"
        assert qp["b"] == "2"

    def test_merge(self):
        qp = QueryParams({"a": "1"})
        qp.merge({"a": "9"})
        assert qp.get_list("a") == ["1", "9"]


# ── QueryParams dunder methods ────────────────────────────────────────

class TestQueryParamsDunder:
    def test_eq(self):
        assert QueryParams({"a": "1"}) == QueryParams({"a": "1"})
        assert QueryParams({"a": "1"}) != QueryParams({"a": "2"})

    def test_hash(self):
        a = QueryParams({"a": "1"})
        b = QueryParams({"a": "1"})
        assert hash(a) == hash(b)

    def test_str(self):
        qp = QueryParams({"a": "1"})
        assert "a=1" in str(qp)

    def test_repr(self):
        qp = QueryParams({"a": "1"})
        assert "QueryParams" in repr(qp)

    def test_bool_empty(self):
        assert not QueryParams()

    def test_bool_nonempty(self):
        assert QueryParams({"a": "1"})

    def test_len(self):
        assert len(QueryParams({"a": "1", "b": "2"})) == 2

    def test_contains(self):
        qp = QueryParams({"a": "1"})
        assert "a" in qp
        assert "b" not in qp

    def test_iter(self):
        qp = QueryParams([("a", "1"), ("b", "2")])
        assert list(qp) == ["a", "b"]

    def test_getitem_missing(self):
        with pytest.raises(KeyError):
            QueryParams()["missing"]

    def test_delitem(self):
        qp = QueryParams({"a": "1", "b": "2"})
        del qp["a"]
        assert "a" not in qp

    def test_delitem_missing(self):
        with pytest.raises(KeyError):
            del QueryParams()["missing"]


class TestQueryParamsDuplicateConversion:
    """Test that duplicate query params survive conversion for native client."""

    def test_duplicate_params_preserved_in_list(self):
        """QueryParams with duplicates should convert to list of tuples."""
        qp = QueryParams([("a", "1"), ("a", "2"), ("b", "3")])
        items = qp.multi_items()
        assert len(items) == 3
        assert ("a", "1") in items
        assert ("a", "2") in items
        assert ("b", "3") in items

    def test_string_query_preserves_duplicates(self):
        """String query with repeated keys should preserve duplicates."""
        qp = QueryParams("a=1&a=2&b=&a=3")
        items = qp.multi_items()
        assert len(items) == 4
        assert ("a", "1") in items
        assert ("a", "2") in items
        assert ("b", "") in items
        assert ("a", "3") in items

    def test_merge_preserves_multiplicity(self):
        """QueryParams merge should preserve multiplicity of both sides."""
        base = QueryParams([("a", "1"), ("a", "2")])
        extra = QueryParams([("a", "3"), ("b", "4")])
        base.merge(extra)
        items = base.multi_items()
        assert len(items) == 4
        assert ("a", "1") in items
        assert ("a", "2") in items
        assert ("a", "3") in items
        assert ("b", "4") in items


# ── QueryParams Mapping ABC ────────────────────────────────────────────

class TestQueryParamsMappingABC:
    def test_isinstance_mapping(self):
        from collections.abc import Mapping
        qp = QueryParams()
        assert isinstance(qp, Mapping)

    def test_getitem(self):
        qp = QueryParams({"a": "1"})
        assert qp["a"] == "1"

    def test_get(self):
        qp = QueryParams({"a": "1"})
        assert qp.get("a") == "1"
        assert qp.get("missing", "default") == "default"

    def test_keys(self):
        qp = QueryParams([("a", "1"), ("b", "2")])
        assert set(qp.keys()) == {"a", "b"}

    def test_values(self):
        qp = QueryParams({"a": "1", "b": "2"})
        assert set(qp.values()) == {"1", "2"}

    def test_items(self):
        qp = QueryParams({"a": "1"})
        assert ("a", "1") in list(qp.items())

    def test_len(self):
        assert len(QueryParams({"a": "1", "b": "2"})) == 2

    def test_contains(self):
        qp = QueryParams({"a": "1"})
        assert "a" in qp
        assert "b" not in qp

    def test_iter(self):
        qp = QueryParams([("a", "1"), ("b", "2")])
        assert set(iter(qp)) == {"a", "b"}


# ── URL.raw property ──────────────────────────────────────────────────

class TestURLRaw:
    def test_raw_returns_tuple(self):
        raw = URL("https://example.com/path").raw
        assert isinstance(raw, tuple)
        assert len(raw) == 4

    def test_raw_scheme(self):
        assert URL("https://example.com").raw[0] == b"https"
        assert URL("http://example.com").raw[0] == b"http"

    def test_raw_host(self):
        assert URL("https://example.com").raw[1] == b"example.com"

    def test_raw_host_ipv6(self):
        raw = URL("https://[::1]:8443/path").raw
        assert raw[1] == b"::1"

    def test_raw_port_explicit(self):
        raw = URL("http://example.com:8080/path").raw
        assert raw[2] == 8080

    def test_raw_port_default_http_stripped(self):
        raw = URL("http://example.com:80/path").raw
        assert raw[2] is None

    def test_raw_port_default_https_stripped(self):
        raw = URL("https://example.com:443/path").raw
        assert raw[2] is None

    def test_raw_port_none_when_omitted(self):
        raw = URL("https://example.com/path").raw
        assert raw[2] is None

    def test_raw_path(self):
        raw = URL("https://example.com/path").raw
        assert raw[3] == b"/path"

    def test_raw_path_with_query(self):
        raw = URL("https://example.com/path?q=1").raw
        assert raw[3] == b"/path?q=1"

    def test_raw_path_empty_defaults_slash(self):
        raw = URL("https://example.com").raw
        assert raw[3] == b"/"

    def test_raw_percent_encoded(self):
        raw = URL("https://example.com/path%20with%20spaces").raw
        assert raw[3] == b"/path%20with%20spaces"