Software Testing Engineering / API & CONTRACT TESTING / TST-023
Adversarial API Inputs
Explore concept graphMateri
Masalah: klien mengirim page=-1, limit=1000000, isAdmin=true atau Idempotency-Key yang sama dengan amount berbeda. Bahkan error response yang benar tidak cukup jika database sudah berubah.
Langkah pertama: definisikan input yang sah dan validasi semuanya sebelum memasuki operasi penyimpanan.
HTTP input
|
Query / raw-body constraints
+-- invalid ---------------------> 400 / 413 / 415
|
Typed DTO and allowed fields
+-- invalid ---------------------> 400
|
Idempotency key + fingerprint
+-- same payload / existing key -> replay original 201
+-- different payload / key ----> 409
+-- valid new operation --------> one 201 + one writeFault matrix
| Input | Response | Additional writes |
|---|---|---|
| Negative page, enormous limit, unsupported sort/filter | 400 | 0 |
| Unknown or repeated query parameter | 400 | 0 |
| JSON larger than 1024 bytes | 413 | 0 |
| Wrong media type | 415 | 0 |
| Missing field, malformed JSON, forbidden property | 400 | 0 |
| Existing key with new amount | 409 | 0 |
| Same key and same body | Cached 201 | 0 |
| First valid POST | 201 | 1 |
The 1024-byte cap and the limit of 50 are explicit lab policies, not universal HTTP or OpenAPI defaults. The example is a sequential Node in-memory API; real concurrent idempotency requires durable transactional enforcement.
Konsep 1 / 3
Reject negative pagination, huge limits, repeated keys and unsupported sort/filter before reading data.
Untrusted pagination and sort keys can trigger unbounded work or invalid reads.
Write the allowed query names and page/limit bounds.
Intuisi utama
Validate the entire query value and enforce a fixed allowlist.
This lab requires positive page in 1..10000, limit in 1..50, sort=createdAt, and filter all or currency:IDR/USD; duplicate and unknown parameters fail with stable 400 Problem Details. These bounds are an educational policy, not universal OpenAPI defaults.
Contoh bertahap
GET /transfers?page=-1&limit=1000000 would bypass a weak parseInt-based handler.
- Inspect raw query parameters, including duplicated page.
Mengapa: A multi-value parameter is ambiguous and should fail closed.
Hasil yang diharapkan: Unknown/duplicate values receive HTTP 400.
- Require full-string positive safe integer and bounded limit.
Mengapa: parseInt('2x') would otherwise accept malformed input.
Hasil yang diharapkan: Negative, huge and fractional values receive INVALID_PAGE or INVALID_LIMIT.
Konsep 2 / 3
Reject oversized, malformed or forbidden JSON and wrong content types before any write.
Input mass-assignment and unbounded request bodies can create unintended state effects.
Define the exact allowed field names and body cap.
Intuisi utama
Raw byte cap -> media type -> parse -> DTO allowlist -> mutation.
The Node fixture accepts five CreateTransfer fields and a 1024-byte maximum. It returns 413 for oversized body, 415 for unsupported media, and 400 for malformed JSON, forbidden fields or invalid values. Validation occurs before the in-memory write. It is a reviewed subset, not a complete JSON Schema engine or authorization system.
Contoh bertahap
A transfer POST supplies a valid amount and a forged isAdmin:true property.
- Consume body with a strict 1024-byte cap.
Mengapa: No unbounded JSON allocation is permitted.
Hasil yang diharapkan: Oversized request yields 413 without mutation.
- Reject additional properties and construct a stored record from only five approved fields.
Mengapa: Untrusted privilege properties must never propagate to state.
Hasil yang diharapkan: isAdmin and balanceAdjustment yield 400 FORBIDDEN_FIELD.
Konsep 3 / 3
Prove same-key exact replay preserves one record and same-key changed payload conflicts.
A repeated POST after a lost response must not duplicate an effect or overwrite an existing operation.
Use one key with identical payload for two POSTs.
Intuisi utama
One validated payload fingerprint per idempotency key; identical replay shares result, conflicting data gets 409.
The fixture uses an 8..64 character alphanumeric/hyphen Idempotency-Key. The first valid POST returns 201 and stores one PENDING transfer. Repeating the same body/key returns the original 201 without a second mutation; altering amount with the same key returns 409. This only proves sequential in-memory behavior, not durable, concurrent, bank-authenticated or settled transfers.
Contoh bertahap
A client repeats its accepted transfer with the original key after losing a response.
- Submit a valid payload and record its 201 transferId.
Mengapa: The first operation creates one state entry.
Hasil yang diharapkan: One transfer and one key record.
- Replay the same key/body, then change amount under that same key.
Mengapa: Identical replay and misuse must be distinguished.
Hasil yang diharapkan: 201 replay with same transferId, then 409 conflict; still one transfer.
Sumber primer
Sumber
Cross-domain connections
Curated relationships for this unit at G1. Proposed edges are suggestions, not prerequisites.
Dashed = proposed relationship; solid = reviewed connection. Only reviewed prerequisites constrain a learning path. Keyboard and text links remain available.