Supertest · ส่งข้อมูลใน Request
Content-Type
เชื่อม body format กับ Content-Type ที่ API คาดหวัง
Content-Type บอก format ของ body
`Content-Type` บอก server ว่า request body ถูกส่งมาในรูปแบบใด JSON, form และ multipart ใช้ parser คนละแบบ การทดสอบ body จึงต้องตรงกับ media type ที่ endpoint คาดหวัง
JSON content typeTS
โดยปกติ `.send(object)` จะช่วยเลือก JSON format แต่การเขียน header ให้เห็นชัดเหมาะกับการสอน contract
จับคู่ body method และ content type
| รูปแบบ | Supertest chain | ตัวอย่าง endpoint |
|---|---|---|
| JSON | .send(object) | POST /todos |
| form urlencoded | .type('form').send(object) | POST /auth/login แบบ form |
| multipart | .field().attach() | POST /files/avatar |
กฎสำคัญ
fidelity
อย่าเปลี่ยน format เพื่อให้ test ง่าย
ถ้า production client ส่ง multipart แต่ test ส่ง JSON เราอาจพลาด parser, interceptor หรือ file handling regression
direction
assert response format ด้วย
request content type กับ response content type เป็นคนละทิศทาง ตรวจตามสิ่งที่แต่ละ contract สัญญาไว้