Struct axum_test::TestRequest
source · pub struct TestRequest { /* private fields */ }Expand description
A TestRequest represents a HTTP request to the test server.
Creating
Requests are created by the TestServer. You do not create them yourself.
The TestServer has functions corresponding to specific requests.
For example calling TestServer::get to create a new HTTP GET request,
or `TestServer::post to create a HTTP POST request.
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, the content type,
and other relevant details.
The TestRequest struct provides a number of methods to set up the request, such as json, text, bytes, expect_failure, content_type, etc. The do_save_cookies and do_not_save_cookies methods are used to control cookie handling.
Sending
Once fully configured you send the rquest by awaiting the request object.
let request = server.get(&"/user");
let response = request.await;You will receive back a TestResponse.
Implementations§
source§impl TestRequest
impl TestRequest
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.
Clears all cookies used internally within this Request.
Adds a Cookie to be sent with this request.
sourcepub fn expect_failure(self) -> Self
pub fn expect_failure(self) -> Self
Marks that this request should expect to fail. Failiure is deemend as any response that isn’t a 200.
By default, requests are expct to always succeed.
sourcepub fn expect_success(self) -> Self
pub fn expect_success(self) -> Self
Marks that this request should expect to succeed. Success is deemend as returning a 200.
Note this is the default behaviour when creating a new TestRequest.
sourcepub fn json<J>(self, body: &J) -> Selfwhere
J: ?Sized + Serialize,
pub fn json<J>(self, body: &J) -> Selfwhere J: ?Sized + Serialize,
Set the body of the request to send up as Json.
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.
If there isn’t a content type set, this will default to text/plain.
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 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.