use std::io; use ntex::http::test::server as test_server; use ntex::http::{client::error::SendRequestError, HttpService, Method, Request, Response}; use ntex::service::ServiceFactory; use ntex::{time, util::Bytes, util::Ready}; const STR: &str = "Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World"; #[ntex::test] async fn test_h1_v2() { let srv = test_server(move || { HttpService::build().finish(|_| Ready::Ok::<_, io::Error>(Response::Ok().body(STR))) }); let response = srv.request(Method::GET, "/").send().await.unwrap(); assert!(response.status().is_success()); let request = srv.request(Method::GET, "/").header("x-test", "111").send(); let mut response = request.await.unwrap(); assert!(response.status().is_success()); // read response let bytes = response.body().await.unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); let mut response = srv.request(Method::POST, "/").send().await.unwrap(); assert!(response.status().is_success()); // read response let bytes = response.body().await.unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } #[ntex::test] async fn test_connection_close() { let srv = test_server(move || { HttpService::build() .finish(|_| Ready::Ok::<_, io::Error>(Response::Ok().body(STR))) .map(|_| ()) }); let response = srv .request(Method::GET, "/") .force_close() .send() .await .unwrap(); assert!(response.status().is_success()); } #[ntex::test] async fn test_with_query_parameter() { let srv = test_server(move || { HttpService::build() .finish(|req: Request| { if req.uri().query().unwrap().contains("qp=") { Ready::Ok::<_, io::Error>(Response::Ok().finish()) } else { Ready::Ok::<_, io::Error>(Response::BadRequest().finish()) } }) .map(|_| ()) }); let request = srv.request(Method::GET, srv.url("/?qp=5")); let response = request.send().await.unwrap(); assert!(response.status().is_success()); } #[ntex::test] async fn test_client_timeout() { let srv = test_server(move || { HttpService::build() .finish(|_| async { time::sleep(time::Seconds(10)).await; Ok::<_, io::Error>(Response::Ok().body(STR)) }) .map(|_| ()) }) .set_client_timeout(time::Seconds(1), time::Millis(30_000)); let err = srv .request(Method::GET, "/") .force_close() .send() .await .err() .unwrap(); assert!(matches!(err, SendRequestError::Timeout)); }