pub struct GooseRequestBuilder<'a> { /* private fields */ }
Expand description

Used to build a GooseRequest object, necessary to make a request with Goose.

It’s only necessary to build manually if the GooseUser::get, GooseUser::get_named, GooseUser::post, GooseUser::post_form, GooseUser::post_json, GooseUser::head and GooseUser::delete helpers don’t provide you with enough flexibility.

Example

use goose::prelude::*;

let mut a_transaction = transaction!(transaction_function);

// A simple transaction that loads a relative path.
async fn transaction_function(user: &mut GooseUser) -> TransactionResult {
    // Manually create a GooseRequestBuilder object.
    let goose_request = GooseRequest::builder()
        // Set a relative path to request.
        .path("about")
        // Name the request in the metircs.
        .name("About page")
        // Build the GooseRequest object.
        .build();

    // Make the configured request.
    let _goose = user.request(goose_request).await?;

    Ok(())
}

Implementations§

source§

impl<'a> GooseRequestBuilder<'a>

source

pub fn path(self, path: impl Into<&'a str>) -> Self

Set the path to request.

Typically is a relative path allowing Goose to append a configurable base_url.

Defaults to "" (the main index).

Example

This can be implemented in a simpler way using the GooseUser::get helper function.

use goose::prelude::*;

let mut a_transaction = transaction!(transaction_function);

// A simple transaction that loads a relative path.
async fn transaction_function(user: &mut GooseUser) -> TransactionResult {
    // Manually create a GooseRequestBuilder object.
    let goose_request = GooseRequest::builder()
        // Set a relative path to request.
        .path("a/relative/path")
        // Build the GooseRequest object.
        .build();

    // Make the configured request.
    let _goose = user.request(goose_request).await?;

    Ok(())
}
source

pub fn method(self, method: GooseMethod) -> Self

Set the method of the request.

Must be set to a GooseMethod.

Defaults to GooseMethod::Get.

Example
use goose::prelude::*;

let mut a_transaction = transaction!(transaction_function);

// Make a DELETE request.
async fn transaction_function(user: &mut GooseUser) -> TransactionResult {
    // Manually create a GooseRequestBuilder object.
    let goose_request = GooseRequest::builder()
        // Set a relative path to request.
        .path("path/to/delete")
        // Set the method to DELETE.
        .method(GooseMethod::Delete)
        // Build the GooseRequest object.
        .build();

    // Make the configured DELETE request.
    let _goose = user.request(goose_request).await?;

    Ok(())
}
source

pub fn name(self, name: impl Into<&'a str>) -> Self

Set a name for the request, affecting how it shows up in metrics.

Must be set to a GooseMethod.

Defaults to GooseMethod::Get.

Example
use goose::prelude::*;

let mut a_transaction = transaction!(transaction_function);

// Make a named request.
async fn transaction_function(user: &mut GooseUser) -> TransactionResult {
    // Manually create a GooseRequestBuilder object.
    let goose_request = GooseRequest::builder()
        // Set a relative path to request.
        .path("path/to/request")
        // Name the request in the metrics.
        .name("custom name")
        // Build the GooseRequest object.
        .build();

    // Make the configured request.
    let _goose = user.request(goose_request).await?;

    Ok(())
}
source

pub fn expect_status_code(self, status_code: u16) -> Self

Manually configure the expected HTTP response status code.

Defaults to reqwest::StatusCode::is_success.

Example

Intentionally request a 404 page, and do not trigger an error.

use goose::prelude::*;

let mut a_transaction = transaction!(transaction_function);

// Make a named request.
async fn transaction_function(user: &mut GooseUser) -> TransactionResult {
    // Manually create a GooseRequestBuilder object.
    let goose_request = GooseRequest::builder()
        // Set a relative path to request.
        .path("no/such/path")
        // Tell Goose to expect a 404 HTTP response status code.
        .expect_status_code(404)
        // Build the GooseRequest object.
        .build();

    // Make the configured request.
    let _goose = user.request(goose_request).await?;

    Ok(())
}
source

pub fn error_on_fail(self) -> Self

Configure whether the request should return on error when it fails

Defaults to false.

Example

Intentionally request a 404 page, and do not trigger an error.

use goose::prelude::*;

let mut a_transaction = transaction!(transaction_function);

// Make a named request.
async fn transaction_function(user: &mut GooseUser) -> TransactionResult {
    // Manually create a GooseRequestBuilder object.
    let goose_request = GooseRequest::builder()
        // Set a relative path to request.
        .path("no/such/path")
        // Tell Goose to expect a 404 HTTP response status code.
        .expect_status_code(404)
        // Tell Goose to return an error if the status code is
        // not a 404
        .error_on_fail()
        // Build the GooseRequest object.
        .build();

    // Make the configured request.
    let _goose = user.request(goose_request).await?;

    Ok(())
}
source

pub fn set_request_builder(self, request_builder: RequestBuilder) -> Self

Manually create the reqwest::RequestBuilder used to make a request.

Example

Manually create a RequestBuilder in order to set a timeout.

use goose::prelude::*;

let mut a_transaction = transaction!(transaction_function);

async fn transaction_function(user: &mut GooseUser) -> TransactionResult {
    use std::time::Duration;

    // Manually interact with the Reqwest RequestBuilder object.
    let request_builder = user.get_request_builder(&GooseMethod::Get, "no/such/path")?
        // Configure the request to timeout if it takes longer than 500 milliseconds.
        .timeout(Duration::from_millis(500));

    // Manually build a GooseRequest in order to set our custom RequestBuilder.
    let goose_request = GooseRequest::builder()
        // Manually add our custom RequestBuilder object.
        .set_request_builder(request_builder)
        // Turn the GooseRequestBuilder object into a GooseRequest.
        .build();

    // Finally make the actual request with our custom GooseRequest object.
    let _goose = user.request(goose_request).await?;

    Ok(())
}
source

pub fn build(self) -> GooseRequest<'a>

Build the GooseRequest object which is then passed to GooseUser::request.

Example
use goose::prelude::*;

// Build the default "GET /".
let goose_request = GooseRequest::builder().build();

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast for Twhere T: Any,

source§

fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
source§

fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for Twhere T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T, Global>) -> Arc<dyn Any + Send + Sync, Global>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> GooseUserData for Twhere T: Send + Sync + 'static,