Function goose_eggs::header_is_set

source ·
pub fn header_is_set(headers: &HeaderMap, header: &str) -> bool
Expand description

Returns a bool indicating whether or not a header was set in the server Response.

Returns true if the expected header was set, otherwise returns false.

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

Example

use goose::prelude::*;
use goose_eggs::header_is_set;

transaction!(validate_header).set_on_start();

async fn validate_header(user: &mut GooseUser) -> TransactionResult {
    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();
            if !header_is_set(headers, "server") {
                return user.set_failure(
                    &format!("{}: header not found: {}", goose.request.raw.url, "server"),
                    &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(())
}