teluzia.Cloud Engineer · Day 69
CHAPTER 10 · SERVERLESS AND MESSAGING

Serverless REST API integration and testing

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Integration testing is rehearsing a complete restaurant order: the waiter, kitchen, receipt, and failure response must agree on the same order number. A serverless REST path includes the public method and route, authorization, integration mapping, handler, and persistence. Unit tests isolate code; integration tests exercise boundaries between services. Define requests and observable responses before building, and include malformed input, missing authorization, duplicate requests, and dependency failures in the acceptance contract. A request ID traces one HTTP request; a business ID traces an order across retries and asynchronous processing. An API that queues work should not claim processing has already completed. A 202 response can provide a status URL, while a synchronous created resource often uses 201 when the contract warrants it.

Think of it this way

Integration testing is rehearsing a complete restaurant order: the waiter, kitchen, receipt, and failure response must agree on the same order number.

TOPIC 01 / 03Step by step

End-to-end contract testing

A serverless REST path includes the public method and route, authorization, integration mapping, handler, and persistence. Unit tests isolate code; integration tests exercise boundaries between services. Define requests and observable responses before building, and include malformed input, missing authorization, duplicate requests, and dependency failures in the acceptance contract. For example, pOST /orders with valid JSON returns a created order ID; the same idempotency key returns the existing order rather than a second record.

WORKED EXAMPLE
POST /orders with valid JSON returns a created order ID; the same idempotency key returns the existing order rather than a second record.

Begin with a client contract: POST /registrations receives JSON, requires a valid credential, and returns either a registration identifier or a documented error. A handler unit test can prove input validation, but it does not prove the gateway forwards the body correctly or applies the intended authorizer. An integration test sends the request through the deployed endpoint and observes both the HTTP result and durable outcome.

A unit test isolates a small component. An integration test checks communicating components. A contract describes observable request and response behavior. Separate test failures by layer: an unauthorized request should fail before creating data; malformed input should produce the chosen client error; a valid authorized request should create exactly the intended record. Deployment-stage configuration and payload versions can differ from local assumptions. Integration tests offer stronger boundary evidence but require controlled setup, scoped data, credentials, and cleanup. They should not use production customer records merely because the endpoint already exists.

Why this matters

Boundary tests catch mismatches between individually correct components, including event shapes and response formats that local handler unit tests cannot reveal by themselves.

A common trap

Calling only the function directly and declaring the public API tested skips authorizers, deployment stages, gateway mapping, and client-visible error status behavior.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

End-to-end contract testing

A serverless REST path includes the public method and route, authorization, integration mapping, handler, and persistence. Unit tests isolate code; integration tests exercise boundaries between services. Define requests and observable responses before building, and include malformed input, missing authorization, duplicate requests, and dependency failures in the acceptance contract. For example, pOST /orders with valid JSON returns a created order ID; the same idempotency key returns the existing order rather than a second record.

WORKED EXAMPLE
POST /orders with valid JSON returns a created order ID; the same idempotency key returns the existing order rather than a second record.

Begin with a client contract: POST /registrations receives JSON, requires a valid credential, and returns either a registration identifier or a documented error. A handler unit test can prove input validation, but it does not prove the gateway forwards the body correctly or applies the intended authorizer. An integration test sends the request through the deployed endpoint and observes both the HTTP result and durable outcome.

A unit test isolates a small component. An integration test checks communicating components. A contract describes observable request and response behavior. Separate test failures by layer: an unauthorized request should fail before creating data; malformed input should produce the chosen client error; a valid authorized request should create exactly the intended record. Deployment-stage configuration and payload versions can differ from local assumptions. Integration tests offer stronger boundary evidence but require controlled setup, scoped data, credentials, and cleanup. They should not use production customer records merely because the endpoint already exists.

Why this matters

Boundary tests catch mismatches between individually correct components, including event shapes and response formats that local handler unit tests cannot reveal by themselves.

A common trap

Calling only the function directly and declaring the public API tested skips authorizers, deployment stages, gateway mapping, and client-visible error status behavior.

Correlation and asynchronous acceptance

