Supertest · ตรวจ Response Contract
Response Headers
ตรวจ header ที่เป็นส่วนหนึ่งของ HTTP contract
Response headers เป็น contract ได้
บาง behavior ไม่ได้อยู่ใน body เช่น `Location` หลัง create, `Cache-Control`, request id หรือ `Set-Cookie` การตรวจ header ช่วยกัน regression ที่ client ใช้งานอยู่แต่ body assertion มองไม่เห็น
เมื่อ string มีค่าที่ runtime สร้าง ให้ประกอบ expected จาก response field ที่เป็น public contract
header name และ value อาจ normalize
Node response headers มักเข้าถึงด้วยชื่อ lowercase เช่น `response.headers['content-type']` อย่าเขียน assertion ที่ขึ้นกับตัวพิมพ์ของ header name แต่ให้ตรวจ value ตาม semantics เช่นใช้ `toContain` กับ media type ที่มี charset ต่อท้ายได้
กฎสำคัญ
ตรวจเฉพาะ header ที่มีความหมาย
ไม่ต้อง assert header ทุกตัวที่ server ส่ง เพราะบางตัวเป็นรายละเอียดของ adapter และเปลี่ยนได้เมื่อ refactor
Location ต้องตรง resource ใหม่
ถ้า API contract กำหนดให้คืน Location หลัง create ให้ตรวจ path ที่ชี้ไปยัง id ที่สร้างจริง