/ justapi / tests / test_multipart.py
swadhinbiswas test: add end-to-end integration test suite with gRPC and proto stubs · 7/9/2026
43e015f
38 lines · 1.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
import asyncio
import httpx
from justapi import JustAPIApp, UploadFile, File, Form

app = JustAPIApp()

@app.post("/upload")
def upload_file(file: UploadFile = File(), username: str = Form()):
    content = file.read()
    file.close()
    return {"filename": file.filename, "content": content.decode("utf-8"), "username": username}

async def main():
    # Start server in background
    import threading
    import time
    
    def run_server():
        app.run("127.0.0.1:8080")
        
    t = threading.Thread(target=run_server, daemon=True)
    t.start()
    time.sleep(1) # wait for server to start
    
    async with httpx.AsyncClient() as client:
        files = {'file': ('test.txt', b'Hello, world!', 'text/plain')}
        data = {'username': 'testuser'}
        response = await client.post('http://localhost:8080/upload', files=files, data=data)
        
        print(response.status_code)
        print(response.json())
        assert response.status_code == 200
        assert response.json() == {"filename": "test.txt", "content": "Hello, world!", "username": "testuser"}
        print("Multipart upload test passed!")

if __name__ == "__main__":
    asyncio.run(main())