Software Testing Engineering / API & CONTRACT TESTING / TST-019

Suggested study path · Step 4 of 6 · G1

REST Contract Matrix

Explore concept graph

Materi

Masalah: OpenAPI SYS-008 tampak benar di atas kertas. Bagaimana memastikan server benar-benar mengirim status, header, JSON schema dan error yang dijanjikan?

Langkah pertama: lihat tiga operation response maps di contracts/SYS-008/transfer-api.openapi.json. Jangan menciptakan expected response baru hanya karena HTTP RFC mendefinisikannya.

Peta konsep statis. Ikuti alur teks dari atas ke bawah.
SYS-008 OpenAPI
      |
  exact response matrix
      |
  HTTP server 127.0.0.1:ephemeral
      |
   fetch: 10 real requests
      |
   status + headers + JSON schema
      |
  validate RFC 9457 Problem
      |
  reject 3 broken responses

422 RFC valid -> NOT declared in SYS-008

Declared response matrix

Method/path Success Declared errors
POST /transfers 201 plus Location 400, 401, 403, 409, 500
GET /transfers/{id} 200 401, 403, 404, 500
GET /accounts/{id} 200 401, 403, 404, 500

422 Unprocessable Content: disebut dalam kriteria kurikulum dan RFC 9110, tetapi belum ada di SYS-008 OpenAPI. Status ini adalah contract gap, bukan bukti pengujian lulus. Perlu revisi kontrak dan implementasi yang disengaja jika 422 akan didukung.

Konsep 1 / 3

Compare the required workbook status set to the exact responses promised by SYS-008 OpenAPI.

An API test based on a guessed code is not a check of the public contract.

Mulai dari sini

List the exact response keys of POST /transfers from SYS-008.

Intuisi utama

The response matrix belongs to each method/path in OpenAPI, not to an arbitrary global list of HTTP status codes.

SYS-008 promises POST transfer 201/400/401/403/409/500, GET transfer and account 200/401/403/404/500. RFC 9110 defines 422 Unprocessable Content but this API does not declare it. A TST-019 test must explicitly report the mismatch, not fabricate a 422 success or silently change 400. 201 also requires Location while 401 requires WWW-Authenticate.

Contoh bertahap

The workbook requests a 422 test case, but the POST /transfers response map has no 422.

  1. Read the three SYS-008 operations and enumerate response keys.

    Mengapa: Only the checked-in design specifies observable promises.

    Hasil yang diharapkan: Exactly three operation matrices, including GET 404.

  2. Check POST 422 against the declared response keys.

    Mengapa: An RFC-defined status is not automatically supported by this particular API.

    Hasil yang diharapkan: Report 422 as undeclared rather than a passing test.

  3. Add Location and WWW-Authenticate to the matrix.

    Mengapa: A correct HTTP status can still have an invalid header contract.

    Hasil yang diharapkan: 201/401 required response headers are explicit.

Konsep 2 / 3

Run ten real HTTP requests to a local server and verify positive and negative responses.

Static specification checks cannot prove what an HTTP server sends on the wire.

Mulai dari sini

Execute the Node loopback fixture and inspect the response summary.

Intuisi utama

Arrange isolated server -> Act network fetch -> Assert status, headers and schema -> Release socket.

The fixture binds to 127.0.0.1 on a random port, handles POST/GET transfers/accounts with in-memory resources, and uses fixed bearer strings only for demonstration. It performs 10 live HTTP requests covering statuses 200/201/400/401/403/404/409/500. The 201 response provides a resource URI and PENDING body, not completed financial settlement.

Contoh bertahap

A new transfer is created, read again, and its account is retrieved through real HTTP.

  1. Run node labs/TST-019/http-contract.mjs from the repository root.

    Mengapa: This starts an actual ephemeral HTTP server.

    Hasil yang diharapkan: The program records ten successful round-trip checks.

  2. Check POST Location points at the new transfer resource.

    Mengapa: 201 Created must identify the created representation.

    Hasil yang diharapkan: Location equals /transfers/tr-001 and body.transferId matches.

  3. Request missing credentials, forbidden and absent resource cases.

    Mengapa: Negative paths must produce stable machine-readable outcomes.

    Hasil yang diharapkan: 401 challenge, 403 denial and 404 missing resource with Problem Details.

Konsep 3 / 3

Reject invalid Problem Details, missing 401 challenge and a breaking amount type mutation.

A test that passes a broken response cannot protect client contracts.

Mulai dari sini

Compare the HTTP status and the problem body status from a negative response.

Intuisi utama

The oracle validates independently of the handler: real HTTP status, media type, schema shape and error member status must agree.

A selected OpenAPI schema subset validates required fields, string/integer types, patterns, enums and additionalProperties. Three deliberate mutations are rejected: HTTP 409 with body status 500, 401 without WWW-Authenticate, and JSON amount as a number instead of decimal string. The checker is not full JSON Schema evaluation or real consumer compatibility testing.

Contoh bertahap

The HTTP response is 409 Conflict but the problem body claims status 500.

  1. Run the known-good ten-request matrix first.

    Mengapa: A baseline distinguishes a bad handler from a broken test.

    Hasil yang diharapkan: All declared cases pass.

  2. Inject status mismatch into a synthetic RFC 9457 body.

    Mengapa: Problem status must match actual HTTP status.

    Hasil yang diharapkan: The assertion rejects it.

  3. Drop the 401 auth challenge and retype the 201 amount field.

    Mengapa: Headers and JSON primitive types are public contracts.

    Hasil yang diharapkan: Both independent mutations fail.

Primary references

Sumber

Cross-domain connections

Curated relationships for this unit at G1. Proposed edges are suggestions, not prerequisites.

Concept connectionsG1 · 4 visible concepts · 4 curated links
100%

Dashed = proposed relationship; solid = reviewed connection. Only reviewed prerequisites constrain a learning path. Keyboard and text links remain available.