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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
"""Tests for the eggfetch Python response compatibility surface (Milestone H)."""

import asyncio
import http.server
import json
import threading
import urllib.parse

import pytest

import eggfetch

from conftest import _ThreadingHTTPServer


# ---------------------------------------------------------------------------
# Extended test server
# ---------------------------------------------------------------------------


class _Handler(http.server.BaseHTTPRequestHandler):
    """Test server that returns various status codes and content types."""

    def do_GET(self):
        parsed = urllib.parse.urlparse(self.path)
        path = parsed.path

        if path == "/json":
            body = json.dumps({"key": "value", "count": 42}).encode()
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/text":
            body = b"Hello, World!"
            self.send_response(200)
            self.send_header("Content-Type", "text/plain")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/html":
            body = b"<html><body>Hello</body></html>"
            self.send_response(200)
            self.send_header("Content-Type", "text/html; charset=utf-8")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/charset-latin1":
            # Send body encoded in latin-1 (iso-8859-1)
            body = "caf\u00e9".encode("latin-1")  # "café" in latin-1
            self.send_response(200)
            self.send_header("Content-Type", "text/plain; charset=iso-8859-1")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/multiline":
            body = b"line1\nline2\nline3\n"
            self.send_response(200)
            self.send_header("Content-Type", "text/plain")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/multi-header":
            body = b"ok"
            self.send_response(200)
            self.send_header("Content-Type", "text/plain")
            self.send_header("X-Thing", "first")
            self.send_header("X-Thing", "second")
            self.send_header("X-Thing", "third")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/404":
            body = b"Not Found"
            self.send_response(404)
            self.send_header("Content-Type", "text/plain")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/500":
            body = b"Internal Server Error"
            self.send_response(500)
            self.send_header("Content-Type", "text/plain")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/301":
            self.send_response(301)
            self.send_header("Location", "/json")
            self.send_header("Content-Length", "0")
            self.end_headers()

        elif path == "/100":
            # HTTP/1.1 100 Continue - hard to simulate with http.server,
            # but we test the property via from_parts
            self.send_response(200)
            self.send_header("Content-Type", "text/plain")
            self.end_headers()

        elif path == "/empty-content-type":
            body = b"no content type"
            self.send_response(200)
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/charset-quoted":
            body = "caf\u00e9".encode("utf-8")
            self.send_response(200)
            self.send_header("Content-Type", "text/plain; charset=\"utf-8\"")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/charset-unknown":
            body = "caf\u00e9".encode("utf-8")
            self.send_response(200)
            self.send_header("Content-Type", "text/plain; charset=x-notreal")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        elif path == "/latin1-bytes-no-charset":
            # Latin-1 encoded bytes, but no charset in Content-Type
            # Decoder should fall back to UTF-8 lossy
            body = "caf\u00e9".encode("latin-1")
            self.send_response(200)
            self.send_header("Content-Type", "text/plain")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

        else:
            body = json.dumps({
                "method": "GET",
                "path": parsed.path,
                "query": parsed.query,
                "headers": dict(self.headers),
            }).encode()
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

    def do_POST(self):
        length = int(self.headers.get("Content-Length", 0))
        raw = self.rfile.read(length) if length else b""
        body = json.dumps({
            "method": "POST",
            "path": self.path,
            "headers": dict(self.headers),
            "body": raw.decode(errors="replace"),
        }).encode()
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def do_PUT(self):
        self.do_POST()

    def do_PATCH(self):
        self.do_POST()

    def do_DELETE(self):
        self.do_GET()

    def do_HEAD(self):
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.send_header("X-Echo", "head-ok")
        self.end_headers()

    def do_OPTIONS(self):
        self.send_response(200)
        self.send_header("Allow", "GET, POST, OPTIONS")
        self.end_headers()

    def log_message(self, format, *args):
        pass  # suppress logs during tests


