pub struct FakeServer { /* private fields */ }Expand description
A fake HTTP service on 127.0.0.1 for request tests: canned answers by method and path prefix, and every request it received.
ocre test --e2e runs the app in workerd on the same machine, so the
app can call it. Point the app at url for one client’s
requests with a test header (Client::header
and http::test_override), or a
webhook endpoint at it, then assert on requests
(wrap the check in eventually when a
job makes the call). A request no answer matches gets 404 with
{"error": "no fake answer"}. The server runs until the test process
ends; each request is served on its own thread.
§Examples
use ocre::{serde_json::json, testing::{Client, FakeServer, eventually}};
#[test]
#[ignore = "request test: run with `ocre test --e2e`"]
fn checkout_creates_an_invoice() {
let payments = FakeServer::start();
payments.answer_json("POST", "/invoices", 201, &json!({"id": "inv_1", "url": "https://pay.example/inv_1"}));
let mut client = Client::new().header("x-ocre-test-payments-api", payments.url());
client.post("/checkout", &[("plan", "pro")]).assert_redirect_to("https://pay.example/inv_1");
let invoice = eventually(|| payments.requests().into_iter().find(|r| r.path == "/invoices"));
assert_eq!(invoice.json::<ocre::serde_json::Value>()["amount"], 900);
}Implementations§
Source§impl FakeServer
impl FakeServer
Sourcepub fn answer(
&self,
method: &str,
path_prefix: &str,
status: u16,
body: &str,
) -> &Self
pub fn answer( &self, method: &str, path_prefix: &str, status: u16, body: &str, ) -> &Self
Answers method requests (* for any) whose path starts with path_prefix with status and body (text/plain).
The newest matching answer wins, so a test can change an answer midway.
Sourcepub fn answer_json(
&self,
method: &str,
path_prefix: &str,
status: u16,
value: &Value,
) -> &Self
pub fn answer_json( &self, method: &str, path_prefix: &str, status: u16, value: &Value, ) -> &Self
answer with a JSON body (application/json).
Sourcepub fn requests(&self) -> Vec<ReceivedRequest>
pub fn requests(&self) -> Vec<ReceivedRequest>
Every request received so far, oldest first.
Trait Implementations§
Source§impl Clone for FakeServer
impl Clone for FakeServer
Source§fn clone(&self) -> FakeServer
fn clone(&self) -> FakeServer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more