Skip to content

Your first mock server ​

A mock server is a stand-in for a real API: it answers at the addresses you choose with the data you choose. In about ten minutes you will start one, add your own endpoint and call it three different ways.

What you need ​

  • Mock Studio installed. See Install Mock Studio.
  • Nothing else. A mock runs on your own computer and needs no internet.

Know the window ​

The first time Mock Studio opens, it creates a sample project called My First Mock API so there is something to explore.

Mock Studio with the sample project open and its server running
  1. 1Rail — Endpoints, Live, Flows, Share and Project, then Import, Guide and Settings.
  2. 2Project — The project you are working in. One project is one mock API.
  3. 3Start server / Stop — Starts and stops the server. The address is shown just above.
  4. 4Endpoints — The addresses this mock answers at.
  5. 5Method and path — What request this endpoint answers.
  6. 6Send — Tries the endpoint without leaving the app.
  7. 7How it responds — One answer, rules, a sequence, or a random pick.
  8. 8Response — The status, headers and body to send back.

Part 1: start the server ​

  1. With My First Mock API open, select Start server.

  2. The card changes to Server running and shows an address such as http://localhost:8080. The number after the colon is the port. Yours may differ; use the one you see.

  3. In the endpoint list, select Health check. It answers GET /health.

  4. Select Send in the bar under the path. The response appears below: status 200 and a small JSON body with the current time.

You now have an API running on your computer.

Part 2: call it from outside the app ​

The mock is a real HTTP server, so anything can call it.

From a browser. Select the Open in browser icon in the tester bar, or type the address yourself: http://localhost:8080/health.

From a terminal:

bash
curl http://localhost:8080/health

From ApiWorkbench. Open a new request, enter http://localhost:8080/health and select Send.

Then open Live on the rail in Mock Studio. Every request you made is listed there with its time, method, path and status. Select one to see its headers and body.

Part 3: add your own endpoint ​

  1. Go back to Endpoints and select New endpoint (the + beside the Endpoints heading).

  2. Set the method to GET and the path to /books/:id.

    The :id part is a path parameter: it matches anything in that position, so the endpoint answers /books/1, /books/2 and /books/abc.

  3. Give it the endpoint name Get book.

  4. Leave Single response selected. In the response, keep the status at 200 and the body type at JSON.

  5. Replace the body with:

    json
    {
      "id": "{{params.id}}",
      "title": "Book number {{params.id}}",
      "author": "{{faker.name}}",
      "fetchedAt": "{{now}}"
    }
  6. Select Send in the tester bar, or open http://localhost:8080/books/7 in your browser.

The answer has 7 where you wrote {{params.id}}, a made-up name where you wrote {{faker.name}}, and the current time. Those double-brace tokens are templates: Mock Studio fills them in on every request. The Insert variable button lists the common ones, and Dynamic templating lists them all.

Part 4: make it say no ​

Real APIs do not always say yes. Make this one return 404 for one book.

  1. In Get book, choose Conditional rules.

  2. Select Add response. Label the new response Not found, set the status to 404, and use this body:

    json
    { "error": "not_found" }
  3. On that response, select Add condition and set it to Path param id equals 0.

  4. Make sure the Not found response comes before the plain one. A response with no conditions is the fallback.

  5. Call /books/0, then /books/5.

/books/0 now answers 404 and every other id answers 200. You have built an API that behaves differently depending on the request, which is what you need in order to test how an app copes with errors.

What you learned ​

  • A mock server answers real HTTP requests with the responses you design.
  • Start server runs it; Live shows every request it receives.
  • Path parameters such as :id match many URLs; templates such as {{params.id}} put request data into the response.
  • Conditional rules choose a response from the request.

Next steps ​