tpt-http-stub
Lightweight in-process HTTP stub server for tests.
Spin up a real HTTP server on a random free port (reserved via
tpt-port-scout, so there are no TOCTOU
races between parallel tests), register route responses, and assert on the
requests your code actually made. No async runtime required — a blocking
thread-per-connection server keeps it simple and synchronous.
Quick Start
[]
= "0.1"
use HttpStub;
let stub = new;
stub.on.respond;
// Point your client at the base URL.
let client = my_http_client;
assert_eq!;
stub.assert_called_once;
Stubbing responses
use HttpStub;
let stub = new;
// Plain text.
stub.on.respond;
// JSON — serialised for you.
stub.on.respond_json;
// Requests to unregistered routes return 500 ("no stub registered …").
Asserting on requests
use HttpStub;
let stub = new;
stub.on.respond;
// ... make a request to /search?q=rust ...
let req = stub.last_request.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
stub.assert_called_once;
stub.assert_called_n;
Parallel tests
Each HttpStub binds its own port and runs on its own threads, so concurrent
stubs in parallel tests never interfere.
License
MIT OR Apache-2.0