mirror of
https://github.com/actix/actix-extras.git
synced 2024-12-01 02:44:37 +01:00
fix warns
This commit is contained in:
parent
2a6e4dc7ab
commit
a7a2d4cf5c
@ -18,7 +18,7 @@ fn main() {
|
|||||||
.client_timeout(1000)
|
.client_timeout(1000)
|
||||||
.client_disconnect(1000)
|
.client_disconnect(1000)
|
||||||
.server_hostname("localhost")
|
.server_hostname("localhost")
|
||||||
.finish(|mut req: Request| {
|
.finish(|req: Request| {
|
||||||
req.body().limit(512).and_then(|bytes: Bytes| {
|
req.body().limit(512).and_then(|bytes: Bytes| {
|
||||||
info!("request body: {:?}", bytes);
|
info!("request body: {:?}", bytes);
|
||||||
let mut res = Response::Ok();
|
let mut res = Response::Ok();
|
||||||
|
@ -8,7 +8,7 @@ use futures::Future;
|
|||||||
use log::info;
|
use log::info;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn handle_request(mut req: Request) -> impl Future<Item = Response, Error = Error> {
|
fn handle_request(req: Request) -> impl Future<Item = Response, Error = Error> {
|
||||||
req.body().limit(512).from_err().and_then(|bytes: Bytes| {
|
req.body().limit(512).from_err().and_then(|bytes: Bytes| {
|
||||||
info!("request body: {:?}", bytes);
|
info!("request body: {:?}", bytes);
|
||||||
let mut res = Response::Ok();
|
let mut res = Response::Ok();
|
||||||
|
@ -128,7 +128,7 @@ pub trait HttpMessage: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
fn body(&mut self) -> MessageBody<Self> {
|
fn body(&self) -> MessageBody<Self> {
|
||||||
MessageBody::new(self)
|
MessageBody::new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ pub trait HttpMessage: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
fn urlencoded<T: DeserializeOwned>(&mut self) -> UrlEncoded<Self, T> {
|
fn urlencoded<T: DeserializeOwned>(&self) -> UrlEncoded<Self, T> {
|
||||||
UrlEncoded::new(self)
|
UrlEncoded::new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,12 +198,12 @@ pub trait HttpMessage: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
fn json<T: DeserializeOwned>(&mut self) -> JsonBody<Self, T> {
|
fn json<T: DeserializeOwned>(&self) -> JsonBody<Self, T> {
|
||||||
JsonBody::new(self)
|
JsonBody::new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return stream of lines.
|
/// Return stream of lines.
|
||||||
fn readlines(&mut self) -> Readlines<Self> {
|
fn readlines(&self) -> Readlines<Self> {
|
||||||
Readlines::new(self)
|
Readlines::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -220,10 +220,10 @@ pub struct Readlines<T: HttpMessage> {
|
|||||||
|
|
||||||
impl<T: HttpMessage> Readlines<T> {
|
impl<T: HttpMessage> Readlines<T> {
|
||||||
/// Create a new stream to read request line by line.
|
/// Create a new stream to read request line by line.
|
||||||
fn new(req: &mut T) -> Self {
|
fn new(req: &T) -> Self {
|
||||||
let encoding = match req.encoding() {
|
let encoding = match req.encoding() {
|
||||||
Ok(enc) => enc,
|
Ok(enc) => enc,
|
||||||
Err(err) => return Self::err(req, err.into()),
|
Err(err) => return Self::err(err.into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
Readlines {
|
Readlines {
|
||||||
@ -242,9 +242,9 @@ impl<T: HttpMessage> Readlines<T> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn err(req: &mut T, err: ReadlinesError) -> Self {
|
fn err(err: ReadlinesError) -> Self {
|
||||||
Readlines {
|
Readlines {
|
||||||
stream: req.payload(),
|
stream: None,
|
||||||
buff: BytesMut::new(),
|
buff: BytesMut::new(),
|
||||||
limit: 262_144,
|
limit: 262_144,
|
||||||
checked_buff: true,
|
checked_buff: true,
|
||||||
@ -366,7 +366,7 @@ pub struct MessageBody<T: HttpMessage> {
|
|||||||
|
|
||||||
impl<T: HttpMessage> MessageBody<T> {
|
impl<T: HttpMessage> MessageBody<T> {
|
||||||
/// Create `MessageBody` for request.
|
/// Create `MessageBody` for request.
|
||||||
pub fn new(req: &mut T) -> MessageBody<T> {
|
pub fn new(req: &T) -> MessageBody<T> {
|
||||||
let mut len = None;
|
let mut len = None;
|
||||||
if let Some(l) = req.headers().get(header::CONTENT_LENGTH) {
|
if let Some(l) = req.headers().get(header::CONTENT_LENGTH) {
|
||||||
if let Ok(s) = l.to_str() {
|
if let Ok(s) = l.to_str() {
|
||||||
@ -465,7 +465,7 @@ pub struct UrlEncoded<T: HttpMessage, U> {
|
|||||||
|
|
||||||
impl<T: HttpMessage, U> UrlEncoded<T, U> {
|
impl<T: HttpMessage, U> UrlEncoded<T, U> {
|
||||||
/// Create a new future to URL encode a request
|
/// Create a new future to URL encode a request
|
||||||
pub fn new(req: &mut T) -> UrlEncoded<T, U> {
|
pub fn new(req: &T) -> UrlEncoded<T, U> {
|
||||||
// check content type
|
// check content type
|
||||||
if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" {
|
if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" {
|
||||||
return Self::err(UrlencodedError::ContentType);
|
return Self::err(UrlencodedError::ContentType);
|
||||||
@ -702,7 +702,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_urlencoded_error() {
|
fn test_urlencoded_error() {
|
||||||
let mut req = TestRequest::with_header(
|
let req = TestRequest::with_header(
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
"application/x-www-form-urlencoded",
|
"application/x-www-form-urlencoded",
|
||||||
)
|
)
|
||||||
@ -713,7 +713,7 @@ mod tests {
|
|||||||
UrlencodedError::UnknownLength
|
UrlencodedError::UnknownLength
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut req = TestRequest::with_header(
|
let req = TestRequest::with_header(
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
"application/x-www-form-urlencoded",
|
"application/x-www-form-urlencoded",
|
||||||
)
|
)
|
||||||
@ -724,7 +724,7 @@ mod tests {
|
|||||||
UrlencodedError::Overflow
|
UrlencodedError::Overflow
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut req = TestRequest::with_header(header::CONTENT_TYPE, "text/plain")
|
let req = TestRequest::with_header(header::CONTENT_TYPE, "text/plain")
|
||||||
.header(header::CONTENT_LENGTH, "10")
|
.header(header::CONTENT_LENGTH, "10")
|
||||||
.finish();
|
.finish();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -735,7 +735,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_urlencoded() {
|
fn test_urlencoded() {
|
||||||
let mut req = TestRequest::with_header(
|
let req = TestRequest::with_header(
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
"application/x-www-form-urlencoded",
|
"application/x-www-form-urlencoded",
|
||||||
)
|
)
|
||||||
@ -751,7 +751,7 @@ mod tests {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut req = TestRequest::with_header(
|
let req = TestRequest::with_header(
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
"application/x-www-form-urlencoded; charset=utf-8",
|
"application/x-www-form-urlencoded; charset=utf-8",
|
||||||
)
|
)
|
||||||
@ -770,20 +770,19 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_message_body() {
|
fn test_message_body() {
|
||||||
let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "xxxx").finish();
|
let req = TestRequest::with_header(header::CONTENT_LENGTH, "xxxx").finish();
|
||||||
match req.body().poll().err().unwrap() {
|
match req.body().poll().err().unwrap() {
|
||||||
PayloadError::UnknownLength => (),
|
PayloadError::UnknownLength => (),
|
||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut req =
|
let req = TestRequest::with_header(header::CONTENT_LENGTH, "1000000").finish();
|
||||||
TestRequest::with_header(header::CONTENT_LENGTH, "1000000").finish();
|
|
||||||
match req.body().poll().err().unwrap() {
|
match req.body().poll().err().unwrap() {
|
||||||
PayloadError::Overflow => (),
|
PayloadError::Overflow => (),
|
||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.set_payload(Bytes::from_static(b"test"))
|
.set_payload(Bytes::from_static(b"test"))
|
||||||
.finish();
|
.finish();
|
||||||
match req.body().poll().ok().unwrap() {
|
match req.body().poll().ok().unwrap() {
|
||||||
@ -791,7 +790,7 @@ mod tests {
|
|||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.set_payload(Bytes::from_static(b"11111111111111"))
|
.set_payload(Bytes::from_static(b"11111111111111"))
|
||||||
.finish();
|
.finish();
|
||||||
match req.body().limit(5).poll().err().unwrap() {
|
match req.body().limit(5).poll().err().unwrap() {
|
||||||
@ -802,14 +801,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_readlines() {
|
fn test_readlines() {
|
||||||
let mut req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.set_payload(Bytes::from_static(
|
.set_payload(Bytes::from_static(
|
||||||
b"Lorem Ipsum is simply dummy text of the printing and typesetting\n\
|
b"Lorem Ipsum is simply dummy text of the printing and typesetting\n\
|
||||||
industry. Lorem Ipsum has been the industry's standard dummy\n\
|
industry. Lorem Ipsum has been the industry's standard dummy\n\
|
||||||
Contrary to popular belief, Lorem Ipsum is not simply random text.",
|
Contrary to popular belief, Lorem Ipsum is not simply random text.",
|
||||||
))
|
))
|
||||||
.finish();
|
.finish();
|
||||||
let mut r = Readlines::new(&mut req);
|
let mut r = Readlines::new(&req);
|
||||||
match r.poll().ok().unwrap() {
|
match r.poll().ok().unwrap() {
|
||||||
Async::Ready(Some(s)) => assert_eq!(
|
Async::Ready(Some(s)) => assert_eq!(
|
||||||
s,
|
s,
|
||||||
|
10
src/json.rs
10
src/json.rs
@ -50,7 +50,7 @@ pub struct JsonBody<T: HttpMessage, U: DeserializeOwned> {
|
|||||||
|
|
||||||
impl<T: HttpMessage, U: DeserializeOwned> JsonBody<T, U> {
|
impl<T: HttpMessage, U: DeserializeOwned> JsonBody<T, U> {
|
||||||
/// Create `JsonBody` for request.
|
/// Create `JsonBody` for request.
|
||||||
pub fn new(req: &mut T) -> Self {
|
pub fn new(req: &T) -> Self {
|
||||||
// check content-type
|
// check content-type
|
||||||
let json = if let Ok(Some(mime)) = req.mime_type() {
|
let json = if let Ok(Some(mime)) = req.mime_type() {
|
||||||
mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
|
mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
|
||||||
@ -164,11 +164,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_json_body() {
|
fn test_json_body() {
|
||||||
let mut req = TestRequest::default().finish();
|
let req = TestRequest::default().finish();
|
||||||
let mut json = req.json::<MyObject>();
|
let mut json = req.json::<MyObject>();
|
||||||
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::ContentType);
|
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::ContentType);
|
||||||
|
|
||||||
let mut req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.header(
|
.header(
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
header::HeaderValue::from_static("application/text"),
|
header::HeaderValue::from_static("application/text"),
|
||||||
@ -177,7 +177,7 @@ mod tests {
|
|||||||
let mut json = req.json::<MyObject>();
|
let mut json = req.json::<MyObject>();
|
||||||
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::ContentType);
|
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::ContentType);
|
||||||
|
|
||||||
let mut req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.header(
|
.header(
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
header::HeaderValue::from_static("application/json"),
|
header::HeaderValue::from_static("application/json"),
|
||||||
@ -190,7 +190,7 @@ mod tests {
|
|||||||
let mut json = req.json::<MyObject>().limit(100);
|
let mut json = req.json::<MyObject>().limit(100);
|
||||||
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::Overflow);
|
assert_eq!(json.poll().err().unwrap(), JsonPayloadError::Overflow);
|
||||||
|
|
||||||
let mut req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.header(
|
.header(
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
header::HeaderValue::from_static("application/json"),
|
header::HeaderValue::from_static("application/json"),
|
||||||
|
@ -47,7 +47,7 @@ fn test_h1_v2() {
|
|||||||
assert!(repr.contains("ClientRequest"));
|
assert!(repr.contains("ClientRequest"));
|
||||||
assert!(repr.contains("x-test"));
|
assert!(repr.contains("x-test"));
|
||||||
|
|
||||||
let mut response = srv.block_on(request.send(&mut connector)).unwrap();
|
let response = srv.block_on(request.send(&mut connector)).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
@ -55,7 +55,7 @@ fn test_h1_v2() {
|
|||||||
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
||||||
|
|
||||||
let request = srv.post().finish().unwrap();
|
let request = srv.post().finish().unwrap();
|
||||||
let mut response = srv.block_on(request.send(&mut connector)).unwrap();
|
let response = srv.block_on(request.send(&mut connector)).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
|
@ -89,7 +89,7 @@ fn test_h2_body() -> std::io::Result<()> {
|
|||||||
.map_err(|e| println!("Openssl error: {}", e))
|
.map_err(|e| println!("Openssl error: {}", e))
|
||||||
.and_then(
|
.and_then(
|
||||||
h2::H2Service::build()
|
h2::H2Service::build()
|
||||||
.finish(|mut req: Request<_>| {
|
.finish(|req: Request<_>| {
|
||||||
req.body()
|
req.body()
|
||||||
.limit(1024 * 1024)
|
.limit(1024 * 1024)
|
||||||
.and_then(|body| Ok(Response::Ok().body(body)))
|
.and_then(|body| Ok(Response::Ok().body(body)))
|
||||||
@ -101,7 +101,7 @@ fn test_h2_body() -> std::io::Result<()> {
|
|||||||
let req = client::ClientRequest::get(srv.surl("/"))
|
let req = client::ClientRequest::get(srv.surl("/"))
|
||||||
.body(data.clone())
|
.body(data.clone())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let mut response = srv.send_request(req).unwrap();
|
let response = srv.send_request(req).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
let body = srv.block_on(response.body().limit(1024 * 1024)).unwrap();
|
let body = srv.block_on(response.body().limit(1024 * 1024)).unwrap();
|
||||||
@ -350,7 +350,7 @@ fn test_headers() {
|
|||||||
|
|
||||||
let req = srv.get().finish().unwrap();
|
let req = srv.get().finish().unwrap();
|
||||||
|
|
||||||
let mut response = srv.block_on(req.send(&mut connector)).unwrap();
|
let response = srv.block_on(req.send(&mut connector)).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
@ -387,7 +387,7 @@ fn test_body() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let req = srv.get().finish().unwrap();
|
let req = srv.get().finish().unwrap();
|
||||||
let mut response = srv.send_request(req).unwrap();
|
let response = srv.send_request(req).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
@ -402,7 +402,7 @@ fn test_head_empty() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let req = client::ClientRequest::head(srv.url("/")).finish().unwrap();
|
let req = client::ClientRequest::head(srv.url("/")).finish().unwrap();
|
||||||
let mut response = srv.send_request(req).unwrap();
|
let response = srv.send_request(req).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -428,7 +428,7 @@ fn test_head_binary() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let req = client::ClientRequest::head(srv.url("/")).finish().unwrap();
|
let req = client::ClientRequest::head(srv.url("/")).finish().unwrap();
|
||||||
let mut response = srv.send_request(req).unwrap();
|
let response = srv.send_request(req).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -477,7 +477,7 @@ fn test_body_length() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let req = srv.get().finish().unwrap();
|
let req = srv.get().finish().unwrap();
|
||||||
let mut response = srv.send_request(req).unwrap();
|
let response = srv.send_request(req).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
@ -496,7 +496,7 @@ fn test_body_chunked_explicit() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let req = srv.get().finish().unwrap();
|
let req = srv.get().finish().unwrap();
|
||||||
let mut response = srv.send_request(req).unwrap();
|
let response = srv.send_request(req).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
@ -517,7 +517,7 @@ fn test_body_chunked_implicit() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let req = srv.get().finish().unwrap();
|
let req = srv.get().finish().unwrap();
|
||||||
let mut response = srv.send_request(req).unwrap();
|
let response = srv.send_request(req).unwrap();
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
@ -540,7 +540,7 @@ fn test_response_http_error_handling() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let req = srv.get().finish().unwrap();
|
let req = srv.get().finish().unwrap();
|
||||||
let mut response = srv.send_request(req).unwrap();
|
let response = srv.send_request(req).unwrap();
|
||||||
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
|
||||||
// read response
|
// read response
|
||||||
|
Loading…
Reference in New Issue
Block a user