@pytest.fixture(scope="module")
def server():
    """Start a local HTTP server for the test module."""
    srv = _ThreadingHTTPServer(("127.0.0.1", 0), _Handler)
    port = srv.server_address[1]
    t = threading.Thread(target=srv.serve_forever, daemon=True)
    t.start()
    yield f"http://127.0.0.1:{port}"
    srv.shutdown()


# ---------------------------------------------------------------------------
# Response properties: reason_phrase, http_version, encoding, history
# ---------------------------------------------------------------------------


class TestResponseProperties:
    def test_reason_phrase_ok(self, server):
        r = eggfetch.get(f"{server}/json")
        assert r.reason_phrase == "OK"

    def test_reason_phrase_not_found(self, server):
        r = eggfetch.get(f"{server}/404")
        assert r.reason_phrase == "Not Found"

    def test_reason_phrase_server_error(self, server):
        r = eggfetch.get(f"{server}/500")
        assert r.reason_phrase == "Internal Server Error"

    def test_http_version(self, server):
        r = eggfetch.get(f"{server}/json")
        assert r.http_version in ("HTTP/1.0", "HTTP/1.1", "HTTP/2", "HTTP/3")

    def test_encoding_from_content_type(self, server):
        r = eggfetch.get(f"{server}/html")
        assert r.encoding is not None
        assert r.encoding.lower() == "utf-8"

    def test_encoding_latin1(self, server):
        r = eggfetch.get(f"{server}/charset-latin1")
        assert r.encoding is not None
        assert "iso-8859-1" in r.encoding.lower() or "latin" in r.encoding.lower()

    def test_encoding_none_when_no_charset(self, server):
        r = eggfetch.get(f"{server}/text")
        # text/plain without charset
        assert r.encoding is None

    def test_encoding_none_when_no_content_type(self, server):
        r = eggfetch.get(f"{server}/empty-content-type")
        assert r.encoding is None

    def test_encoding_quoted_charset(self, server):
        r = eggfetch.get(f"{server}/charset-quoted")
        assert r.encoding is not None
        assert r.encoding.lower() == "utf-8"

    def test_encoding_unknown_charset_falls_back(self, server):
        r = eggfetch.get(f"{server}/charset-unknown")
        # Unknown charset is not recognized by encoding_rs,
        # so encoding property is the raw string but text falls back to UTF-8
        assert r.encoding is not None
        assert r.encoding == "x-notreal"
        # Body was valid UTF-8, so it decodes correctly via lossy fallback
        assert "caf" in r.text

    def test_history_empty(self, server):
        r = eggfetch.get(f"{server}/json")
        assert r.history == []

    def test_stream_consumed_false(self, server):
        r = eggfetch.get(f"{server}/json")
        assert r._stream_consumed is False


# ---------------------------------------------------------------------------
# Status helpers
# ---------------------------------------------------------------------------


class TestStatusHelpers:
    def test_is_informational(self, server):
        r = eggfetch.get(f"{server}/json")
        assert r.is_informational is False
        assert r.is_success is True
        assert r.is_redirect is False
        assert r.is_client_error is False
        assert r.is_server_error is False
        assert r.is_error is False

    def test_is_client_error(self, server):
        r = eggfetch.get(f"{server}/404")
        assert r.is_informational is False
        assert r.is_success is False
        assert r.is_redirect is False
        assert r.is_client_error is True
        assert r.is_server_error is False
        assert r.is_error is True

    def test_is_server_error(self, server):
        r = eggfetch.get(f"{server}/500")
        assert r.is_informational is False
        assert r.is_success is False
        assert r.is_redirect is False
        assert r.is_client_error is False
        assert r.is_server_error is True
        assert r.is_error is True

    def test_is_redirect(self, server):
        # Our server returns 301 without redirect following
        r = eggfetch.get(f"{server}/301")
        assert r.is_redirect is True
        assert r.is_error is False


# ---------------------------------------------------------------------------
# json() method
# ---------------------------------------------------------------------------


