1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-23 16:21:06 +01:00

refactor http-test server

This commit is contained in:
Nikolay Kim 2019-12-12 23:08:38 +06:00
parent db1d6b7963
commit 4937c9f9c2
17 changed files with 252 additions and 296 deletions

View File

@ -43,7 +43,7 @@ members = [
]
[features]
default = ["compress", "fail"]
default = ["compress", "failure"]
# content-encoding support
compress = ["actix-http/compress", "awc/compress"]
@ -51,7 +51,7 @@ compress = ["actix-http/compress", "awc/compress"]
# sessions feature, session require "ring" crate and c compiler
secure-cookies = ["actix-http/secure-cookies"]
fail = ["actix-http/fail"]
failure = ["actix-http/failure"]
# openssl
openssl = ["actix-tls/openssl", "awc/openssl", "open-ssl"]

View File

@ -1,6 +1,6 @@
use actix_codec::{AsyncRead, AsyncWrite};
use actix_http::{body, http::StatusCode, ws, Error, HttpService, Response};
use actix_http_test::TestServer;
use actix_http_test::test_server;
use actix_service::{pipeline_factory, IntoServiceFactory, ServiceFactory};
use actix_utils::framed::Dispatcher;
use bytes::Bytes;
@ -40,7 +40,7 @@ async fn service(msg: ws::Frame) -> Result<ws::Message, Error> {
#[actix_rt::test]
async fn test_simple() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.upgrade(
FramedApp::new().service(FramedRoute::get("/index.html").to(ws_service)),
@ -94,7 +94,7 @@ async fn test_simple() {
#[actix_rt::test]
async fn test_service() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
pipeline_factory(actix_http::h1::OneRequest::new().map_err(|_| ())).and_then(
pipeline_factory(
pipeline_factory(VerifyWebSockets::default())

View File

@ -34,7 +34,7 @@ rustls = ["actix-tls/rustls", "actix-connect/rustls"]
compress = ["flate2", "brotli"]
# failure integration. actix does not use failure anymore
fail = ["failure"]
failure = ["fail-ure"]
# support for secure cookies
secure-cookies = ["ring"]
@ -85,7 +85,7 @@ brotli = { version = "3.3.0", optional = true }
flate2 = { version = "1.0.13", optional = true }
# optional deps
failure = { version = "0.1.5", optional = true }
fail-ure = { version = "0.1.5", package="failure", optional = true }
[dev-dependencies]
actix-server = "1.0.0"

View File

@ -957,13 +957,9 @@ where
InternalError::new(err, StatusCode::NETWORK_AUTHENTICATION_REQUIRED).into()
}
#[cfg(feature = "fail")]
mod failure_integration {
use super::*;
/// Compatibility for `failure::Error`
impl ResponseError for failure::Error {}
}
#[cfg(feature = "failure")]
/// Compatibility for `failure::Error`
impl ResponseError for fail_ure::Error {}
#[cfg(test)]
mod tests {

View File

@ -3,7 +3,7 @@ use bytes::Bytes;
use futures::future::{self, ok};
use actix_http::{http, HttpService, Request, Response};
use actix_http_test::TestServer;
use actix_http_test::test_server;
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
@ -29,7 +29,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
#[actix_rt::test]
async fn test_h1_v2() {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
@ -56,7 +56,7 @@ async fn test_h1_v2() {
#[actix_rt::test]
async fn test_connection_close() {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
@ -69,7 +69,7 @@ async fn test_connection_close() {
#[actix_rt::test]
async fn test_with_query_parameter() {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.finish(|req: Request| {
if req.uri().query().unwrap().contains("qp=") {

View File

@ -1,7 +1,7 @@
#![cfg(feature = "openssl")]
use std::io;
use actix_http_test::TestServer;
use actix_http_test::test_server;
use actix_service::{fn_service, ServiceFactory};
use bytes::{Bytes, BytesMut};
@ -62,7 +62,7 @@ fn ssl_acceptor() -> SslAcceptor {
#[actix_rt::test]
async fn test_h2() -> io::Result<()> {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, Error>(Response::Ok().finish()))
.openssl(ssl_acceptor())
@ -76,7 +76,7 @@ async fn test_h2() -> io::Result<()> {
#[actix_rt::test]
async fn test_h2_1() -> io::Result<()> {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.finish(|req: Request| {
assert!(req.peer_addr().is_some());
@ -95,7 +95,7 @@ async fn test_h2_1() -> io::Result<()> {
#[actix_rt::test]
async fn test_h2_body() -> io::Result<()> {
let data = "HELLOWORLD".to_owned().repeat(64 * 1024);
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|mut req: Request<_>| {
async move {
@ -117,7 +117,7 @@ async fn test_h2_body() -> io::Result<()> {
#[actix_rt::test]
async fn test_h2_content_length() {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.h2(|req: Request| {
let indx: usize = req.uri().path()[1..].parse().unwrap();
@ -168,7 +168,7 @@ async fn test_h2_headers() {
let data = STR.repeat(10);
let data2 = data.clone();
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
let data = data.clone();
HttpService::build().h2(move |_| {
let mut builder = Response::Ok();
@ -228,7 +228,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
#[actix_rt::test]
async fn test_h2_body2() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
.openssl(ssl_acceptor())
@ -245,7 +245,7 @@ async fn test_h2_body2() {
#[actix_rt::test]
async fn test_h2_head_empty() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
.openssl(ssl_acceptor())
@ -268,7 +268,7 @@ async fn test_h2_head_empty() {
#[actix_rt::test]
async fn test_h2_head_binary() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| {
ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR))
@ -292,7 +292,7 @@ async fn test_h2_head_binary() {
#[actix_rt::test]
async fn test_h2_head_binary2() {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
.openssl(ssl_acceptor())
@ -310,7 +310,7 @@ async fn test_h2_head_binary2() {
#[actix_rt::test]
async fn test_h2_body_length() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| {
let body = once(ok(Bytes::from_static(STR.as_ref())));
@ -332,7 +332,7 @@ async fn test_h2_body_length() {
#[actix_rt::test]
async fn test_h2_body_chunked_explicit() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| {
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
@ -359,7 +359,7 @@ async fn test_h2_body_chunked_explicit() {
#[actix_rt::test]
async fn test_h2_response_http_error_handling() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(fn_service(|_| {
let broken_header = Bytes::from_static(b"\0\0\0");
@ -383,7 +383,7 @@ async fn test_h2_response_http_error_handling() {
#[actix_rt::test]
async fn test_h2_service_error() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| err::<Response, Error>(ErrorBadRequest("error")))
.openssl(ssl_acceptor())
@ -400,7 +400,7 @@ async fn test_h2_service_error() {
#[actix_rt::test]
async fn test_h2_on_connect() {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.on_connect(|_| 10usize)
.h2(|req: Request| {

View File

@ -3,7 +3,7 @@ use actix_http::error::PayloadError;
use actix_http::http::header::{self, HeaderName, HeaderValue};
use actix_http::http::{Method, StatusCode, Version};
use actix_http::{body, error, Error, HttpService, Request, Response};
use actix_http_test::TestServer;
use actix_http_test::test_server;
use actix_service::{fn_factory_with_config, fn_service};
use bytes::{Bytes, BytesMut};
@ -41,7 +41,7 @@ fn ssl_acceptor() -> RustlsServerConfig {
#[actix_rt::test]
async fn test_h1() -> io::Result<()> {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.h1(|_| future::ok::<_, Error>(Response::Ok().finish()))
.rustls(ssl_acceptor())
@ -54,7 +54,7 @@ async fn test_h1() -> io::Result<()> {
#[actix_rt::test]
async fn test_h2() -> io::Result<()> {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.h2(|_| future::ok::<_, Error>(Response::Ok().finish()))
.rustls(ssl_acceptor())
@ -67,7 +67,7 @@ async fn test_h2() -> io::Result<()> {
#[actix_rt::test]
async fn test_h1_1() -> io::Result<()> {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.h1(|req: Request| {
assert!(req.peer_addr().is_some());
@ -84,7 +84,7 @@ async fn test_h1_1() -> io::Result<()> {
#[actix_rt::test]
async fn test_h2_1() -> io::Result<()> {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.finish(|req: Request| {
assert!(req.peer_addr().is_some());
@ -102,7 +102,7 @@ async fn test_h2_1() -> io::Result<()> {
#[actix_rt::test]
async fn test_h2_body1() -> io::Result<()> {
let data = "HELLOWORLD".to_owned().repeat(64 * 1024);
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|mut req: Request<_>| {
async move {
@ -123,7 +123,7 @@ async fn test_h2_body1() -> io::Result<()> {
#[actix_rt::test]
async fn test_h2_content_length() {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.h2(|req: Request| {
let indx: usize = req.uri().path()[1..].parse().unwrap();
@ -172,7 +172,7 @@ async fn test_h2_headers() {
let data = STR.repeat(10);
let data2 = data.clone();
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
let data = data.clone();
HttpService::build().h2(move |_| {
let mut config = Response::Ok();
@ -231,7 +231,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
#[actix_rt::test]
async fn test_h2_body2() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
.rustls(ssl_acceptor())
@ -247,7 +247,7 @@ async fn test_h2_body2() {
#[actix_rt::test]
async fn test_h2_head_empty() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
.rustls(ssl_acceptor())
@ -272,7 +272,7 @@ async fn test_h2_head_empty() {
#[actix_rt::test]
async fn test_h2_head_binary() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| {
ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR))
@ -298,7 +298,7 @@ async fn test_h2_head_binary() {
#[actix_rt::test]
async fn test_h2_head_binary2() {
let srv = TestServer::start(move || {
let srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
.rustls(ssl_acceptor())
@ -318,7 +318,7 @@ async fn test_h2_head_binary2() {
#[actix_rt::test]
async fn test_h2_body_length() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| {
let body = once(ok(Bytes::from_static(STR.as_ref())));
@ -339,7 +339,7 @@ async fn test_h2_body_length() {
#[actix_rt::test]
async fn test_h2_body_chunked_explicit() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| {
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
@ -365,7 +365,7 @@ async fn test_h2_body_chunked_explicit() {
#[actix_rt::test]
async fn test_h2_response_http_error_handling() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(fn_factory_with_config(|_: ()| {
ok::<_, ()>(fn_service(|_| {
@ -390,7 +390,7 @@ async fn test_h2_response_http_error_handling() {
#[actix_rt::test]
async fn test_h2_service_error() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| err::<Response, Error>(error::ErrorBadRequest("error")))
.rustls(ssl_acceptor())
@ -406,7 +406,7 @@ async fn test_h2_service_error() {
#[actix_rt::test]
async fn test_h1_service_error() {
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
HttpService::build()
.h1(|_| err::<Response, Error>(error::ErrorBadRequest("error")))
.rustls(ssl_acceptor())

View File

@ -2,7 +2,7 @@ use std::io::{Read, Write};
use std::time::Duration;
use std::{net, thread};
use actix_http_test::TestServer;
use actix_http_test::test_server;
use actix_rt::time::delay_for;
use actix_service::fn_service;
use bytes::Bytes;
@ -17,7 +17,7 @@ use actix_http::{
#[actix_rt::test]
async fn test_h1() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.keep_alive(KeepAlive::Disabled)
.client_timeout(1000)
@ -35,7 +35,7 @@ async fn test_h1() {
#[actix_rt::test]
async fn test_h1_2() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.keep_alive(KeepAlive::Disabled)
.client_timeout(1000)
@ -54,7 +54,7 @@ async fn test_h1_2() {
#[actix_rt::test]
async fn test_expect_continue() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.expect(fn_service(|req: Request| {
if req.head().uri.query() == Some("yes=") {
@ -82,7 +82,7 @@ async fn test_expect_continue() {
#[actix_rt::test]
async fn test_expect_continue_h1() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.expect(fn_service(|req: Request| {
delay_for(Duration::from_millis(20)).then(move |_| {
@ -115,7 +115,7 @@ async fn test_chunked_payload() {
let chunk_sizes = vec![32768, 32, 32768];
let total_size: usize = chunk_sizes.iter().sum();
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.h1(fn_service(|mut request: Request| {
request
@ -167,7 +167,7 @@ async fn test_chunked_payload() {
#[actix_rt::test]
async fn test_slow_request() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.client_timeout(100)
.finish(|_| future::ok::<_, ()>(Response::Ok().finish()))
@ -183,7 +183,7 @@ async fn test_slow_request() {
#[actix_rt::test]
async fn test_http1_malformed_request() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
@ -198,7 +198,7 @@ async fn test_http1_malformed_request() {
#[actix_rt::test]
async fn test_http1_keepalive() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
@ -218,7 +218,7 @@ async fn test_http1_keepalive() {
#[actix_rt::test]
async fn test_http1_keepalive_timeout() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.keep_alive(1)
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
@ -239,7 +239,7 @@ async fn test_http1_keepalive_timeout() {
#[actix_rt::test]
async fn test_http1_keepalive_close() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
@ -259,7 +259,7 @@ async fn test_http1_keepalive_close() {
#[actix_rt::test]
async fn test_http10_keepalive_default_close() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
@ -278,7 +278,7 @@ async fn test_http10_keepalive_default_close() {
#[actix_rt::test]
async fn test_http10_keepalive() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
.tcp()
@ -304,7 +304,7 @@ async fn test_http10_keepalive() {
#[actix_rt::test]
async fn test_http1_keepalive_disabled() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.keep_alive(KeepAlive::Disabled)
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
@ -329,7 +329,7 @@ async fn test_content_length() {
StatusCode,
};
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.h1(|req: Request| {
let indx: usize = req.uri().path()[1..].parse().unwrap();
@ -373,7 +373,7 @@ async fn test_h1_headers() {
let data = STR.repeat(10);
let data2 = data.clone();
let mut srv = TestServer::start(move || {
let mut srv = test_server(move || {
let data = data.clone();
HttpService::build().h1(move |_| {
let mut builder = Response::Ok();
@ -431,7 +431,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
#[actix_rt::test]
async fn test_h1_body() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
@ -447,7 +447,7 @@ async fn test_h1_body() {
#[actix_rt::test]
async fn test_h1_head_empty() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
@ -471,7 +471,7 @@ async fn test_h1_head_empty() {
#[actix_rt::test]
async fn test_h1_head_binary() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| {
ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR))
@ -497,7 +497,7 @@ async fn test_h1_head_binary() {
#[actix_rt::test]
async fn test_h1_head_binary2() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
.tcp()
@ -517,7 +517,7 @@ async fn test_h1_head_binary2() {
#[actix_rt::test]
async fn test_h1_body_length() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| {
let body = once(ok(Bytes::from_static(STR.as_ref())));
@ -538,7 +538,7 @@ async fn test_h1_body_length() {
#[actix_rt::test]
async fn test_h1_body_chunked_explicit() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| {
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
@ -572,7 +572,7 @@ async fn test_h1_body_chunked_explicit() {
#[actix_rt::test]
async fn test_h1_body_chunked_implicit() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| {
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
@ -600,7 +600,7 @@ async fn test_h1_body_chunked_implicit() {
#[actix_rt::test]
async fn test_h1_response_http_error_handling() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.h1(fn_service(|_| {
let broken_header = Bytes::from_static(b"\0\0\0");
@ -623,7 +623,7 @@ async fn test_h1_response_http_error_handling() {
#[actix_rt::test]
async fn test_h1_service_error() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| future::err::<Response, Error>(error::ErrorBadRequest("error")))
.tcp()
@ -639,7 +639,7 @@ async fn test_h1_service_error() {
#[actix_rt::test]
async fn test_h1_on_connect() {
let srv = TestServer::start(|| {
let srv = test_server(|| {
HttpService::build()
.on_connect(|_| 10usize)
.h1(|req: Request| {

View File

@ -1,6 +1,6 @@
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_http::{body, h1, ws, Error, HttpService, Request, Response};
use actix_http_test::TestServer;
use actix_http_test::test_server;
use actix_utils::framed::Dispatcher;
use bytes::Bytes;
use futures::future;
@ -37,7 +37,7 @@ async fn service(msg: ws::Frame) -> Result<ws::Message, Error> {
#[actix_rt::test]
async fn test_simple() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.upgrade(actix_service::fn_service(ws_service))
.finish(|_| future::ok::<_, ()>(Response::NotFound()))

View File

@ -1,6 +1,6 @@
[package]
name = "actix-web-codegen"
version = "0.2.0-alpha.2"
version = "0.2.0"
description = "Actix web proc macros"
readme = "README.md"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
@ -17,8 +17,6 @@ syn = { version = "^1", features = ["full", "parsing"] }
proc-macro2 = "^1"
[dev-dependencies]
actix-rt = { version = "1.0.0-alpha.2" }
actix-web = { version = "2.0.0-alpha.2" }
actix-http = { version = "1.0.0-alpha.3", features=["openssl"] }
actix-http-test = { version = "1.0.0-alpha.3", features=["openssl"] }
actix-rt = { version = "1.0.0" }
actix-web = { version = "2.0.0-alpha.4" }
futures = { version = "0.3.1" }

View File

@ -1,11 +1,9 @@
use actix_http::HttpService;
use actix_http_test::TestServer;
use actix_web::{http, web::Path, App, HttpResponse, Responder};
use actix_web::{http, test, web::Path, App, HttpResponse, Responder};
use actix_web_codegen::{connect, delete, get, head, options, patch, post, put, trace};
use futures::{future, Future};
#[get("/test")]
async fn test() -> impl Responder {
async fn test_handler() -> impl Responder {
HttpResponse::Ok()
}
@ -71,14 +69,11 @@ async fn get_param_test(_: Path<String>) -> impl Responder {
#[actix_rt::test]
async fn test_params() {
let srv = TestServer::start(|| {
HttpService::new(
App::new()
.service(get_param_test)
.service(put_param_test)
.service(delete_param_test),
)
.tcp()
let srv = test::start(|| {
App::new()
.service(get_param_test)
.service(put_param_test)
.service(delete_param_test)
});
let request = srv.request(http::Method::GET, srv.url("/test/it"));
@ -96,19 +91,16 @@ async fn test_params() {
#[actix_rt::test]
async fn test_body() {
let srv = TestServer::start(|| {
HttpService::new(
App::new()
.service(post_test)
.service(put_test)
.service(head_test)
.service(connect_test)
.service(options_test)
.service(trace_test)
.service(patch_test)
.service(test),
)
.tcp()
let srv = test::start(|| {
App::new()
.service(post_test)
.service(put_test)
.service(head_test)
.service(connect_test)
.service(options_test)
.service(trace_test)
.service(patch_test)
.service(test_handler)
});
let request = srv.request(http::Method::GET, srv.url("/test"));
let response = request.send().await.unwrap();
@ -151,8 +143,7 @@ async fn test_body() {
#[actix_rt::test]
async fn test_auto_async() {
let srv =
TestServer::start(|| HttpService::new(App::new().service(auto_async)).tcp());
let srv = test::start(|| App::new().service(auto_async));
let request = srv.request(http::Method::GET, srv.url("/test"));
let response = request.send().await.unwrap();

View File

@ -13,11 +13,13 @@ use futures::future::ok;
use rand::Rng;
use actix_http::HttpService;
use actix_http_test::TestServer;
use actix_http_test::test_server;
use actix_service::pipeline_factory;
use actix_web::http::Cookie;
use actix_web::middleware::{BodyEncoding, Compress};
use actix_web::{http::header, web, App, Error, HttpMessage, HttpRequest, HttpResponse};
use actix_web::{
http::header, test, web, App, Error, HttpMessage, HttpRequest, HttpResponse,
};
use awc::error::SendRequestError;
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
@ -44,13 +46,10 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
#[actix_rt::test]
async fn test_simple() {
let srv =
TestServer::start(|| {
HttpService::new(App::new().service(
web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))),
))
.tcp()
});
let srv = test::start(|| {
App::new()
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
});
let request = srv.get("/").header("x-test", "111").send();
let mut response = request.await.unwrap();
@ -74,11 +73,10 @@ async fn test_simple() {
#[actix_rt::test]
async fn test_json() {
let srv = TestServer::start(|| {
HttpService::new(App::new().service(
let srv = test::start(|| {
App::new().service(
web::resource("/").route(web::to(|_: web::Json<String>| HttpResponse::Ok())),
))
.tcp()
)
});
let request = srv
@ -91,11 +89,10 @@ async fn test_json() {
#[actix_rt::test]
async fn test_form() {
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(
|_: web::Form<HashMap<String, String>>| HttpResponse::Ok(),
))))
.tcp()
)))
});
let mut data = HashMap::new();
@ -108,14 +105,13 @@ async fn test_form() {
#[actix_rt::test]
async fn test_timeout() {
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(|| {
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|| {
async {
actix_rt::time::delay_for(Duration::from_millis(200)).await;
Ok::<_, Error>(HttpResponse::Ok().body(STR))
}
}))))
.tcp()
})))
});
let connector = awc::Connector::new()
@ -139,14 +135,13 @@ async fn test_timeout() {
#[actix_rt::test]
async fn test_timeout_override() {
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(|| {
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|| {
async {
actix_rt::time::delay_for(Duration::from_millis(200)).await;
Ok::<_, Error>(HttpResponse::Ok().body(STR))
}
}))))
.tcp()
})))
});
let client = awc::Client::build()
@ -167,7 +162,7 @@ async fn test_connection_reuse() {
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
let srv = TestServer::start(move || {
let srv = test_server(move || {
let num2 = num2.clone();
pipeline_factory(move |io| {
num2.fetch_add(1, Ordering::Relaxed);
@ -203,7 +198,7 @@ async fn test_connection_force_close() {
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
let srv = TestServer::start(move || {
let srv = test_server(move || {
let num2 = num2.clone();
pipeline_factory(move |io| {
num2.fetch_add(1, Ordering::Relaxed);
@ -239,7 +234,7 @@ async fn test_connection_server_close() {
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
let srv = TestServer::start(move || {
let srv = test_server(move || {
let num2 = num2.clone();
pipeline_factory(move |io| {
num2.fetch_add(1, Ordering::Relaxed);
@ -277,7 +272,7 @@ async fn test_connection_wait_queue() {
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
let srv = TestServer::start(move || {
let srv = test_server(move || {
let num2 = num2.clone();
pipeline_factory(move |io| {
num2.fetch_add(1, Ordering::Relaxed);
@ -321,7 +316,7 @@ async fn test_connection_wait_queue_force_close() {
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
let srv = TestServer::start(move || {
let srv = test_server(move || {
let num2 = num2.clone();
pipeline_factory(move |io| {
num2.fetch_add(1, Ordering::Relaxed);
@ -365,17 +360,14 @@ async fn test_connection_wait_queue_force_close() {
#[actix_rt::test]
async fn test_with_query_parameter() {
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").to(
|req: HttpRequest| {
if req.query_string().contains("qp") {
HttpResponse::Ok()
} else {
HttpResponse::BadRequest()
}
},
)))
.tcp()
let srv = test::start(|| {
App::new().service(web::resource("/").to(|req: HttpRequest| {
if req.query_string().contains("qp") {
HttpResponse::Ok()
} else {
HttpResponse::BadRequest()
}
}))
});
let res = awc::Client::new()
@ -388,15 +380,14 @@ async fn test_with_query_parameter() {
#[actix_rt::test]
async fn test_no_decompress() {
let srv = TestServer::start(|| {
HttpService::new(App::new().wrap(Compress::default()).service(
web::resource("/").route(web::to(|| {
let srv = test::start(|| {
App::new()
.wrap(Compress::default())
.service(web::resource("/").route(web::to(|| {
let mut res = HttpResponse::Ok().body(STR);
res.encoding(header::ContentEncoding::Gzip);
res
})),
))
.tcp()
})))
});
let mut res = awc::Client::new()
@ -433,8 +424,8 @@ async fn test_no_decompress() {
#[actix_rt::test]
async fn test_client_gzip_encoding() {
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(|| {
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|| {
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let data = e.finish().unwrap();
@ -442,8 +433,7 @@ async fn test_client_gzip_encoding() {
HttpResponse::Ok()
.header("content-encoding", "gzip")
.body(data)
}))))
.tcp()
})))
});
// client request
@ -457,8 +447,8 @@ async fn test_client_gzip_encoding() {
#[actix_rt::test]
async fn test_client_gzip_encoding_large() {
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(|| {
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|| {
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.repeat(10).as_ref()).unwrap();
let data = e.finish().unwrap();
@ -466,8 +456,7 @@ async fn test_client_gzip_encoding_large() {
HttpResponse::Ok()
.header("content-encoding", "gzip")
.body(data)
}))))
.tcp()
})))
});
// client request
@ -486,18 +475,15 @@ async fn test_client_gzip_encoding_large_random() {
.take(100_000)
.collect::<String>();
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(
|data: Bytes| {
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(&data).unwrap();
let data = e.finish().unwrap();
HttpResponse::Ok()
.header("content-encoding", "gzip")
.body(data)
},
))))
.tcp()
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(&data).unwrap();
let data = e.finish().unwrap();
HttpResponse::Ok()
.header("content-encoding", "gzip")
.body(data)
})))
});
// client request
@ -511,18 +497,15 @@ async fn test_client_gzip_encoding_large_random() {
#[actix_rt::test]
async fn test_client_brotli_encoding() {
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(
|data: Bytes| {
let mut e = CompressorWriter::new(Vec::new(), 0, 5, 0);
e.write_all(&data).unwrap();
let data = e.into_inner();
HttpResponse::Ok()
.header("content-encoding", "br")
.body(data)
},
))))
.tcp()
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
let mut e = CompressorWriter::new(Vec::new(), 0, 5, 0);
e.write_all(&data).unwrap();
let data = e.into_inner();
HttpResponse::Ok()
.header("content-encoding", "br")
.body(data)
})))
});
// client request
@ -541,18 +524,15 @@ async fn test_client_brotli_encoding_large_random() {
.take(70_000)
.collect::<String>();
let srv = TestServer::start(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(
|data: Bytes| {
let mut e = CompressorWriter::new(Vec::new(), 0, 5, 0);
e.write_all(&data).unwrap();
let data = e.into_inner();
HttpResponse::Ok()
.header("content-encoding", "br")
.body(data)
},
))))
.tcp()
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
let mut e = CompressorWriter::new(Vec::new(), 0, 5, 0);
e.write_all(&data).unwrap();
let data = e.into_inner();
HttpResponse::Ok()
.header("content-encoding", "br")
.body(data)
})))
});
// client request
@ -688,11 +668,11 @@ async fn test_client_cookie_handling() {
let cookie1b = cookie1.clone();
let cookie2b = cookie2.clone();
let srv = TestServer::start(move || {
let srv = test::start(move || {
let cookie1 = cookie1b.clone();
let cookie2 = cookie2b.clone();
HttpService::new(App::new().route(
App::new().route(
"/",
web::to(move |req: HttpRequest| {
let cookie1 = cookie1.clone();
@ -730,8 +710,7 @@ async fn test_client_cookie_handling() {
}
}
}),
))
.tcp()
)
});
let request = srv.get("/").cookie(cookie1.clone()).cookie(cookie2.clone());
@ -775,8 +754,8 @@ async fn test_client_cookie_handling() {
#[actix_rt::test]
async fn client_basic_auth() {
let srv = TestServer::start(|| {
HttpService::new(App::new().route(
let srv = test::start(|| {
App::new().route(
"/",
web::to(|req: HttpRequest| {
if req
@ -792,8 +771,7 @@ async fn client_basic_auth() {
HttpResponse::BadRequest()
}
}),
))
.tcp()
)
});
// set authorization header to Basic <base64 encoded username:password>
@ -804,8 +782,8 @@ async fn client_basic_auth() {
#[actix_rt::test]
async fn client_bearer_auth() {
let srv = TestServer::start(|| {
HttpService::new(App::new().route(
let srv = test::start(|| {
App::new().route(
"/",
web::to(|req: HttpRequest| {
if req
@ -821,8 +799,7 @@ async fn client_bearer_auth() {
HttpResponse::BadRequest()
}
}),
))
.tcp()
)
});
// set authorization header to Bearer <token>

View File

@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use actix_http::HttpService;
use actix_http_test::TestServer;
use actix_http_test::test_server;
use actix_service::{pipeline_factory, ServiceFactory};
use actix_web::http::Version;
use actix_web::{web, App, HttpResponse};
@ -54,7 +54,7 @@ async fn _test_connection_reuse_h2() {
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
let srv = TestServer::start(move || {
let srv = test_server(move || {
let num2 = num2.clone();
pipeline_factory(move |io| {
num2.fetch_add(1, Ordering::Relaxed);

View File

@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use actix_http::HttpService;
use actix_http_test::TestServer;
use actix_http_test::test_server;
use actix_service::{pipeline_factory, ServiceFactory};
use actix_web::http::Version;
use actix_web::{web, App, HttpResponse};
@ -36,7 +36,7 @@ async fn test_connection_reuse_h2() {
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
let srv = TestServer::start(move || {
let srv = test_server(move || {
let num2 = num2.clone();
pipeline_factory(move |io| {
num2.fetch_add(1, Ordering::Relaxed);

View File

@ -2,7 +2,7 @@ use std::io;
use actix_codec::Framed;
use actix_http::{body::BodySize, h1, ws, Error, HttpService, Request, Response};
use actix_http_test::TestServer;
use actix_http_test::test_server;
use bytes::Bytes;
use futures::future::ok;
use futures::{SinkExt, StreamExt};
@ -21,7 +21,7 @@ async fn ws_service(req: ws::Frame) -> Result<ws::Message, io::Error> {
#[actix_rt::test]
async fn test_simple() {
let mut srv = TestServer::start(|| {
let mut srv = test_server(|| {
HttpService::build()
.upgrade(|(req, mut framed): (Request, Framed<_, _>)| {
async move {

View File

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

View File

@ -13,7 +13,7 @@ use net2::TcpBuilder;
pub use actix_testing::*;
/// The `TestServer` type.
/// Start test server
///
/// `TestServer` is very simple test server that simplify process of writing
/// integration tests cases for actix web applications.
@ -43,88 +43,82 @@ pub use actix_testing::*;
/// assert!(response.status().is_success());
/// }
/// ```
pub struct TestServer;
pub fn test_server<F: ServiceFactory<TcpStream>>(factory: F) -> TestServer {
let (tx, rx) = mpsc::channel();
// run server in separate thread
thread::spawn(move || {
let sys = System::new("actix-test-server");
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
let local_addr = tcp.local_addr().unwrap();
Server::build()
.listen("test", tcp, factory)?
.workers(1)
.disable_signals()
.start();
tx.send((System::current(), local_addr)).unwrap();
sys.run()
});
let (system, addr) = rx.recv().unwrap();
let client = {
let connector = {
#[cfg(feature = "openssl")]
{
use open_ssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
builder.set_verify(SslVerifyMode::NONE);
let _ = builder
.set_alpn_protos(b"\x02h2\x08http/1.1")
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
Connector::new()
.conn_lifetime(time::Duration::from_secs(0))
.timeout(time::Duration::from_millis(3000))
.ssl(builder.build())
.finish()
}
#[cfg(not(feature = "openssl"))]
{
Connector::new()
.conn_lifetime(time::Duration::from_secs(0))
.timeout(time::Duration::from_millis(3000))
.finish()
}
};
Client::build().connector(connector).finish()
};
actix_connect::start_default_resolver();
TestServer {
addr,
client,
system,
}
}
/// Get first available unused address
pub fn unused_addr() -> net::SocketAddr {
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = TcpBuilder::new_v4().unwrap();
socket.bind(&addr).unwrap();
socket.reuse_address(true).unwrap();
let tcp = socket.to_tcp_listener().unwrap();
tcp.local_addr().unwrap()
}
/// Test server controller
pub struct TestServerRuntime {
pub struct TestServer {
addr: net::SocketAddr,
client: Client,
system: System,
}
impl TestServer {
#[allow(clippy::new_ret_no_self)]
/// Start new test server with application factory
pub fn start<F: ServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
let (tx, rx) = mpsc::channel();
// run server in separate thread
thread::spawn(move || {
let sys = System::new("actix-test-server");
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
let local_addr = tcp.local_addr().unwrap();
Server::build()
.listen("test", tcp, factory)?
.workers(1)
.disable_signals()
.start();
tx.send((System::current(), local_addr)).unwrap();
sys.run()
});
let (system, addr) = rx.recv().unwrap();
let client = {
let connector = {
#[cfg(feature = "openssl")]
{
use open_ssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
builder.set_verify(SslVerifyMode::NONE);
let _ = builder
.set_alpn_protos(b"\x02h2\x08http/1.1")
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
Connector::new()
.conn_lifetime(time::Duration::from_secs(0))
.timeout(time::Duration::from_millis(3000))
.ssl(builder.build())
.finish()
}
#[cfg(not(feature = "openssl"))]
{
Connector::new()
.conn_lifetime(time::Duration::from_secs(0))
.timeout(time::Duration::from_millis(3000))
.finish()
}
};
Client::build().connector(connector).finish()
};
actix_connect::start_default_resolver();
TestServerRuntime {
addr,
client,
system,
}
}
/// Get first available unused address
pub fn unused_addr() -> net::SocketAddr {
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = TcpBuilder::new_v4().unwrap();
socket.bind(&addr).unwrap();
socket.reuse_address(true).unwrap();
let tcp = socket.to_tcp_listener().unwrap();
tcp.local_addr().unwrap()
}
}
impl TestServerRuntime {
/// Construct test server url
pub fn addr(&self) -> net::SocketAddr {
self.addr
@ -258,7 +252,7 @@ impl TestServerRuntime {
}
}
impl Drop for TestServerRuntime {
impl Drop for TestServer {
fn drop(&mut self) {
self.stop()
}