pub struct MockSite { /* private fields */ }Available on crate feature
testing only.Expand description
A Backend serving fixed MockPages by URI, for tests.
URIs without a page get a 404 Not Found. Statuses are treated as the
real backends treat them, by a StatusPolicy: by default, an error
status fails the fetch, so the crawler retries or fails the request.
Clones share the record of requests.
§Examples
use spire_core::Crawler;
use spire_core::frontier::seed::Seed;
use spire_core::testing::{MockPage, MockSite};
use spire_core::worker::from_fn;
let site = MockSite::new()
.page("https://example.com/", MockPage::html("<p>Home</p>"))
.page("https://example.com/old", MockPage::redirect("https://example.com/"));
let worker = from_fn(|cx: spire_core::Context<_>| async move { cx.body().await.map(drop) });
let crawler = Crawler::new(site.clone(), worker).with_seeds([
Seed::get("https://example.com/old")?,
Seed::get("https://example.com/missing")?,
]);
let stats = crawler.run().await?;
assert_eq!((stats.succeeded, stats.failed), (1, 1));
assert_eq!(site.requests().len(), 2);Implementations§
Source§impl MockSite
impl MockSite
Sourcepub fn page(self, uri: &str, page: MockPage) -> Self
pub fn page(self, uri: &str, page: MockPage) -> Self
Serves page at uri, replacing any previous page there.
§Panics
Panics if uri is not a valid absolute URI.
Sourcepub const fn with_status_policy(self, policy: StatusPolicy) -> Self
pub const fn with_status_policy(self, policy: StatusPolicy) -> Self
Replaces the StatusPolicy, Classify
by default.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MockSite
impl RefUnwindSafe for MockSite
impl Send for MockSite
impl Sync for MockSite
impl Unpin for MockSite
impl UnsafeUnpin for MockSite
impl UnwindSafe for MockSite
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more