Function goose_eggs::valid_header_value
source · pub fn valid_header_value<'a>(
headers: &HeaderMap,
header: (&'a str, &'a str)
) -> bool
Expand description
Returns a bool
indicating whether or not a header contains an expected value.
Returns true
if the expected value was found, otherwise returns false
.
Expects a &str
tuple
with a length of 2 where the first defines the header
name and the second defines the header value, ie ("name", "value")
.
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::valid_header_value;
transaction!(validate_header_value).set_on_start();
async fn validate_header_value(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 !valid_header_value(headers, ("server", "nginx")) {
return user.set_failure(
&format!("{}: server header value not correct: {}", goose.request.raw.url, "nginx"),
&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(())
}