swadhinbiswas 🔧 fix: Test failures in CI · 7/28/2026
ccd5a2e
105 lines · 3.2 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
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
import os
import tempfile

from justapi import JustAPIApp, JustAPITestClient
from justapi.responses import (
    Response,
    RedirectResponse,
    JSONResponse,
    FileResponse,
    HTMLResponse,
)


def test_set_cookie_and_delete_cookie():
    r = Response("hi", media_type="text/plain")
    r.set_cookie("session", "abc123", max_age=3600, httponly=True, samesite="lax")
    cookies = [v for k, v in r.headers if k == b"set-cookie"]
    assert cookies == [b"session=abc123; Max-Age=3600; path=/; httponly; samesite=lax"]

    r.delete_cookie("session")
    expired = [v for k, v in r.headers if k == b"set-cookie" and b"expires=" in v]
    assert len(expired) == 1
    assert b"session=" in expired[0] and b"Max-Age=0" in expired[0]


def test_redirect_response_sets_location():
    r = RedirectResponse("/login", status_code=302)
    assert r.status_code == 302
    loc = [v for k, v in r.headers if k == b"location"]
    assert loc == [b"/login"]


def test_file_response_serves_content():
    with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as tf:
        tf.write(b"hello from disk")
        path = tf.name
    try:
        r = FileResponse(path)
        assert r.status_code == 200
        assert r.body == b"hello from disk"
        ctype = [v for k, v in r.headers if k == b"content-type"]
        assert ctype == [b"text/plain"]
        cdisp = [v for k, v in r.headers if k == b"content-disposition"]
        assert cdisp and b'filename="' in cdisp[0]
    finally:
        os.unlink(path)


def test_response_end_to_end_via_testclient():
    app = JustAPIApp()

    @app.get("/set")
    def set_cookie(req):
        r = JSONResponse({"ok": True})
        r.set_cookie("token", "xyz", httponly=True)
        return r

    @app.get("/go")
    def go(req):
        return RedirectResponse("/set", status_code=307)

    client = JustAPITestClient(app)
    resp = client.get("/set")
    assert resp["status"] == 200
    assert "token=xyz" in resp["headers"].get("set-cookie", "")

    resp2 = client.get("/go")
    assert resp2["status"] == 307
    assert resp2["headers"].get("location") == "/set"


def test_status_key_in_body_not_dropped():
    # BUG-1 (PRODUCTION_PLAN.md P0.3): a response dict that contains a
    # top-level "status" field must NOT be misclassified as a legacy
    # {"status", "body"} envelope. Its body must be returned intact.
    app = JustAPIApp()

    @app.get("/with_status")
    def with_status(req):
        return {"status": "ok", "products": 5}

    client = JustAPITestClient(app)
    resp = client.get("/with_status")
    assert resp["status"] == 200
    # The key point is the "status" field is preserved in the body, not dropped
    # as a legacy envelope. Accept both compact and spaced JSON.
    import json
    body = json.loads(resp["body"])
    assert body == {"status": "ok", "products": 5}


def test_explicit_envelope_still_works():
    # A real envelope (with "body") is still passed through unchanged.
    app = JustAPIApp()

    @app.get("/env")
    def env(req):
        return {"__response__": True, "status": 201, "body": "created"}

    client = JustAPITestClient(app)
    resp = client.get("/env")
    assert resp["status"] == 201
    assert resp["body"] == b"created"