class TestJsonMethod:
    def test_json_returns_dict(self, server):
        r = eggfetch.get(f"{server}/json")
        data = r.json()
        assert isinstance(data, dict)
        assert data["key"] == "value"
        assert data["count"] == 42

    def test_json_is_cached(self, server):
        r = eggfetch.get(f"{server}/json")
        d1 = r.json()
        d2 = r.json()
        assert d1 == d2

    def test_json_with_kwargs(self, server):
        r = eggfetch.get(f"{server}/json")
        # parse_float and parse_int are valid json.loads kwargs
        data = r.json(parse_int=str)
        assert data["count"] == "42"


# ---------------------------------------------------------------------------
# Text decoding
# ---------------------------------------------------------------------------


class TestTextDecoding:
    def test_utf8_text(self, server):
        r = eggfetch.get(f"{server}/text")
        assert r.text == "Hello, World!"

    def test_latin1_decoded_as_latin1(self, server):
        r = eggfetch.get(f"{server}/charset-latin1")
        # The server sends "café" encoded in latin-1 with charset=iso-8859-1
        # Our decoder should decode it properly
        assert "caf" in r.text

    def test_latin1_bytes_without_charset_use_utf8_lossy(self, server):
        # Latin-1 bytes without charset → UTF-8 lossy decode produces
        # replacement characters for the non-ASCII bytes
        r = eggfetch.get(f"{server}/latin1-bytes-no-charset")
        assert r.encoding is None
        # The latin-1 byte 0xe9 is not valid UTF-8, so lossy decode
        # replaces it with U+FFFD
        assert "\ufffd" in r.text

    def test_html_text(self, server):
        r = eggfetch.get(f"{server}/html")
        assert "<html>" in r.text


# ---------------------------------------------------------------------------
# iter_bytes, iter_text, iter_lines
# ---------------------------------------------------------------------------


class TestIterators:
    def test_iter_bytes(self, server):
        r = eggfetch.get(f"{server}/text")
        chunks = list(r.iter_bytes())
        assert len(chunks) > 0
        combined = b"".join(chunks)
        assert combined == b"Hello, World!"

    def test_iter_bytes_custom_chunk_size(self, server):
        r = eggfetch.get(f"{server}/text")
        chunks = list(r.iter_bytes(chunk_size=5))
        assert len(chunks) > 1
        combined = b"".join(chunks)
        assert combined == b"Hello, World!"

    def test_iter_text(self, server):
        r = eggfetch.get(f"{server}/text")
        chunks = list(r.iter_text())
        assert len(chunks) > 0
        combined = "".join(chunks)
        assert combined == "Hello, World!"

    def test_iter_text_custom_chunk_size(self, server):
        r = eggfetch.get(f"{server}/text")
        chunks = list(r.iter_text(chunk_size=5))
        assert len(chunks) > 1
        combined = "".join(chunks)
        assert combined == "Hello, World!"

    def test_iter_lines(self, server):
        r = eggfetch.get(f"{server}/multiline")
        lines = list(r.iter_lines())
        assert lines == ["line1", "line2", "line3"]

    def test_iter_lines_single_line(self, server):
        r = eggfetch.get(f"{server}/text")
        lines = list(r.iter_lines())
        assert lines == ["Hello, World!"]

    def test_iter_bytes_returns_iterator(self, server):
        r = eggfetch.get(f"{server}/text")
        it = r.iter_bytes()
        # Should be an iterator (has __iter__)
        assert hasattr(it, "__iter__")


# ---------------------------------------------------------------------------
# close / aclose
# ---------------------------------------------------------------------------


class TestClose:
    def test_close_is_noop(self, server):
        r = eggfetch.get(f"{server}/json")
        r.close()  # should not raise

    def test_aclose_is_noop(self, server):
        r = eggfetch.get(f"{server}/json")
        # aclose() returns an awaitable (consistent with the streaming
        # response surface); awaiting it must not raise.
        asyncio.run(r.aclose())


# ---------------------------------------------------------------------------
# __repr__
# ---------------------------------------------------------------------------


