From fa81d9700457864be8a73b9953e970ca04888e61 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 6 May 2018 20:05:31 -0700 Subject: [PATCH] more handler tests --- src/server/h1.rs | 6 +-- tests/test_handlers.rs | 114 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 3 deletions(-) diff --git a/src/server/h1.rs b/src/server/h1.rs index 4f502605f..8d862b39b 100644 --- a/src/server/h1.rs +++ b/src/server/h1.rs @@ -165,7 +165,7 @@ where // completed 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); } } else { @@ -350,7 +350,7 @@ where } } Ok(Some(Message::Eof)) => { - if let Some(ref mut payload) = self.payload.take() { + if let Some(mut payload) = self.payload.take() { payload.feed_eof(); } else { error!("Internal server error: unexpected eof"); @@ -360,7 +360,7 @@ where Ok(None) => break, Err(e) => { 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 { DecoderError::Io(e) => PayloadError::Io(e), DecoderError::Error(_) => PayloadError::EncodingCorrupted, diff --git a/tests/test_handlers.rs b/tests/test_handlers.rs index adba95d7a..8aea34d0a 100644 --- a/tests/test_handlers.rs +++ b/tests/test_handlers.rs @@ -74,6 +74,33 @@ fn test_query_extractor() { 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| { + 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] fn test_path_and_query_extractor() { 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); } +#[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, p: Path, _: Query| { + 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, Path, Query)| { + 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] fn test_non_ascii_route() { let mut srv = test::TestServer::new(|app| {