Supertest · ตรวจ Response Contract
Response Content-Type
ยืนยันว่า response อยู่ในรูปแบบที่ client คาดหวัง
ยืนยันรูปแบบ response ที่ client คาดหวัง
`Content-Type` ของ response บอก client ว่าควร parse body อย่างไร Todo API ที่คืน JSON ควรมี media type เป็น `application/json` และอาจมี charset ต่อท้าย จึงควรตรวจด้วย matcher ที่ไม่ fragile
ตรวจ JSON responseTS
ใช้ regex ที่ยอมรับ parameter ต่อท้าย media type
request content type กับ response content type คนละทิศทาง
| ทิศทาง | ตัวอย่าง | คำถามที่ test ตอบ |
|---|---|---|
| request | .set('Content-Type', 'application/json') | server parse input ถูกหรือไม่ |
| response | response.headers['content-type'] | client รู้วิธีอ่าน output หรือไม่ |
i
อย่าใช้ status แทน content type
response 200 อาจมี body format ผิดได้ การตรวจสองมิตินี้จึงเป็นคนละ assertion