Function goose_eggs::valid_title [−][src]
pub fn valid_title(html: &str, title: &str) -> bool
Expand description
Returns a bool
indicating whether or not the title (case insensitive) on the
webpage contains the provided string.
While you can invoke this function directly, it’s generally preferred to invoke
validate_and_load_static_assets
which in turn invokes this function.
A valid title is found between <title></title>
tags inside <head></head>
tags.
For example, if the title is as follows:
<head>
<title>this is the title</title>
</head>
Then a call to valid_title("the title")
will return true
, whereas a call to
valid_title("foo")
will return false
.
This function is case insensitive, so in the above example calling
valid_title("The Title")
and valid_title("THE TITLE")
will both also return
true
. The function only tests if the title includes the specified text, the
title can also include other text and will still be considered valid.
Example
use goose::prelude::*;
use goose_eggs::valid_title;
task!(validate_title).set_on_start();
async fn validate_title(user: &mut GooseUser) -> GooseTaskResult {
let mut goose = user.get("/").await?;
match goose.response {
Ok(response) => {
// Copy the headers so we have them for logging if there are errors.
let headers = &response.headers().clone();
match response.text().await {
Ok(html) => {
// Confirm that the HTML header includes the expected title.
let title = "example";
if !valid_title(&html, title) {
return user.set_failure(
&format!("{}: title not found: {}", goose.request.raw.url, title),
&mut goose.request,
Some(headers),
Some(&html),
);
}
}
Err(e) => {
return user.set_failure(
&format!("{}: failed to parse page: {}", goose.request.raw.url, e),
&mut goose.request,
Some(headers),
None,
);
}
}
}
Err(e) => {
return user.set_failure(
&format!("{}: no response from server: {}", goose.request.raw.url, e),
&mut goose.request,
None,
None,
);
}
}
Ok(())
}