DataCareerHub
Explore learning
← Core software skillsFree companion · 40 minute suggested activity

7. Test HTTP failures and preserve the contract

Core software skills · Use status codes and negative tests to distinguish malformed data from invalid records.

Learn → Try → ExplainReviewed 2026-10-15
01

Objective & prerequisites

Use status codes and negative tests to distinguish malformed data from invalid records. Before starting: basic Python functions, dictionaries, loops, JSON and terminal use. Complete the earlier Git/API/testing workshops if these are unfamiliar. Download and extract the kit into a new practice folder, then open a terminal in the software-kit folder. Python 3.10+ is required; the reference uses only the standard library. Work with the supplied fictional data. These free companion lessons do not change protected course access or award progress or certificates.

python3 --version
python3 -m unittest discover -s tests -v
02

Understand the concept

Failure behavior is part of an API contract. This lab rejects a missing Content-Length with 411, invalid length or unsupported transfer encoding with 400, more than 16384 body bytes with 413, unsupported media type with 415, malformed UTF-8/JSON with 400 and valid JSON that violates the order schema with 422. JSON objects with repeated keys are ambiguous, so strict_json rejects them rather than accepting the last value. Unexpected fields, boolean money, duplicate business IDs and more than 100 rows are contract errors. The server refuses browser Origin headers and sends no CORS permission; this is a teaching restriction, not authentication. Never describe it as protection for a private production API.

03

Worked example

A payload containing orders:[{}] is syntactically valid JSON but lacks required record fields, so the response is 422. A single opening brace is malformed JSON and returns 400. Sending text/plain returns 415 before record processing. The error response contains a stable generic category without echoing source records. The connection uses HTTP/1.0 and is closed after the response, avoiding reuse after a rejected body. Tests check both successful calculation and independent boundary failures.

python3 -m unittest discover -s tests -p test_http.py -v
04

Try it yourself

Run test_http.py through unittest discovery. Add a new test sending True as amount_cents and expect 422. Add a duplicated JSON key and expect 400. Write a decision table that maps observation, status and caller action. Do not automatically retry malformed or invalid inputs. Write your expected outcome before running your implementation. Keep the supplied solution unchanged so that you can compare your own reasoning with it.

Worked solution

Use HTTPTests.post with the two payloads. True parses as a boolean, then fails exact integer validation; duplicate object keys fail strict parsing earlier. A caller should repair its request for 400/411/413/415/422, not retry blindly. A timeout can have several causes; this stateless calculation can be repeated with the same input, but that does not establish retry safety for writes such as payments.

06

Knowledge check

Is every JSON response with a 4xx status caused by a network outage?

Check and explain

No. The transport delivered an application response. Inspect the stable category and contract rather than treating a rejected input as a connectivity failure.

08

Evidence & common mistakes

Record the exact command, Python version, observed output and one rejected input. Explain what each check establishes and what it leaves untested. Do not remove validation merely to obtain a passing result. A local passing suite is evidence for these fixtures, not proof of production readiness. Credit the teaching reference and distinguish your own changes. The kit includes an exercise worksheet, a separate worked-solutions document, an API contract, expected results and a capstone rubric.

09

Authoritative reference

Read the official reference ↗

— official reference for the underlying interface. Lesson prose and fictional order examples are original. The teaching HTTP service is loopback-only, stateless and not suitable for production or uploading as a running Aruba service. No cloud account, paid API or registration is required.

Make the reasoning yours.

Try the activity before reading its reference. Keep your sources, commands and assumptions with your work. Companion activities are unsaved self-practice and do not award a certificate or change lesson progress.

Resume guidance and examples are for learning and preparation only. They do not guarantee interviews, a job offer or employment.

Public registration, payments and new paid enrolment remain disabled. These free resources do not grant access to protected course modules.

Learner dashboard ↗ · Ask a question ↗