1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-23 15:24:36 +01:00

update test-server tests

This commit is contained in:
Nikolay Kim 2019-11-19 11:08:03 +06:00
parent 5ab29b2e62
commit 687884fb94
8 changed files with 54 additions and 52 deletions

View File

@ -109,17 +109,22 @@ webpki-roots = { version = "0.18", optional = true }
actix-rt = "1.0.0-alpha.1" actix-rt = "1.0.0-alpha.1"
actix-server = { version = "0.8.0-alpha.1", features=["openssl"] } actix-server = { version = "0.8.0-alpha.1", features=["openssl"] }
actix-connect = { version = "1.0.0-alpha.1", features=["openssl"] } actix-connect = { version = "1.0.0-alpha.1", features=["openssl"] }
#actix-http-test = { version = "0.2.4", features=["ssl"] } actix-http-test = { version = "0.3.0-alpha.1", features=["openssl"] }
env_logger = "0.6" env_logger = "0.6"
serde_derive = "1.0" serde_derive = "1.0"
open-ssl = { version="0.10", package="openssl" } open-ssl = { version="0.10", package="openssl" }
[patch.crates-io] [patch.crates-io]
awc = { path = "../awc" }
actix-http = { path = "." }
actix-http-test = { path = "../test-server" }
actix-codec = { path = "../../actix-net/actix-codec" } actix-codec = { path = "../../actix-net/actix-codec" }
actix-connect = { path = "../../actix-net/actix-connect" } actix-connect = { path = "../../actix-net/actix-connect" }
actix-rt = { path = "../../actix-net/actix-rt" } actix-rt = { path = "../../actix-net/actix-rt" }
actix-server = { path = "../../actix-net/actix-server" } actix-server = { path = "../../actix-net/actix-server" }
actix-server-config = { path = "../../actix-net/actix-server-config" } actix-server-config = { path = "../../actix-net/actix-server-config" }
actix-service = { path = "../../actix-net/actix-service" } actix-service = { path = "../../actix-net/actix-service" }
actix-testing = { path = "../../actix-net/actix-testing" }
actix-threadpool = { path = "../../actix-net/actix-threadpool" } actix-threadpool = { path = "../../actix-net/actix-threadpool" }
actix-utils = { path = "../../actix-net/actix-utils" } actix-utils = { path = "../../actix-net/actix-utils" }

View File

@ -76,7 +76,8 @@ where
}; };
// read response and init read body // read response and init read body
let (head, framed) = if let (Some(result), framed) = framed.into_future().await { let res = framed.into_future().await;
let (head, framed) = if let (Some(result), framed) = res {
let item = result.map_err(SendRequestError::from)?; let item = result.map_err(SendRequestError::from)?;
(item, framed) (item, framed)
} else { } else {

View File

@ -331,12 +331,10 @@ where
Poll::Ready(Err(err)) => return Err(DispatchError::Io(err)), Poll::Ready(Err(err)) => return Err(DispatchError::Io(err)),
} }
} }
if written > 0 { if written == self.write_buf.len() {
if written == self.write_buf.len() { unsafe { self.write_buf.set_len(0) }
unsafe { self.write_buf.set_len(0) } } else {
} else { let _ = self.write_buf.split_to(written);
let _ = self.write_buf.split_to(written);
}
} }
Ok(false) Ok(false)
} }

View File

