HTTP mock server
Constructor
mock_server()
Returns HttpMock
Start a mock HTTP server on a free port and return a handle. Stopped
automatically at the end of the scenario. Use url to point the system
under test at it, respond/on to define routes.
Example
#![allow(unused)]
fn main() {
let hooks = mock_server();
hooks.on("POST", "/voice", |req| json_response(#{ actions: [ #{ type: "answer" } ] }));
http("PUT", env("API_URL") + "/config?webhook=" + hooks.url + "/voice");
}
mock_server(config: map)
Returns HttpMock
Start a mock HTTP server with config; stopped automatically at scenario
end. Omit port (or use mock_server()) for a free one.
Config — mock_server(#{ … }):
| Field | Type | Description |
|---|---|---|
port | int | port to bind (omit for a free one) |
Example
#![allow(unused)]
fn main() {
let hooks = mock_server(#{ port: 8080 });
}
Methods
mock.last_request(path: PathPattern)
Receiver HttpMock · Takes PathPattern · Returns MockRequest
The most recent request on a regex(...) path (errors if none yet).
mock.last_request(path: string)
Receiver HttpMock · Returns MockRequest
The most recent request on path (errors if none yet). Read it after
await_until confirms the webhook arrived.
Example
#![allow(unused)]
fn main() {
let req = hooks.last_request("/voice");
assert(req.json("event")).equals("incoming_call");
}
mock.on(method: string, path: PathPattern, responder: Fn)
Receiver HttpMock · Takes PathPattern
Dynamic responder for method and a regex(...) path.
mock.on(method: string, path: string, responder: Fn)
Receiver HttpMock
Answer method path dynamically: the |req| closure receives the
MockRequest and returns a response map (e.g. json_response(#{…})).
method may be "*" for any method. The closure runs on a runtime
worker, so keep it pure (request → response): no agent verbs, no wait
— those block a worker thread.
Example
#![allow(unused)]
fn main() {
hooks.on("POST", "/voice", |req| {
if req.json("event") == "incoming_call" {
json_response(#{ actions: [ #{ type: "answer" } ] })
} else {
json_response(#{ actions: [ #{ type: "hangup" } ] })
}
});
}
mock.on(path: PathPattern, responder: Fn)
Receiver HttpMock · Takes PathPattern
Dynamic responder for a regex(...) path on any HTTP method.
mock.on(path: string, responder: Fn)
Receiver HttpMock
Dynamic responder for path on any HTTP method.
mock.request_count(path: PathPattern)
Receiver HttpMock · Takes PathPattern · Returns int
How many requests arrived on a regex(...) path (any method).
mock.request_count(path: string)
Receiver HttpMock · Returns int
How many requests arrived on path (any method). Poll it with
await_until to wait for a webhook.
Example
#![allow(unused)]
fn main() {
await_until(|| assert(hooks.request_count("/voice")).equals(1), "10s");
}
mock.requests(path: PathPattern)
Receiver HttpMock · Takes PathPattern · Returns array
All requests on a regex(...) path, in arrival order, as MockRequests.
mock.requests(path: string)
Receiver HttpMock · Returns array
All requests received on path, in arrival order, as MockRequests.
mock.respond(method: string, path: PathPattern, response: map)
Receiver HttpMock · Takes PathPattern
Static response for method and a regex(...) path.
mock.respond(method: string, path: string, response: map)
Receiver HttpMock
Register a static response for method path: a map
#{ status: 200, content_type: "…", headers: #{…}, body: <string|map> }
(use json_response/text_response to build it). method may be "*"
for any method. Re-register to stage the next answer between webhooks.
Example
#![allow(unused)]
fn main() {
hooks.respond("POST", "/voice", json_response(#{ actions: [ #{ type: "hangup" } ] }));
}
mock.respond(path: PathPattern, response: map)
Receiver HttpMock · Takes PathPattern
Static response for a regex(...) path on any HTTP method.
mock.respond(path: string, response: map)
Receiver HttpMock
Static response for path on any HTTP method.
mock.stop()
Receiver HttpMock
Stop the server now (it otherwise stops automatically at scenario end).
Fields
mock.port
Receiver HttpMock · Returns int
The port the server is listening on.
mock.url
Receiver HttpMock · Returns string
The server’s base URL, e.g. http://127.0.0.1:8080.
Helpers
json_response(body)
Returns map
Build a 200 application/json response map from body (JSON-encoded),
for respond/on. body may be a map or an array, e.g.
json_response(#{ actions: [ … ] }) or json_response([ … ]).
Example
#![allow(unused)]
fn main() {
hooks.respond("POST", "/voice", json_response(#{ actions: [ #{ type: "answer" } ] }));
}
regex(pattern: string)
Returns PathPattern
A regex path matcher for respond/on/request_count/… anchored to the
whole path: regex("/calls/.*") matches /calls/123. Errors on a bad
pattern.
Example
#![allow(unused)]
fn main() {
hooks.on(regex("/calls/.*"), |req| text_response("ok"));
}
text_response(body: string)
Returns map
Build a 200 text/plain response map from body, for respond/on.