liveBook · Manning

Mock server

As explained in Rust Web Development

A mock server is a server that simulates an application's behavior for testing.

A mock server simulates an external service during a test. Pointing the application at a local endpoint preserves the real HTTP client code while allowing the test to exercise predefined success and error responses. section 11.1.3

For a profanity check, create a Rust library with `cargo new --lib mock-server` and add it as a local dependency. The server should start at a specified address and port, expose the third-party route, accept the same parameters, and return preset JSON responses. Configure the API URL so the test can target localhost instead of the real service. section 11.1.3

Shut the server down after the test. A Tokio oneshot channel provides a way for the test to signal shutdown. section 11.1.3

Figure 11.3 A oneshot channel to send exactly one message can be used to trigger a shutdown from the receiver side of the message.
11-03

The diagram illustrates how a oneshot message triggers server shutdown. section 11.1.3

How Rust Web Development covers it

How deep it goes
Walks through library setup, matching the API endpoint, predefined responses, and test-triggered shutdown.
Examples
Exercises a profanity-checking function with local success and error responses.
What you can do after
Build it from scratch as a local server that tests application responses and shuts down afterward.
How it connects
Connects HTTP testing with API configuration and oneshot channels.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Web Development. The section links point to the parts of the book it draws on.