1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use futures::stream::TryStreamExt;
use futures::AsyncRead;
use http::{HeaderMap, HeaderValue, Method, Uri, Version};
use hyper::Body;
use std::io;
pub struct Request {
pub method: Method,
pub uri: Uri,
pub version: Version,
pub headers: HeaderMap<HeaderValue>,
body: Body,
}
impl Request {
pub fn body_stream(&mut self) -> Body {
let mut body = Body::empty();
std::mem::swap(&mut body, &mut self.body);
body
}
pub fn body(&mut self) -> impl AsyncRead + Sync + Send + Unpin + 'static {
self.body_stream()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
.into_async_read()
}
#[cfg(test)]
pub fn set_body(&mut self, body: Body) {
self.body = body
}
}
impl From<http::Request<Body>> for Request {
fn from(req: http::Request<Body>) -> Self {
let (parts, body) = req.into_parts();
Self {
method: parts.method,
uri: parts.uri,
version: parts.version,
headers: parts.headers,
body,
}
}
}
impl Default for Request {
fn default() -> Self {
http::Request::new(Body::empty()).into()
}
}
#[cfg(all(test, feature = "runtime"))]
mod tests {
use crate::{App, Request};
use futures::AsyncReadExt;
use http::StatusCode;
use hyper::Body;
#[async_std::test]
async fn body_read() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::new(());
app.gate_fn(|mut ctx, _next| async move {
let mut data = String::new();
ctx.req_mut().body().read_to_string(&mut data).await?;
assert_eq!("Hello, World!", data);
Ok(())
});
let service = app.fake_service();
let mut req = Request::default();
req.set_body(Body::from("Hello, World!"));
let resp = service.serve(req).await?;
assert_eq!(StatusCode::OK, resp.status);
Ok(())
}
}