pub struct TestRequest { /* private fields */ }
Expand description
A TestRequest
is for building and executing a HTTP request to the TestServer
.
§Building
Requests are created by the TestServer
, using it’s builder functions.
They correspond to the appropriate HTTP method: TestServer::get()
,
TestServer::post()
, etc.
See there for documentation.
§Customising
The TestRequest
allows the caller to fill in the rest of the request
to be sent to the server. Including the headers, the body, cookies,
and the content type, using the relevant functions.
The TestRequest struct provides a number of methods to set up the request, such as json, text, bytes, expect_failure, content_type, etc.
§Sending
Once fully configured you send the request by awaiting the request object.
// Build your request
let request = server.get(&"/user")
.add_header("x-custom-header", "example.com")
.content_type("application/yaml");
// await request to execute
let response = request.await;
You will receive a TestResponse
.
§Cookie Saving
TestRequest::save_cookies()
and TestRequest::do_not_save_cookies()
methods allow you to set the request to save cookies to the TestServer
,
for reuse on any future requests.
This behaviour is off by default, and can be changed for all TestRequests
when building the TestServer
. By building it with a TestServerConfig
where save_cookies
is set to true.
§Expecting Failure and Success
When making a request you can mark it to expect a response within, or outside, of the 2xx range of HTTP status codes.
If the response returns a status code different to what is expected, then it will panic.
This is useful when making multiple requests within a test. As it can find issues earlier than later.
See the TestRequest::expect_failure()
,
and TestRequest::expect_success()
.
Implementations§
Source§impl TestRequest
impl TestRequest
Sourcepub fn json<J>(self, body: &J) -> Self
pub fn json<J>(self, body: &J) -> Self
Set the body of the request to send up data as Json,
and changes the content type to application/json
.
Sourcepub fn json_from_file<P>(self, path: P) -> Self
pub fn json_from_file<P>(self, path: P) -> Self
Sends a payload as a Json request, with the contents coming from a file.
Sourcepub fn yaml<Y>(self, body: &Y) -> Self
Available on crate feature yaml
only.
pub fn yaml<Y>(self, body: &Y) -> Self
yaml
only.Set the body of the request to send up data as Yaml,
and changes the content type to application/yaml
.
Sourcepub fn yaml_from_file<P>(self, path: P) -> Self
Available on crate feature yaml
only.
pub fn yaml_from_file<P>(self, path: P) -> Self
yaml
only.Sends a payload as a Yaml request, with the contents coming from a file.
Sourcepub fn msgpack<M>(self, body: &M) -> Self
Available on crate feature msgpack
only.
pub fn msgpack<M>(self, body: &M) -> Self
msgpack
only.Set the body of the request to send up data as MsgPack,
and changes the content type to application/msgpack
.
Sourcepub fn form<F>(self, body: &F) -> Self
pub fn form<F>(self, body: &F) -> Self
Sets the body of the request, with the content type of ‘application/x-www-form-urlencoded’.
Sourcepub fn multipart(self, multipart: MultipartForm) -> Self
pub fn multipart(self, multipart: MultipartForm) -> Self
For sending multipart forms.
The payload is built using MultipartForm
and Part
.
This will be sent with the content type of ‘multipart/form-data’.
§Simple example
use axum::Router;
use axum_test::TestServer;
use axum_test::multipart::MultipartForm;
let app = Router::new();
let server = TestServer::new(app)?;
let multipart_form = MultipartForm::new()
.add_text("name", "Joe")
.add_text("animals", "foxes");
let response = server.post(&"/my-form")
.multipart(multipart_form)
.await;
§Sending byte parts
use axum::Router;
use axum_test::TestServer;
use axum_test::multipart::MultipartForm;
use axum_test::multipart::Part;
let app = Router::new();
let server = TestServer::new(app)?;
let image_bytes = include_bytes!("../README.md");
let image_part = Part::bytes(image_bytes.as_slice())
.file_name(&"README.md")
.mime_type(&"text/markdown");
let multipart_form = MultipartForm::new()
.add_part("file", image_part);
let response = server.post(&"/my-form")
.multipart(multipart_form)
.await;
Sourcepub fn text<T>(self, raw_text: T) -> Selfwhere
T: Display,
pub fn text<T>(self, raw_text: T) -> Selfwhere
T: Display,
Set raw text as the body of the request,
and sets the content type to text/plain
.
Sourcepub fn text_from_file<P>(self, path: P) -> Self
pub fn text_from_file<P>(self, path: P) -> Self
Sends a payload as plain text, with the contents coming from a file.
Sourcepub fn bytes(self, body_bytes: Bytes) -> Self
pub fn bytes(self, body_bytes: Bytes) -> Self
Set raw bytes as the body of the request.
The content type is left unchanged.
Sourcepub fn bytes_from_file<P>(self, path: P) -> Self
pub fn bytes_from_file<P>(self, path: P) -> Self
Reads the contents of the file as raw bytes, and sends it within the request.
The content type is left unchanged, and no parsing of the file is done.
Sourcepub fn content_type(self, content_type: &str) -> Self
pub fn content_type(self, content_type: &str) -> Self
Set the content type to use for this request in the header.
Adds a Cookie to be sent with this request.
Adds many cookies to be used with this request.
Clears all cookies used internally within this Request,
including any that came from the TestServer
.
Any cookies returned will be saved to the TestServer
that created this,
which will continue to use those cookies on future requests.
Cookies returned by this will not be saved to the TestServer
.
For use by future requests.
This is the default behaviour.
You can change that default in TestServerConfig
.
Sourcepub fn add_query_param<V>(self, key: &str, value: V) -> Selfwhere
V: Serialize,
pub fn add_query_param<V>(self, key: &str, value: V) -> Selfwhere
V: Serialize,
Adds query parameters to be sent with this request.
Sourcepub fn add_query_params<V>(self, query_params: V) -> Selfwhere
V: Serialize,
pub fn add_query_params<V>(self, query_params: V) -> Selfwhere
V: Serialize,
Adds the structure given as query parameters for this request.
This is designed to take a list of parameters, or a body of parameters, and then serializes them into the parameters of the request.
§Sending a body of parameters using json!
use axum::Router;
use axum_test::TestServer;
use serde_json::json;
let app = Router::new();
let server = TestServer::new(app)?;
let response = server.get(&"/my-end-point")
.add_query_params(json!({
"username": "Brian",
"age": 20
}))
.await;
§Sending a body of parameters with Serde
use axum::Router;
use axum_test::TestServer;
use serde::Deserialize;
use serde::Serialize;
#[derive(Serialize, Deserialize)]
struct UserQueryParams {
username: String,
age: u32,
}
let app = Router::new();
let server = TestServer::new(app)?;
let response = server.get(&"/my-end-point")
.add_query_params(UserQueryParams {
username: "Brian".to_string(),
age: 20
})
.await;
§Sending a list of parameters
use axum::Router;
use axum_test::TestServer;
let app = Router::new();
let server = TestServer::new(app)?;
let response = server.get(&"/my-end-point")
.add_query_params(&[
("username", "Brian"),
("age", "20"),
])
.await;
Sourcepub fn add_raw_query_param(self, query_param: &str) -> Self
pub fn add_raw_query_param(self, query_param: &str) -> Self
Adds a query param onto the end of the request, with no urlencoding of any kind.
This exists to allow custom query parameters, such as for the many versions of query param arrays.
use axum::Router;
use axum_test::TestServer;
let app = Router::new();
let server = TestServer::new(app)?;
let response = server.get(&"/my-end-point")
.add_raw_query_param(&"my-flag")
.add_raw_query_param(&"array[]=123")
.add_raw_query_param(&"filter[value]=some-value")
.await;
Sourcepub fn clear_query_params(self) -> Self
pub fn clear_query_params(self) -> Self
Clears all query params set,
including any that came from the TestServer
.
Sourcepub fn add_header<N, V>(self, name: N, value: V) -> Self
pub fn add_header<N, V>(self, name: N, value: V) -> Self
Adds a header to be sent with this request.
use axum::Router;
use axum_test::TestServer;
let app = Router::new();
let server = TestServer::new(app)?;
let response = server.get(&"/my-end-point")
.add_header("x-custom-header", "custom-value")
.add_header(http::header::CONTENT_LENGTH, 12345)
.add_header(http::header::HOST, "example.com")
.await;
Adds an ‘AUTHORIZATION’ HTTP header to the request, with no internal formatting of what is given.
Adds an ‘AUTHORIZATION’ HTTP header to the request, in the ‘Bearer {token}’ format.
Sourcepub fn clear_headers(self) -> Self
pub fn clear_headers(self) -> Self
Clears all headers set.
Sourcepub fn scheme(self, scheme: &str) -> Self
pub fn scheme(self, scheme: &str) -> Self
Sets the scheme to use when making the request. i.e. http or https. The default scheme is ‘http’.
use axum::Router;
use axum_test::TestServer;
let app = Router::new();
let server = TestServer::new(app)?;
let response = server
.get(&"/my-end-point")
.scheme(&"https")
.await;
Sourcepub fn expect_success(self) -> Self
pub fn expect_success(self) -> Self
Marks that this request is expected to always return a HTTP status code within the 2xx range (200 to 299).
If a code outside of that range is returned, then this will panic.
use axum::Json;
use axum::Router;
use axum::routing::put;
use serde_json::json;
use axum_test::TestServer;
let app = Router::new()
.route(&"/todo", put(|| async { unimplemented!() }));
let server = TestServer::new(app)?;
// If this doesn't return a value in the 2xx range,
// then it will panic.
server.put(&"/todo")
.expect_success()
.json(&json!({
"task": "buy milk",
}))
.await;
Sourcepub fn expect_failure(self) -> Self
pub fn expect_failure(self) -> Self
Marks that this request is expected to return a HTTP status code outside of the 2xx range.
If a code within the 2xx range is returned, then this will panic.
Trait Implementations§
Source§impl Debug for TestRequest
impl Debug for TestRequest
Source§impl IntoFuture for TestRequest
impl IntoFuture for TestRequest
Source§type Output = TestResponse
type Output = TestResponse
Source§type IntoFuture = AutoFuture<TestResponse>
type IntoFuture = AutoFuture<TestResponse>
Source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Auto Trait Implementations§
impl Freeze for TestRequest
impl !RefUnwindSafe for TestRequest
impl Send for TestRequest
impl !Sync for TestRequest
impl Unpin for TestRequest
impl !UnwindSafe for TestRequest
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.bright_black());
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.bright_green());
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.bright_yellow());
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.bright_magenta());
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.bright_white());
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.on_bright_black());
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.on_bright_green());
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.on_bright_yellow());
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
§Example
println!("{}", value.on_bright_blue());
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.on_bright_magenta());
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
§Example
println!("{}", value.on_bright_cyan());
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.on_bright_white());
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
§Example
println!("{}", value.underline());
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
§Example
println!("{}", value.rapid_blink());
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);