class TestRepr:
    def test_repr_200(self, server):
        r = eggfetch.get(f"{server}/json")
        assert repr(r) == "<Response [200 OK]>"

    def test_repr_404(self, server):
        r = eggfetch.get(f"{server}/404")
        assert repr(r) == "<Response [404 Not Found]>"

    def test_repr_500(self, server):
        r = eggfetch.get(f"{server}/500")
        assert repr(r) == "<Response [500 Internal Server Error]>"


# ---------------------------------------------------------------------------
# raise_for_status
# ---------------------------------------------------------------------------


class TestRaiseForStatus:
    def test_no_raise_on_200(self, server):
        r = eggfetch.get(f"{server}/json")
        r.raise_for_status()  # should not raise

    def test_raises_on_404(self, server):
        r = eggfetch.get(f"{server}/404")
        with pytest.raises(eggfetch.HTTPStatusError, match="404"):
            r.raise_for_status()

    def test_raises_on_500(self, server):
        r = eggfetch.get(f"{server}/500")
        with pytest.raises(eggfetch.HTTPStatusError, match="500"):
            r.raise_for_status()

    def test_error_message_includes_reason(self, server):
        r = eggfetch.get(f"{server}/404")
        with pytest.raises(eggfetch.HTTPStatusError, match="Not Found"):
            r.raise_for_status()

    def test_error_message_includes_url(self, server):
        r = eggfetch.get(f"{server}/404")
        with pytest.raises(eggfetch.HTTPStatusError, match=f"{server}/404"):
            r.raise_for_status()


# ---------------------------------------------------------------------------
# Headers.get_list
# ---------------------------------------------------------------------------


class TestHeadersGetList:
    def test_get_list_single_value(self, server):
        r = eggfetch.get(f"{server}/text")
        vals = r.headers.get_list("content-type")
        assert isinstance(vals, list)
        assert len(vals) == 1
        assert "text/plain" in vals[0]

    def test_get_list_multiple_values(self, server):
        r = eggfetch.get(f"{server}/multi-header")
        vals = r.headers.get_list("x-thing")
        assert vals == ["first", "second", "third"]

    def test_get_list_missing_header(self, server):
        r = eggfetch.get(f"{server}/text")
        vals = r.headers.get_list("x-not-present")
        assert vals == []

    def test_get_list_invalid_name(self, server):
        r = eggfetch.get(f"{server}/text")
        with pytest.raises(ValueError, match="Invalid header name"):
            r.headers.get_list("invalid header name!")


# ---------------------------------------------------------------------------
# Async response properties
# ---------------------------------------------------------------------------


class TestAsyncResponseProperties:
    def test_async_reason_phrase(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/json")
                assert r.reason_phrase == "OK"
        asyncio.run(_test())

    def test_async_http_version(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/json")
                assert r.http_version in ("HTTP/1.0", "HTTP/1.1", "HTTP/2", "HTTP/3")
        asyncio.run(_test())

    def test_async_status_helpers(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/404")
                assert r.is_client_error is True
                assert r.is_error is True
                assert r.is_success is False
        asyncio.run(_test())

    def test_async_json(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/json")
                data = r.json()
                assert data["key"] == "value"
        asyncio.run(_test())

    def test_async_repr(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/json")
                assert repr(r) == "<Response [200 OK]>"
        asyncio.run(_test())

    def test_async_encoding(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/html")
                assert r.encoding is not None
        asyncio.run(_test())

    def test_async_iter_lines(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/multiline")
                lines = list(r.iter_lines())
                assert lines == ["line1", "line2", "line3"]
        asyncio.run(_test())

    def test_async_headers_get_list(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/multi-header")
                vals = r.headers.get_list("x-thing")
                assert vals == ["first", "second", "third"]
        asyncio.run(_test())

    def test_async_raise_for_status(self, server):
        async def _test():
            async with eggfetch.AsyncClient() as client:
                r = await client.get(f"{server}/404")
                with pytest.raises(eggfetch.HTTPStatusError, match="404"):
                    r.raise_for_status()
        asyncio.run(_test())