From 193f8fb2d9b11995b1e1dc35041e2f4d10095299 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 29 Mar 2019 18:51:07 -0700 Subject: [PATCH] update tests --- actix-http/Cargo.toml | 3 +- actix-http/tests/test_client.rs | 8 ++-- actix-http/tests/test_server.rs | 75 ++++++++++++++++----------------- awc/src/ws.rs | 8 ++-- test-server/Cargo.toml | 5 +-- test-server/src/lib.rs | 26 ++++++++---- tests/test_server.rs | 12 ++++-- 7 files changed, 74 insertions(+), 63 deletions(-) diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 180cda787..0c910cb1e 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -96,8 +96,9 @@ openssl = { version="0.10", optional = true } [dev-dependencies] actix-rt = "0.2.2" -actix-server = { version = "0.4.0", features=["ssl"] } +actix-server = { version = "0.4.1", features=["ssl"] } actix-connect = { version = "0.1.0", features=["ssl"] } +#actix-http-test = { version = "0.1.0-alpha.1", features=["ssl"] } actix-http-test = { path = "../test-server", features=["ssl"] } env_logger = "0.6" diff --git a/actix-http/tests/test_client.rs b/actix-http/tests/test_client.rs index 1ca7437d6..78b999703 100644 --- a/actix-http/tests/test_client.rs +++ b/actix-http/tests/test_client.rs @@ -52,18 +52,18 @@ fn test_h1_v2() { assert!(response.status().is_success()); let request = srv.get().header("x-test", "111").send(); - let mut response = srv.block_on(request).unwrap(); + let response = srv.block_on(request).unwrap(); assert!(response.status().is_success()); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); - let mut response = srv.block_on(srv.post().send()).unwrap(); + let response = srv.block_on(srv.post().send()).unwrap(); assert!(response.status().is_success()); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } diff --git a/actix-http/tests/test_server.rs b/actix-http/tests/test_server.rs index 455edfec1..f1f82b089 100644 --- a/actix-http/tests/test_server.rs +++ b/actix-http/tests/test_server.rs @@ -13,8 +13,7 @@ use futures::stream::{once, Stream}; use actix_http::body::Body; use actix_http::error::PayloadError; use actix_http::{ - body, error, http, http::header, Error, HttpMessage, HttpService, KeepAlive, - Request, Response, + body, error, http, http::header, Error, HttpService, KeepAlive, Request, Response, }; fn load_body(stream: S) -> impl Future @@ -145,10 +144,10 @@ fn test_h2_body() -> std::io::Result<()> { ) }); - let mut response = srv.block_on(srv.sget().send_body(data.clone())).unwrap(); + let response = srv.block_on(srv.sget().send_body(data.clone())).unwrap(); assert!(response.status().is_success()); - let body = srv.block_on(load_body(response.take_payload())).unwrap(); + let body = srv.load_body(response).unwrap(); assert_eq!(&body, data.as_bytes()); Ok(()) } @@ -436,11 +435,11 @@ fn test_h1_headers() { }) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get().send()).unwrap(); assert!(response.status().is_success()); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from(data2)); } @@ -480,11 +479,11 @@ fn test_h2_headers() { }).map_err(|_| ())) }); - let mut response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget().send()).unwrap(); assert!(response.status().is_success()); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from(data2)); } @@ -516,11 +515,11 @@ fn test_h1_body() { HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().body(STR))) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get().send()).unwrap(); assert!(response.status().is_success()); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -538,11 +537,11 @@ fn test_h2_body2() { ) }); - let mut response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget().send()).unwrap(); assert!(response.status().is_success()); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -552,7 +551,7 @@ fn test_h1_head_empty() { HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR))) }); - let mut response = srv.block_on(srv.head().send()).unwrap(); + let response = srv.block_on(srv.head().send()).unwrap(); assert!(response.status().is_success()); { @@ -564,7 +563,7 @@ fn test_h1_head_empty() { } // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert!(bytes.is_empty()); } @@ -582,7 +581,7 @@ fn test_h2_head_empty() { ) }); - let mut response = srv.block_on(srv.shead().send()).unwrap(); + let response = srv.block_on(srv.shead().send()).unwrap(); assert!(response.status().is_success()); assert_eq!(response.version(), http::Version::HTTP_2); @@ -595,7 +594,7 @@ fn test_h2_head_empty() { } // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert!(bytes.is_empty()); } @@ -607,7 +606,7 @@ fn test_h1_head_binary() { }) }); - let mut response = srv.block_on(srv.head().send()).unwrap(); + let response = srv.block_on(srv.head().send()).unwrap(); assert!(response.status().is_success()); { @@ -619,7 +618,7 @@ fn test_h1_head_binary() { } // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert!(bytes.is_empty()); } @@ -641,7 +640,7 @@ fn test_h2_head_binary() { ) }); - let mut response = srv.block_on(srv.shead().send()).unwrap(); + let response = srv.block_on(srv.shead().send()).unwrap(); assert!(response.status().is_success()); { @@ -653,7 +652,7 @@ fn test_h2_head_binary() { } // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert!(bytes.is_empty()); } @@ -713,11 +712,11 @@ fn test_h1_body_length() { }) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get().send()).unwrap(); assert!(response.status().is_success()); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -740,11 +739,11 @@ fn test_h2_body_length() { ) }); - let mut response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget().send()).unwrap(); assert!(response.status().is_success()); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -761,7 +760,7 @@ fn test_h1_body_chunked_explicit() { }) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get().send()).unwrap(); assert!(response.status().is_success()); assert_eq!( response @@ -774,7 +773,7 @@ fn test_h1_body_chunked_explicit() { ); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); // decode assert_eq!(bytes, Bytes::from_static(STR.as_ref())); @@ -802,12 +801,12 @@ fn test_h2_body_chunked_explicit() { ) }); - let mut response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget().send()).unwrap(); assert!(response.status().is_success()); assert!(!response.headers().contains_key(header::TRANSFER_ENCODING)); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); // decode assert_eq!(bytes, Bytes::from_static(STR.as_ref())); @@ -822,7 +821,7 @@ fn test_h1_body_chunked_implicit() { }) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get().send()).unwrap(); assert!(response.status().is_success()); assert_eq!( response @@ -835,7 +834,7 @@ fn test_h1_body_chunked_implicit() { ); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } @@ -854,11 +853,11 @@ fn test_h1_response_http_error_handling() { })) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get().send()).unwrap(); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert!(bytes.is_empty()); } @@ -886,11 +885,11 @@ fn test_h2_response_http_error_handling() { ) }); - let mut response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget().send()).unwrap(); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert!(bytes.is_empty()); } @@ -901,11 +900,11 @@ fn test_h1_service_error() { .h1(|_| Err::(error::ErrorBadRequest("error"))) }); - let mut response = srv.block_on(srv.get().send()).unwrap(); + let response = srv.block_on(srv.get().send()).unwrap(); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert!(bytes.is_empty()); } @@ -924,10 +923,10 @@ fn test_h2_service_error() { ) }); - let mut response = srv.block_on(srv.sget().send()).unwrap(); + let response = srv.block_on(srv.sget().send()).unwrap(); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); // read response - let bytes = srv.block_on(load_body(response.take_payload())).unwrap(); + let bytes = srv.load_body(response).unwrap(); assert!(bytes.is_empty()); } diff --git a/awc/src/ws.rs b/awc/src/ws.rs index 9697210de..bc023c069 100644 --- a/awc/src/ws.rs +++ b/awc/src/ws.rs @@ -11,7 +11,7 @@ use cookie::{Cookie, CookieJar}; use futures::future::{err, Either, Future}; use tokio_timer::Timeout; -pub use actix_http::ws::{CloseCode, CloseReason, Frame, Message}; +pub use actix_http::ws::{CloseCode, CloseReason, Codec, Frame, Message}; use crate::connect::BoxedSocket; use crate::error::{InvalidUrl, SendRequestError, WsClientError}; @@ -213,10 +213,8 @@ impl WebsocketsRequest { /// Complete request construction and connect to a websockets server. pub fn connect( mut self, - ) -> impl Future< - Item = (ClientResponse, Framed), - Error = WsClientError, - > { + ) -> impl Future), Error = WsClientError> + { if let Some(e) = self.err.take() { return Either::A(err(e.into())); } diff --git a/test-server/Cargo.toml b/test-server/Cargo.toml index 582d96ed3..a9b58483c 100644 --- a/test-server/Cargo.toml +++ b/test-server/Cargo.toml @@ -30,12 +30,11 @@ default = ["session"] session = ["cookie/secure"] # openssl -ssl = ["openssl", "actix-http/ssl", "actix-server/ssl", "awc/ssl"] +ssl = ["openssl", "actix-server/ssl", "awc/ssl"] [dependencies] actix-codec = "0.1.1" actix-rt = "0.2.1" -actix-http = { path = "../actix-http" } actix-service = "0.3.4" actix-server = "0.4.0" actix-utils = "0.3.4" @@ -61,4 +60,4 @@ tokio-timer = "0.2" openssl = { version="0.10", optional = true } [dev-dependencies] -actix-web = { path = ".." } +actix-web = "1.0.0-alpha.1" diff --git a/test-server/src/lib.rs b/test-server/src/lib.rs index 07a0e0b4c..9eec065cd 100644 --- a/test-server/src/lib.rs +++ b/test-server/src/lib.rs @@ -3,12 +3,12 @@ use std::sync::mpsc; use std::{net, thread, time}; use actix_codec::{AsyncRead, AsyncWrite, Framed}; -use actix_http::client::Connector; -use actix_http::ws; use actix_rt::{Runtime, System}; use actix_server::{Server, StreamServiceFactory}; -use awc::{Client, ClientRequest}; -use futures::future::{lazy, Future}; +use awc::{error::PayloadError, ws, Client, ClientRequest, ClientResponse, Connector}; +use bytes::Bytes; +use futures::future::lazy; +use futures::{Future, Stream}; use http::Method; use net2::TcpBuilder; @@ -193,13 +193,16 @@ impl TestServerRuntime { self.client.request(method, path.as_ref()) } - /// Stop http server - fn stop(&mut self) { - System::current().stop(); + pub fn load_body( + &mut self, + response: ClientResponse, + ) -> Result + where + S: Stream + 'static, + { + self.block_on(response.body().limit(10_485_760)) } -} -impl TestServerRuntime { /// Connect to websocket server at a given path pub fn ws_at( &mut self, @@ -219,6 +222,11 @@ impl TestServerRuntime { { self.ws_at("/") } + + /// Stop http server + fn stop(&mut self) { + System::current().stop(); + } } impl Drop for TestServerRuntime { diff --git a/tests/test_server.rs b/tests/test_server.rs index 11ba1b6a9..fc590ff0b 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -100,7 +100,8 @@ fn test_body_encoding_override() { .service(web::resource("/raw").route(web::to(|| { use actix_web::middleware::encoding::BodyEncoding; let body = actix_web::dev::Body::Bytes(STR.into()); - let mut response = Response::with_body(actix_web::http::StatusCode::OK, body); + let mut response = + Response::with_body(actix_web::http::StatusCode::OK, body); response.encoding(ContentEncoding::Deflate); @@ -123,7 +124,13 @@ fn test_body_encoding_override() { assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref())); // Raw Response - let mut response = srv.block_on(srv.request(actix_web::http::Method::GET, srv.url("/raw")).no_decompress().send()).unwrap(); + let mut response = srv + .block_on( + srv.request(actix_web::http::Method::GET, srv.url("/raw")) + .no_decompress() + .send(), + ) + .unwrap(); assert!(response.status().is_success()); // read response @@ -134,7 +141,6 @@ fn test_body_encoding_override() { e.write_all(bytes.as_ref()).unwrap(); let dec = e.finish().unwrap(); assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref())); - } #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]