@ -122,7 +122,7 @@ where
match Pin::new(&mut this.connection).poll_accept(cx) { match Pin::new(&mut this.connection).poll_accept(cx) {
Poll::Ready(None) => return Poll::Ready(Ok(())), Poll::Ready(None) => return Poll::Ready(Ok(())),
Poll::Ready(Some(Err(err))) => return Poll::Ready(Err(err.into())), Poll::Ready(Some(Err(err))) => return Poll::Ready(Err(err.into())),
Poll::Ready(Some(Ok((req, res)))) => { Poll::Ready(Some(Ok((req, _)))) => {
// update keep-alive expire // update keep-alive expire
if this.ka_timer.is_some() { if this.ka_timer.is_some() {
if let Some(expire) = this.config.keep_alive_expire() { if let Some(expire) = this.config.keep_alive_expire() {

View File

@ -2,14 +2,14 @@ use std::io::{Read, Write};
use std::time::Duration; use std::time::Duration;
use std::{net, thread}; use std::{net, thread};
use actix_http_test::TestServer; use actix_http_test::{block_fn, TestServer};
use actix_server_config::ServerConfig; use actix_server_config::ServerConfig;
use actix_service::{factory_fn_cfg, pipeline, service_fn, ServiceFactory}; use actix_service::{factory_fn_cfg, pipeline, service_fn, ServiceFactory};
use bytes::Bytes; use bytes::Bytes;
use futures::future::{self, err, ok, ready, Future, FutureExt}; use futures::future::{self, err, ok, ready, FutureExt};
use futures::stream::{once, Stream, StreamExt}; use futures::stream::{once, StreamExt};
use regex::Regex; use regex::Regex;
use tokio_timer::sleep; use tokio_timer::delay_for;
use actix_http::httpmessage::HttpMessage; use actix_http::httpmessage::HttpMessage;
use actix_http::{ use actix_http::{
@ -18,7 +18,7 @@ use actix_http::{
#[test] #[test]
fn test_h1() { fn test_h1() {
let mut srv = TestServer::new(|| { let srv = TestServer::new(|| {
HttpService::build() HttpService::build()
.keep_alive(KeepAlive::Disabled) .keep_alive(KeepAlive::Disabled)
.client_timeout(1000) .client_timeout(1000)
@ -29,13 +29,13 @@ fn test_h1() {
}) })
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
} }
#[test] #[test]
fn test_h1_2() { fn test_h1_2() {
let mut srv = TestServer::new(|| { let srv = TestServer::new(|| {
HttpService::build() HttpService::build()
.keep_alive(KeepAlive::Disabled) .keep_alive(KeepAlive::Disabled)
.client_timeout(1000) .client_timeout(1000)
@ -48,7 +48,7 @@ fn test_h1_2() {
.map(|_| ()) .map(|_| ())
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
} }
@ -84,11 +84,11 @@ fn test_expect_continue_h1() {
let srv = TestServer::new(|| { let srv = TestServer::new(|| {
HttpService::build() HttpService::build()
.expect(service_fn(|req: Request| { .expect(service_fn(|req: Request| {
sleep(Duration::from_millis(20)).then(move |_| { delay_for(Duration::from_millis(20)).then(move |_| {
if req.head().uri.query() == Some("yes=") { if req.head().uri.query() == Some("yes=") {
Ok(req) ok(req)
} else { } else {
Err(error::ErrorPreconditionFailed("error")) err(error::ErrorPreconditionFailed("error"))
} }
}) })
})) }))
@ -114,7 +114,7 @@ fn test_chunked_payload() {
let total_size: usize = chunk_sizes.iter().sum(); let total_size: usize = chunk_sizes.iter().sum();
let srv = TestServer::new(|| { let srv = TestServer::new(|| {
HttpService::build().h1(|mut request: Request| { HttpService::build().h1(service_fn(|mut request: Request| {
request request
.take_payload() .take_payload()
.map(|res| match res { .map(|res| match res {
@ -122,8 +122,10 @@ fn test_chunked_payload() {
Err(e) => panic!(format!("Error reading payload: {}", e)), Err(e) => panic!(format!("Error reading payload: {}", e)),
}) })
.fold(0usize, |acc, chunk| ready(acc + chunk.len())) .fold(0usize, |acc, chunk| ready(acc + chunk.len()))
.map(|req_size| ok(Response::Ok().body(format!("size={}", req_size)))) .map(|req_size| {
}) Ok::<_, Error>(Response::Ok().body(format!("size={}", req_size)))
})
}))
}); });
let returned_size = { let returned_size = {
@ -310,7 +312,7 @@ fn test_content_length() {
StatusCode, StatusCode,
}; };
let mut srv = TestServer::new(|| { let srv = TestServer::new(|| {
HttpService::build().h1(|req: Request| { HttpService::build().h1(|req: Request| {
let indx: usize = req.uri().path()[1..].parse().unwrap(); let indx: usize = req.uri().path()[1..].parse().unwrap();
let statuses = [ let statuses = [
@ -330,24 +332,18 @@ fn test_content_length() {
{ {
for i in 0..4 { for i in 0..4 {
let req = srv let req = srv.request(http::Method::GET, srv.url(&format!("/{}", i)));
.request(http::Method::GET, srv.url(&format!("/{}", i))) let response = block_fn(move || req.send()).unwrap();
.send();
let response = srv.block_on(req).unwrap();
assert_eq!(response.headers().get(&header), None); assert_eq!(response.headers().get(&header), None);
let req = srv let req = srv.request(http::Method::HEAD, srv.url(&format!("/{}", i)));
.request(http::Method::HEAD, srv.url(&format!("/{}", i))) let response = block_fn(move || req.send()).unwrap();
.send();
let response = srv.block_on(req).unwrap();
assert_eq!(response.headers().get(&header), None); assert_eq!(response.headers().get(&header), None);
} }
for i in 4..6 { for i in 4..6 {
let req = srv let req = srv.request(http::Method::GET, srv.url(&format!("/{}", i)));
.request(http::Method::GET, srv.url(&format!("/{}", i))) let response = block_fn(move || req.send()).unwrap();
.send();
let response = srv.block_on(req).unwrap();
assert_eq!(response.headers().get(&header), Some(&value)); assert_eq!(response.headers().get(&header), Some(&value));
} }
} }
@ -384,7 +380,7 @@ fn test_h1_headers() {
}) })
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
@ -420,7 +416,7 @@ fn test_h1_body() {
HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR))) HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
@ -434,7 +430,7 @@ fn test_h1_head_empty() {
HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR))) HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
}); });
let response = srv.block_on(srv.head("/").send()).unwrap(); let response = block_fn(|| srv.head("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
{ {
@ -458,7 +454,7 @@ fn test_h1_head_binary() {
}) })
}); });
let response = srv.block_on(srv.head("/").send()).unwrap(); let response = block_fn(|| srv.head("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
{ {
@ -476,11 +472,11 @@ fn test_h1_head_binary() {
#[test] #[test]
fn test_h1_head_binary2() { fn test_h1_head_binary2() {
let mut srv = TestServer::new(|| { let srv = TestServer::new(|| {
HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR))) HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
}); });
let response = srv.block_on(srv.head("/").send()).unwrap(); let response = block_fn(|| srv.head("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
{ {
@ -503,7 +499,7 @@ fn test_h1_body_length() {
}) })
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
// read response // read response
@ -524,7 +520,7 @@ fn test_h1_body_chunked_explicit() {
}) })
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
assert_eq!( assert_eq!(
response response
@ -552,7 +548,7 @@ fn test_h1_body_chunked_implicit() {
}) })
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
assert_eq!( assert_eq!(
response response
@ -584,7 +580,7 @@ fn test_h1_response_http_error_handling() {
})) }))
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR); assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
// read response // read response
@ -599,7 +595,7 @@ fn test_h1_service_error() {
.h1(|_| future::err::<Response, Error>(error::ErrorBadRequest("error"))) .h1(|_| future::err::<Response, Error>(error::ErrorBadRequest("error")))
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST); assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
// read response // read response
@ -609,7 +605,7 @@ fn test_h1_service_error() {
#[test] #[test]
fn test_h1_on_connect() { fn test_h1_on_connect() {
let mut srv = TestServer::new(|| { let srv = TestServer::new(|| {
HttpService::build() HttpService::build()
.on_connect(|_| 10usize) .on_connect(|_| 10usize)
.h1(|req: Request| { .h1(|req: Request| {
@ -618,6 +614,6 @@ fn test_h1_on_connect() {
}) })
}); });
let response = srv.block_on(srv.get("/").send()).unwrap(); let response = block_fn(|| srv.get("/").send()).unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
} }

View File

@ -65,7 +65,7 @@ rust-tls = { version = "0.16.0", package="rustls", optional = true }
actix-rt = "1.0.0-alpha.1" actix-rt = "1.0.0-alpha.1"
#actix-web = { version = "1.0.8", features=["ssl"] } #actix-web = { version = "1.0.8", features=["ssl"] }
actix-http = { version = "0.3.0-alpha.1", features=["openssl"] } actix-http = { version = "0.3.0-alpha.1", features=["openssl"] }
#actix-http-test = { version = "0.2.0", features=["ssl"] } actix-http-test = { version = "0.3.0-alpha.1", features=["openssl"] }
actix-utils = "0.5.0-alpha.1" actix-utils = "0.5.0-alpha.1"
actix-server = { version = "0.8.0-alpha.1", features=["openssl", "rustls"] } actix-server = { version = "0.8.0-alpha.1", features=["openssl", "rustls"] }
brotli2 = { version="0.3.2" } brotli2 = { version="0.3.2" }
@ -77,7 +77,9 @@ webpki = { version = "0.21" }
rus-tls = { version = "0.16.0", package="rustls", features = ["dangerous_configuration"] } rus-tls = { version = "0.16.0", package="rustls", features = ["dangerous_configuration"] }
[patch.crates-io] [patch.crates-io]
awc = { path = "." }
actix-http = { path = "../actix-http" } actix-http = { path = "../actix-http" }
actix-http-test = { path = "../test-server" }
actix-codec = { path = "../../actix-net/actix-codec" } actix-codec = { path = "../../actix-net/actix-codec" }
actix-connect = { path = "../../actix-net/actix-connect" } actix-connect = { path = "../../actix-net/actix-connect" }

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-http-test" name = "actix-http-test"
version = "0.2.5" version = "0.3.0-alpha.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix http test server" description = "Actix http test server"
readme = "README.md" readme = "README.md"

View File

@ -109,7 +109,7 @@ impl TestServer {
block_on(lazy(|_| { block_on(lazy(|_| {
Ok::<_, ()>(actix_connect::start_default_resolver()) Ok::<_, ()>(actix_connect::start_default_resolver())
})) }))
.unwrap(); .unwrap();
TestServerRuntime { TestServerRuntime {
addr, addr,