Function goose_eggs::valid_text[][src]

pub fn valid_text(html: &str, text: &str) -> bool
Expand description

Returns a bool indicating whether or not an arbitrary str (case sensitive) is found within the html.

Returns true if the expected str is found, otherwise returns false.

This function is case sensitive, if the text “foo” is specified it will only match “foo”, not “Foo” or “FOO”.

It is generally preferred to use validate_and_load_static_assets which uses this function.

Example

use goose::prelude::*;
use goose_eggs::valid_text;

task!(validate_text).set_on_start();

async fn validate_text(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 text = r#"<code class="language-console">$ cargo new hello_world --bin"#;
                    if !valid_text(&html, text) {
                        return user.set_failure(
                            &format!("{}: text not found: {}", goose.request.url, text),
                            &mut goose.request,
                            Some(&headers),
                            Some(&html),
                        );
                    }
                }
                Err(e) => {
                    return user.set_failure(
                        &format!("{}: failed to parse page: {}", goose.request.url, e),
                        &mut goose.request,
                        Some(&headers),
                        None,
                    );
                }
            }
        }
        Err(e) => {
            return user.set_failure(
                &format!("{}: no response from server: {}", goose.request.url, e),
                &mut goose.request,
                None,
                None,
            );
        }
    }

    Ok(())
}