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) is found within the html.

A valid title starts with <title>foo where foo is the expected title text. Returns true if the expected title is found, otherwise returns false.

This function is case insensitive, if a title of “foo” is specified it will match “foo” or “Foo” or “FOO”.

While you can invoke this function directly, it’s generally preferred to invoke validate_and_load_static_assets which in turn invokes this function.

Example

use goose::prelude::*;
use goose_eggs::valid_title;

task!(validate_title).set_on_start();

async fn validate_title(user: &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) => {
                    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(())
}