Skip to content

Commit 87c8fa8

Browse files
committed
more context to tests
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent b101271 commit 87c8fa8

File tree

3 files changed

+76
-18
lines changed

3 files changed

+76
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ http = { version = "0.2.0", optional = true }
2828

2929
[dev-dependencies]
3030
http = "0.2.0"
31+
async-std = "1.4.0"

src/request.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pin_project_lite::pin_project! {
2020
/// use http_types::{Url, Method, Request};
2121
///
2222
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
23-
/// req.set_body("hello world");
23+
/// req.set_body("Hello, Nori!");
2424
/// ```
2525
#[derive(Debug)]
2626
pub struct Request {
@@ -111,7 +111,7 @@ impl Request {
111111
/// use http_types::{Url, Method, Request};
112112
///
113113
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
114-
/// req.set_body("hello world");
114+
/// req.set_body("Hello, Nori!");
115115
/// ```
116116
pub fn set_body(&mut self, body: impl Into<Body>) {
117117
self.body = body.into();
@@ -121,16 +121,27 @@ impl Request {
121121
}
122122
}
123123

124-
/// Replace the request body with a new body, and return the old body.
124+
/// Swaps the value of the body with another body, without deinitializing
125+
/// either one.
125126
///
126127
/// # Examples
127128
///
128129
/// ```
130+
/// # use async_std::io::prelude::*;
131+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
132+
/// # async_std::task::block_on(async {
133+
/// #
129134
/// use http_types::{Body, Url, Method, Request};
130135
///
131136
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
132-
/// req.set_body("hello world");
133-
/// let _body: Body = req.replace_body("hello planet");
137+
/// req.set_body("Hello, Nori!");
138+
/// let mut body: Body = req.replace_body("Hello, Chashu!");
139+
///
140+
/// let mut string = String::new();
141+
/// body.read_to_string(&mut string).await?;
142+
/// assert_eq!(&string, "Hello, Nori!");
143+
/// #
144+
/// # Ok(()) }) }
134145
/// ```
135146
pub fn replace_body(&mut self, body: impl Into<Body>) -> Body {
136147
mem::replace(&mut self.body, body.into())
@@ -141,12 +152,22 @@ impl Request {
141152
/// # Examples
142153
///
143154
/// ```
155+
/// # use async_std::io::prelude::*;
156+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
157+
/// # async_std::task::block_on(async {
158+
/// #
144159
/// use http_types::{Body, Url, Method, Request};
145160
///
146161
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
147-
/// req.set_body("hello world");
148-
/// let mut body = "hello planet".into();
162+
/// req.set_body("Hello, Nori!");
163+
/// let mut body = "Hello, Chashu!".into();
149164
/// req.swap_body(&mut body);
165+
///
166+
/// let mut string = String::new();
167+
/// body.read_to_string(&mut string).await?;
168+
/// assert_eq!(&string, "Hello, Nori!");
169+
/// #
170+
/// # Ok(()) }) }
150171
/// ```
151172
pub fn swap_body(&mut self, body: &mut Body) {
152173
mem::swap(&mut self.body, body);
@@ -157,11 +178,25 @@ impl Request {
157178
/// # Examples
158179
///
159180
/// ```
181+
/// # use async_std::io::prelude::*;
182+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
183+
/// # async_std::task::block_on(async {
184+
/// #
160185
/// use http_types::{Body, Url, Method, Request};
161186
///
162187
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
163-
/// req.set_body("hello world");
164-
/// let _body: Body = req.take_body();
188+
/// req.set_body("Hello, Nori!");
189+
/// let mut body: Body = req.take_body();
190+
///
191+
/// let mut string = String::new();
192+
/// body.read_to_string(&mut string).await?;
193+
/// assert_eq!(&string, "Hello, Nori!");
194+
///
195+
/// # let mut string = String::new();
196+
/// # req.read_to_string(&mut string).await?;
197+
/// # assert_eq!(&string, "");
198+
/// #
199+
/// # Ok(()) }) }
165200
/// ```
166201
pub fn take_body(&mut self) -> Body {
167202
self.replace_body(Body::empty())

src/response.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,22 @@ impl Response {
114114
/// # Examples
115115
///
116116
/// ```
117-
/// # fn main() -> Result<(), http_types::url::ParseError> {
117+
/// # use async_std::io::prelude::*;
118+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
119+
/// # async_std::task::block_on(async {
118120
/// #
119121
/// use http_types::{Body, Url, Method, Request};
120122
///
121123
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
122124
/// req.set_body("Hello, Nori!");
123125
///
124-
/// let body: Body = req.replace_body("Hello, Chashu");
126+
/// let mut body: Body = req.replace_body("Hello, Chashu");
127+
///
128+
/// let mut string = String::new();
129+
/// body.read_to_string(&mut string).await?;
130+
/// assert_eq!(&string, "Hello, Nori!");
125131
/// #
126-
/// # Ok(()) }
132+
/// # Ok(()) }) }
127133
/// ```
128134
pub fn replace_body(&mut self, body: impl Into<Body>) -> Body {
129135
mem::replace(&mut self.body, body.into())
@@ -135,17 +141,23 @@ impl Response {
135141
/// # Examples
136142
///
137143
/// ```
138-
/// # fn main() -> Result<(), http_types::url::ParseError> {
144+
/// # use async_std::io::prelude::*;
145+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
146+
/// # async_std::task::block_on(async {
139147
/// #
140148
/// use http_types::{Body, Url, Method, Request};
141149
///
142150
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
143151
/// req.set_body("Hello, Nori!");
144152
///
145-
/// let mut body = "Hello, Chashu".into();
153+
/// let mut body = "Hello, Chashu!".into();
146154
/// req.swap_body(&mut body);
155+
///
156+
/// let mut string = String::new();
157+
/// body.read_to_string(&mut string).await?;
158+
/// assert_eq!(&string, "Hello, Nori!");
147159
/// #
148-
/// # Ok(()) }
160+
/// # Ok(()) }) }
149161
/// ```
150162
pub fn swap_body(&mut self, body: &mut Body) {
151163
mem::swap(&mut self.body, body);
@@ -156,15 +168,25 @@ impl Response {
156168
/// # Examples
157169
///
158170
/// ```
159-
/// # fn main() -> Result<(), http_types::url::ParseError> {
171+
/// # use async_std::io::prelude::*;
172+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
173+
/// # async_std::task::block_on(async {
160174
/// #
161175
/// use http_types::{Body, Url, Method, Request};
162176
///
163177
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
164178
/// req.set_body("Hello, Nori!");
165-
/// let _body: Body = req.take_body();
179+
/// let mut body: Body = req.take_body();
180+
///
181+
/// let mut string = String::new();
182+
/// body.read_to_string(&mut string).await?;
183+
/// assert_eq!(&string, "Hello, Nori!");
184+
///
185+
/// # let mut string = String::new();
186+
/// # req.read_to_string(&mut string).await?;
187+
/// # assert_eq!(&string, "");
166188
/// #
167-
/// # Ok(()) }
189+
/// # Ok(()) }) }
168190
/// ```
169191
pub fn take_body(&mut self) -> Body {
170192
self.replace_body(Body::empty())

0 commit comments

Comments
 (0)