mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
more handler tests
This commit is contained in:
parent
c54f045b39
commit
fa81d97004
@ -165,7 +165,7 @@ where
|
|||||||
// completed
|
// completed
|
||||||
self.flags.insert(Flags::ERROR);
|
self.flags.insert(Flags::ERROR);
|
||||||
|
|
||||||
if let Some(ref mut payload) = self.payload {
|
if let Some(mut payload) = self.payload.take() {
|
||||||
payload.set_error(PayloadError::Incomplete);
|
payload.set_error(PayloadError::Incomplete);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -350,7 +350,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Some(Message::Eof)) => {
|
Ok(Some(Message::Eof)) => {
|
||||||
if let Some(ref mut payload) = self.payload.take() {
|
if let Some(mut payload) = self.payload.take() {
|
||||||
payload.feed_eof();
|
payload.feed_eof();
|
||||||
} else {
|
} else {
|
||||||
error!("Internal server error: unexpected eof");
|
error!("Internal server error: unexpected eof");
|
||||||
@ -360,7 +360,7 @@ where
|
|||||||
Ok(None) => break,
|
Ok(None) => break,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.flags.insert(Flags::ERROR);
|
self.flags.insert(Flags::ERROR);
|
||||||
if let Some(ref mut payload) = self.payload {
|
if let Some(mut payload) = self.payload.take() {
|
||||||
let e = match e {
|
let e = match e {
|
||||||
DecoderError::Io(e) => PayloadError::Io(e),
|
DecoderError::Io(e) => PayloadError::Io(e),
|
||||||
DecoderError::Error(_) => PayloadError::EncodingCorrupted,
|
DecoderError::Error(_) => PayloadError::EncodingCorrupted,
|
||||||
|
@ -74,6 +74,33 @@ fn test_query_extractor() {
|
|||||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_async_extractor_async() {
|
||||||
|
let mut srv = test::TestServer::new(|app| {
|
||||||
|
app.resource("/{username}/index.html", |r| {
|
||||||
|
r.route().with(|data: Json<Value>| {
|
||||||
|
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
|
||||||
|
.unwrap()
|
||||||
|
.and_then(move |_| Ok(format!("{}", data.0)))
|
||||||
|
.responder()
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// client request
|
||||||
|
let request = srv.post()
|
||||||
|
.uri(srv.url("/test1/index.html"))
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.body("{\"test\": 1}")
|
||||||
|
.unwrap();
|
||||||
|
let response = srv.execute(request.send()).unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
|
// read response
|
||||||
|
let bytes = srv.execute(response.body()).unwrap();
|
||||||
|
assert_eq!(bytes, Bytes::from_static(b"{\"test\":1}"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_path_and_query_extractor() {
|
fn test_path_and_query_extractor() {
|
||||||
let mut srv = test::TestServer::new(|app| {
|
let mut srv = test::TestServer::new(|app| {
|
||||||
@ -263,6 +290,93 @@ fn test_path_and_query_extractor2_async2() {
|
|||||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_path_and_query_extractor2_async3() {
|
||||||
|
let mut srv = test::TestServer::new(|app| {
|
||||||
|
app.resource("/{username}/index.html", |r| {
|
||||||
|
r.route().with3(
|
||||||
|
|data: Json<Value>, p: Path<PParam>, _: Query<PParam>| {
|
||||||
|
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
|
||||||
|
.unwrap()
|
||||||
|
.and_then(move |_| {
|
||||||
|
Ok(format!("Welcome {} - {}!", p.username, data.0))
|
||||||
|
})
|
||||||
|
.responder()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// client request
|
||||||
|
let request = srv.post()
|
||||||
|
.uri(srv.url("/test1/index.html?username=test2"))
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.body("{\"test\": 1}")
|
||||||
|
.unwrap();
|
||||||
|
let response = srv.execute(request.send()).unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
|
// read response
|
||||||
|
let bytes = srv.execute(response.body()).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
bytes,
|
||||||
|
Bytes::from_static(b"Welcome test1 - {\"test\":1}!")
|
||||||
|
);
|
||||||
|
|
||||||
|
// client request
|
||||||
|
let request = srv.get()
|
||||||
|
.uri(srv.url("/test1/index.html"))
|
||||||
|
.finish()
|
||||||
|
.unwrap();
|
||||||
|
let response = srv.execute(request.send()).unwrap();
|
||||||
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_path_and_query_extractor2_async4() {
|
||||||
|
let mut srv = test::TestServer::new(|app| {
|
||||||
|
app.resource("/{username}/index.html", |r| {
|
||||||
|
r.route()
|
||||||
|
.with(|data: (Json<Value>, Path<PParam>, Query<PParam>)| {
|
||||||
|
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
|
||||||
|
.unwrap()
|
||||||
|
.and_then(move |_| {
|
||||||
|
Ok(format!(
|
||||||
|
"Welcome {} - {}!",
|
||||||
|
data.1.username,
|
||||||
|
(data.0).0
|
||||||
|
))
|
||||||
|
})
|
||||||
|
.responder()
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// client request
|
||||||
|
let request = srv.post()
|
||||||
|
.uri(srv.url("/test1/index.html?username=test2"))
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.body("{\"test\": 1}")
|
||||||
|
.unwrap();
|
||||||
|
let response = srv.execute(request.send()).unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
|
// read response
|
||||||
|
let bytes = srv.execute(response.body()).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
bytes,
|
||||||
|
Bytes::from_static(b"Welcome test1 - {\"test\":1}!")
|
||||||
|
);
|
||||||
|
|
||||||
|
// client request
|
||||||
|
let request = srv.get()
|
||||||
|
.uri(srv.url("/test1/index.html"))
|
||||||
|
.finish()
|
||||||
|
.unwrap();
|
||||||
|
let response = srv.execute(request.send()).unwrap();
|
||||||
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_non_ascii_route() {
|
fn test_non_ascii_route() {
|
||||||
let mut srv = test::TestServer::new(|app| {
|
let mut srv = test::TestServer::new(|app| {
|
||||||
|
Loading…
Reference in New Issue
Block a user