pub struct HttpRequestBuilder { /* private fields */ }Expand description
Builder for HttpRequest.
Implementations§
Source§impl HttpRequestBuilder
impl HttpRequestBuilder
Sourcepub fn header(
&mut self,
name: impl Into<String>,
value: impl Into<String>,
) -> &mut Self
pub fn header( &mut self, name: impl Into<String>, value: impl Into<String>, ) -> &mut Self
Appends a header to the request.
Both name and value are accepted as plain strings with no validation.
HttpRequestBuilder constructs protocol-layer values (primarily for tests),
not validated HTTP requests, so arbitrary strings — including deliberately
malformed ones — are allowed.
For validated header setting in app code, use
crate::command::RequestBuilder::header, which panics on invalid values.
Sourcepub fn body_json(&mut self, body: impl Serialize) -> &mut Self
pub fn body_json(&mut self, body: impl Serialize) -> &mut Self
Sets the body to the JSON representation of body and sets
content-type: application/json, mirroring
crate::command::RequestBuilder::body_json.
This is what you want when asserting on a request an app actually made, where spelling the header out by hand is easy to forget:
let body = serde_json::json!({ "title": "New Post" });
assert_eq!(
HttpRequest::post("https://example.com/posts")
.body_json(&body)
.build(),
HttpRequest::post("https://example.com/posts")
.header("content-type", "application/json")
.json(&body)
.build(),
);json deliberately does not set the header — these builders
construct protocol-layer values, so they must stay able to express a JSON
body with no content-type, or a malformed one. But because the capability
side sets the mime (via Body::from_json), mirroring a real request with
json alone fails on a header-only difference. Hence this method.
§Panics
Panics if the serialization fails.
Sourcepub fn build(&self) -> HttpRequest
pub fn build(&self) -> HttpRequest
Trait Implementations§
Source§impl Clone for HttpRequestBuilder
impl Clone for HttpRequestBuilder
Source§fn clone(&self) -> HttpRequestBuilder
fn clone(&self) -> HttpRequestBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more