Skip to main content

Module testing

Module testing 

Source
Available on crate feature testing only.
Expand description

Test handlers offline with MockSite, MockPage and Visit.

A MockSite is a Backend, so a whole crawl can run against it. To test what a handler does with one page, without crawling the links it queues, visit the page:

use spire_core::dataset::InMemDataset;
use spire_core::frontier::seed::Seed;
use spire_core::testing::{MockPage, MockSite};
use spire_core::worker::{Outcome, from_fn};
use spire_core::{Context, Result};

async fn page(cx: Context<impl spire_core::backend::Session>) -> Result<()> {
    let body = cx.body().await?;
    cx.dataset::<usize>()?.write(body.len()).await?;
    cx.queue().follow("/next").await?;
    Ok(())
}

let site = MockSite::new().page("https://example.com/", MockPage::html("<p>Hi</p>"));
let visit = site
    .visit(Seed::get("https://example.com/")?)
    .with_dataset(InMemDataset::<usize>::new());

assert!(matches!(visit.run(&from_fn(page)).await, Outcome::Continue));
assert_eq!(visit.dataset::<usize>()?.read_all().await?, [9]);
assert_eq!(visit.queued()[0].uri(), "https://example.com/next");
assert_eq!(site.requests(), ["https://example.com/"]);

Structsยง

MockPage
A response a MockSite serves.
MockSession
The Session of a MockSite, for one request.
MockSite
A Backend serving fixed MockPages by URI, for tests.
Sent
A request a MockSite received, see MockSite::sent.
Visit
One request to a MockSite, run through a worker without crawling the links it queues.