Skip to main content

FakeServer

Struct FakeServer 

Source
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

Source

pub fn start() -> Self

Starts the server on a free port of 127.0.0.1.

§Panics

When no port can be bound.

Source

pub fn url(&self) -> &str

The base URL, http://127.0.0.1:<port>, without a trailing slash.

Source

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.

Source

pub fn answer_json( &self, method: &str, path_prefix: &str, status: u16, value: &Value, ) -> &Self

answer with a JSON body (application/json).

Source

pub fn requests(&self) -> Vec<ReceivedRequest>

Every request received so far, oldest first.

Trait Implementations§

Source§

impl Clone for FakeServer

Source§

fn clone(&self) -> FakeServer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FakeServer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V