API Testing
ทดสอบ API ตรง ๆ ด้วย Swagger / curl / Postman
Swagger UI (ง่ายสุด — แนะนำสำหรับทดสอบ)
http://localhost:3000/docs
มีทุก endpoint พร้อม:
- ดู request schema + response schema
- กด “Try it out” ลองยิงได้เลยใน browser
- Authenticate ได้ — กด “Authorize” บนสุดแล้ว paste token
curl examples — ลองในเทอร์มินัล
1. Login
curl -X POST http://localhost:3000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "customer@example.com",
"password": "Customer1234!"
}'
Response:
{
"accessToken": "eyJhbG...",
"refreshToken": "eyJhbG...",
"user": { "id": 2, "email": "customer@example.com", ... }
}
เก็บ accessToken ไว้ใช้ต่อ:
export TOKEN="eyJhbG..."
2. ดูสินค้าทั้งหมด
curl http://localhost:3000/api/v1/products?pageSize=5
3. ดูสินค้าตาม slug
curl http://localhost:3000/api/v1/products/by-slug/polo-shirt
4. ดู profile ตัวเอง
curl http://localhost:3000/api/v1/users/me \
-H "Authorization: Bearer $TOKEN"
5. เพิ่มลงตะกร้า
curl -X POST http://localhost:3000/api/v1/cart/items \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{ "productId": 1, "quantity": 2 }'
6. ดูตะกร้า
curl http://localhost:3000/api/v1/cart \
-H "Authorization: Bearer $TOKEN"
7. Preview checkout
curl -X POST http://localhost:3000/api/v1/checkout/preview \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{ "addressId": 1, "paymentMethod": "bank_transfer" }'
8. Place order
curl -X POST http://localhost:3000/api/v1/checkout/place-order \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"addressId": 1,
"paymentMethod": "bank_transfer",
"customerNote": "test"
}'
9. Upload slip
curl -X POST http://localhost:3000/api/v1/payments/<orderId>/upload-slip \
-H "Authorization: Bearer $TOKEN" \
-F "file=@/path/to/slip.jpg"
10. ดูออเดอร์
# all orders ของ user
curl http://localhost:3000/api/v1/orders \
-H "Authorization: Bearer $TOKEN"
# detail
curl http://localhost:3000/api/v1/orders/<orderId> \
-H "Authorization: Bearer $TOKEN"
ทดสอบ admin endpoints
Login admin
curl -X POST http://localhost:3000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{ "email": "admin@lumora.shop", "password": "Admin1234!" }'
เก็บ token:
export ADMIN_TOKEN="..."
ยืนยันสลิป
curl -X POST http://localhost:3000/api/v1/payments/<paymentId>/verify \
-H "Authorization: Bearer $ADMIN_TOKEN"
ปฏิเสธสลิป
curl -X POST http://localhost:3000/api/v1/payments/<paymentId>/reject \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{ "reason": "ยอดเงินไม่ตรง" }'
เปลี่ยนสถานะออเดอร์
curl -X PATCH http://localhost:3000/api/v1/orders/<orderId>/status \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{ "status": "preparing" }'
ดูรายงานยอดขาย
curl "http://localhost:3000/api/v1/reports/sales?from=2026-01-01&to=2026-01-31" \
-H "Authorization: Bearer $ADMIN_TOKEN"
Postman / Insomnia
Import collection
ดาวน์โหลด OpenAPI spec:
curl http://localhost:3000/docs-json > lumora-api.json
แล้ว import ใน Postman → “Import” → file lumora-api.json
หรือใน Insomnia → “Import / Export” → “Import from File”
Load test
# ติดตั้ง autocannon
npm install -g autocannon
# ทดสอบ products list
autocannon -d 30 -c 50 http://localhost:3000/api/v1/products
ค่าเป้าหมาย dev (laptop):
- ✓ p95 < 200ms
- ✓ p99 < 500ms
- ✓ Error rate = 0%
- ✓ ≥ 500 req/s
Production VPS ควรเร็วกว่า 2-3 เท่า
Rate limit testing
ทดสอบว่า limit ทำงาน:
for i in {1..10}; do
curl -X POST http://localhost:3000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"wrong@x.com","password":"x"}'
echo
done
หลังครั้งที่ 5-6 ควรเห็น 429 Too Many Requests
ขั้นต่อไป
- ดู feature checklist → 06 — Checklist
- ถ้าซื้อแล้ว → คู่มือเจ้าของระบบ
