/ justapi / tests / test_responses.py
swadhinbiswas test: add end-to-end integration test suite with gRPC and proto stubs · 7/9/2026
43e015f
50 lines · 1.4 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
import threading
import time
import urllib.request
from justapi import JustAPIApp, JSONResponse, HTMLResponse, RedirectResponse, PlainTextResponse

app = JustAPIApp()

@app.get("/json")
def get_json():
    return JSONResponse({"hello": "world"}, status_code=201)

@app.get("/html")
def get_html():
    return HTMLResponse("<h1>Hello</h1>")

@app.get("/redirect")
def get_redirect():
    return RedirectResponse("/html", status_code=302)

@app.get("/plain")
def get_plain():
    return PlainTextResponse("Hello, world!")

def run_server():
    app.run("127.0.0.1:8082")

if __name__ == "__main__":
    t = threading.Thread(target=run_server, daemon=True)
    t.start()
    time.sleep(1)

    # JSON
    req = urllib.request.urlopen("http://127.0.0.1:8082/json")
    print("JSON:", req.read().decode(), "Status:", req.status, "Headers:", req.headers.get("content-type"))
    
    # HTML
    req = urllib.request.urlopen("http://127.0.0.1:8082/html")
    print("HTML:", req.read().decode(), "Status:", req.status, "Headers:", req.headers.get("content-type"))
    
    # Plain
    req = urllib.request.urlopen("http://127.0.0.1:8082/plain")
    print("Plain:", req.read().decode(), "Status:", req.status, "Headers:", req.headers.get("content-type"))
    
    # Redirect
    try:
        req = urllib.request.urlopen("http://127.0.0.1:8082/redirect")
        print("Redirect reached:", req.read().decode(), req.status)
    except Exception as e:
        print("Redirect error:", e)