A request ID traces one HTTP request; a business ID traces an order across retries and asynchronous processing. An API that queues work should not claim processing has already completed. A 202 response can provide a status URL, while a synchronous created resource often uses 201 when the contract warrants it. For example, the API accepts job j-22 with 202 and a status endpoint; later worker logs use j-22 even though their invocation IDs differ.

WORKED EXAMPLE
The API accepts job j-22 with 202 and a status endpoint; later worker logs use j-22 even though their invocation IDs differ.

An API accepts a video-processing request and assigns job v-91. Its HTTP attempt has request ID r-8, but the queue message and worker later have different transport identifiers. The job ID must travel through the whole business process so the client can query status and operators can connect related evidence. The response should communicate acceptance rather than falsely announce a completed video.

A correlation identifier links related observations. A business identifier names the durable operation. An invocation identifier distinguishes one execution attempt. Use each for its purpose instead of overloading one field. The status endpoint can return queued, processing, completed, or failed according to a defined state model. Transitions require durable writes and duplicate-safe handling. Polling clients should use a bounded or backoff strategy. Returning 202 accurately represents incomplete asynchronous work, but the application must then provide a useful way to discover completion or failure; acceptance without status or notification leaves clients uncertain indefinitely.

Why this matters

Distinct request and business identifiers connect gateway errors, queued work, and durable records without forcing operators to infer relationships from timestamps alone during incidents.

A common trap

Returning 201 for a merely enqueued task can mislead clients into assuming downstream processing succeeded when the worker has not started or has failed.

Test isolation and teardown

Test resources and records should be distinguishable from production. Use a dedicated account or environment, unique test identifiers, least privilege, and explicit cleanup. Asynchronous tests need bounded polling because completion is eventual. Capture enough diagnostic evidence without retaining real secrets or customer payloads in fixtures and screenshots. For example, poll /jobs/j-22 for up to 30 seconds at a reasonable interval, then fail with request IDs and observed states rather than wait forever.

WORKED EXAMPLE
Poll /jobs/j-22 for up to 30 seconds at a reasonable interval, then fail with request IDs and observed states rather than wait forever.

A test suite creates records whose IDs begin with a unique run prefix. After requesting asynchronous processing, it polls one status record until success, explicit failure, or a thirty-second deadline. On timeout it preserves the request ID, last observed state, and relevant diagnostic metadata. It deletes only records owned by that test run, not everything in the table.

Isolation limits interference with other users and tests. A fixture is controlled setup data. Teardown removes or resets that setup. Eventual processing means a single immediate read can legitimately see incomplete work, so integration assertions must distinguish waiting from failure. A fixed sleep is easy but often either wastes time or remains too short; bounded polling explains progress better. Cleanup must also run when assertions fail, while preserving enough sanitized evidence to debug. Tests that depend on real cloud services provide useful realism, but need a dedicated environment and explicit resource budget to avoid turning repeatability into uncontrolled charges.

Why this matters

Isolation and bounded waiting keep repeatable tests from damaging live data, generating unnecessary charges, or hanging pipelines without explaining which dependency failed to complete.

A common trap

Deleting every table item during test cleanup can erase unrelated data; remove only records marked by the current test’s explicit identifier and ownership boundary.

KEEP THESE WITH YOU

Today’s takeaways

End-to-end contract testing: A serverless REST path includes the public method and route, authorization, integration mapping, handler, and persistence. Unit tests isolate code; integration tests exercise boundaries between services. Define requests and observable responses before building, and include malformed input, missing authorization, duplicate requests, and dependency failures in the acceptance contract.

Correlation and asynchronous acceptance: A request ID traces one HTTP request; a business ID traces an order across retries and asynchronous processing. An API that queues work should not claim processing has already completed. A 202 response can provide a status URL, while a synchronous created resource often uses 201 when the contract warrants it.

Test isolation and teardown: Test resources and records should be distinguishable from production. Use a dedicated account or environment, unique test identifiers, least privilege, and explicit cleanup. Asynchronous tests need bounded polling because completion is eventual. Capture enough diagnostic evidence without retaining real secrets or customer payloads in fixtures and screenshots.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed