KatInterview

Build a Plain-Text Notes REST API and Black-Box Test Plan

Data Structure DesignModel answer
MediumModel answerData Structure Designrest-apihttp-status-codesunit-testing

Last updated

Design a small CRUD HTTP service that accepts raw text bodies but replies in JSON, then explain how to test an unseen function from its specification alone.

This exercise has two parts.

Part 1: A plain-text CRUD service

Build a small HTTP service that manages a collection of short text entries (call them notes). Clients send raw text when writing, but every response from the server must be JSON.

Expose these routes:

  • POST /items with Content-Type: text/plain creates a new note from the request body.
  • GET /items/{id} returns the note with that id.
  • PUT /items/{id} with Content-Type: text/plain replaces the note's text.
  • DELETE /items/{id} removes the note.

Example response shapes:

  • Success: { "message": "created", "id": 1 }
  • Failure: { "message": "item not found" }

Your service should:

  • Choose sensible HTTP status codes for each outcome.
  • Handle missing notes, invalid input, and unsupported media types.
  • Return JSON even for errors that the web framework would normally produce itself (unknown route, wrong method, oversized body).

Part 2: Testing without the source

You are given only the written specification of a function, not its implementation. Describe which behaviors you would test and how you would organize the tests so they give high confidence in correctness.

What to deliver

  • A short description of the API contract (routes, status codes, validation order).
  • Working code for the service in a language and framework of your choice.
  • A systematic test strategy for Part 2, optionally applied to your own API.

Hints

Hint 1

Decide a fixed order for validating write requests: check the media type first, then whether the body is decodable and non-empty, and only then whether the target id exists. Write that order down and test it.

Hint 2

Compare the parsed media type, not the raw header string, so that `text/plain; charset=utf-8` is accepted. Also remember that framework-generated 404, 405 and 413 pages are usually HTML unless you register JSON error handlers.

Hint 3

For black-box testing, derive cases from the spec using equivalence classes, boundary values, documented error paths and invariants (round-trip, idempotence, no input mutation). Do not assert behavior the spec leaves undefined.