mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-30 00:14:58 +02:00
migrate integration testing to new crate (#2112)
This commit is contained in:
@ -3,14 +3,14 @@ extern crate tls_openssl as openssl;
|
||||
|
||||
#[cfg(any(unix, feature = "openssl"))]
|
||||
use {
|
||||
actix_web::{test, web, App, HttpResponse, HttpServer},
|
||||
actix_web::{web, App, HttpResponse, HttpServer},
|
||||
std::{sync::mpsc, thread, time::Duration},
|
||||
};
|
||||
|
||||
#[cfg(unix)]
|
||||
#[actix_rt::test]
|
||||
async fn test_start() {
|
||||
let addr = test::unused_addr();
|
||||
let addr = actix_test::unused_addr();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
thread::spawn(move || {
|
||||
@ -93,7 +93,7 @@ fn ssl_acceptor() -> openssl::ssl::SslAcceptorBuilder {
|
||||
async fn test_start_ssl() {
|
||||
use actix_web::HttpRequest;
|
||||
|
||||
let addr = test::unused_addr();
|
||||
let addr = actix_test::unused_addr();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
thread::spawn(move || {
|
||||
|
@ -31,7 +31,7 @@ use rand::{distributions::Alphanumeric, Rng};
|
||||
|
||||
use actix_web::dev::BodyEncoding;
|
||||
use actix_web::middleware::{Compress, NormalizePath, TrailingSlash};
|
||||
use actix_web::{dev, test, web, App, Error, HttpResponse};
|
||||
use actix_web::{dev, web, App, Error, HttpResponse};
|
||||
|
||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||
Hello World Hello World Hello World Hello World Hello World \
|
||||
@ -115,7 +115,7 @@ impl futures_core::stream::Stream for TestBody {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body() {
|
||||
let srv = test::start(|| {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||
});
|
||||
|
||||
@ -129,7 +129,7 @@ async fn test_body() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_gzip() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||
@ -156,7 +156,7 @@ async fn test_body_gzip() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_gzip2() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(web::resource("/").route(web::to(|| {
|
||||
@ -185,7 +185,7 @@ async fn test_body_gzip2() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_encoding_override() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(web::resource("/").route(web::to(|| {
|
||||
@ -248,7 +248,7 @@ async fn test_body_gzip_large() {
|
||||
let data = STR.repeat(10);
|
||||
let srv_data = data.clone();
|
||||
|
||||
let srv = test::start_with(test::config().h1(), move || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), move || {
|
||||
let data = srv_data.clone();
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
@ -286,7 +286,7 @@ async fn test_body_gzip_large_random() {
|
||||
.collect::<String>();
|
||||
let srv_data = data.clone();
|
||||
|
||||
let srv = test::start_with(test::config().h1(), move || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), move || {
|
||||
let data = srv_data.clone();
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
@ -318,7 +318,7 @@ async fn test_body_gzip_large_random() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_chunked_implicit() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||
.service(web::resource("/").route(web::get().to(move || {
|
||||
@ -352,7 +352,7 @@ async fn test_body_chunked_implicit() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_br_streaming() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Br))
|
||||
.service(web::resource("/").route(web::to(move || {
|
||||
@ -384,7 +384,7 @@ async fn test_body_br_streaming() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_head_binary() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::head().to(move || HttpResponse::Ok().body(STR))),
|
||||
)
|
||||
@ -405,7 +405,7 @@ async fn test_head_binary() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_no_chunking() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(web::resource("/").route(web::to(move || {
|
||||
HttpResponse::Ok()
|
||||
.no_chunking(STR.len() as u64)
|
||||
@ -424,7 +424,7 @@ async fn test_no_chunking() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_deflate() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Deflate))
|
||||
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
|
||||
@ -451,7 +451,7 @@ async fn test_body_deflate() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_body_brotli() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(Compress::new(ContentEncoding::Br))
|
||||
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
|
||||
@ -479,7 +479,7 @@ async fn test_body_brotli() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_encoding() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().wrap(Compress::default()).service(
|
||||
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||
)
|
||||
@ -504,7 +504,7 @@ async fn test_encoding() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_gzip_encoding() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||
)
|
||||
@ -530,7 +530,7 @@ async fn test_gzip_encoding() {
|
||||
#[actix_rt::test]
|
||||
async fn test_gzip_encoding_large() {
|
||||
let data = STR.repeat(10);
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||
)
|
||||
@ -561,7 +561,7 @@ async fn test_reading_gzip_encoding_large_random() {
|
||||
.map(char::from)
|
||||
.collect::<String>();
|
||||
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||
)
|
||||
@ -587,7 +587,7 @@ async fn test_reading_gzip_encoding_large_random() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_reading_deflate_encoding() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||
)
|
||||
@ -613,7 +613,7 @@ async fn test_reading_deflate_encoding() {
|
||||
#[actix_rt::test]
|
||||
async fn test_reading_deflate_encoding_large() {
|
||||
let data = STR.repeat(10);
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||
)
|
||||
@ -644,7 +644,7 @@ async fn test_reading_deflate_encoding_large_random() {
|
||||
.map(char::from)
|
||||
.collect::<String>();
|
||||
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||
)
|
||||
@ -670,7 +670,7 @@ async fn test_reading_deflate_encoding_large_random() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_brotli_encoding() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
||||
)
|
||||
@ -701,7 +701,7 @@ async fn test_brotli_encoding_large() {
|
||||
.map(char::from)
|
||||
.collect::<String>();
|
||||
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new().service(
|
||||
web::resource("/")
|
||||
.app_data(web::PayloadConfig::new(320_000))
|
||||
@ -732,13 +732,14 @@ async fn test_brotli_encoding_large() {
|
||||
#[actix_rt::test]
|
||||
async fn test_brotli_encoding_large_openssl() {
|
||||
let data = STR.repeat(10);
|
||||
let srv = test::start_with(test::config().openssl(openssl_config()), move || {
|
||||
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
||||
HttpResponse::Ok()
|
||||
.encoding(actix_web::http::ContentEncoding::Identity)
|
||||
.body(bytes)
|
||||
})))
|
||||
});
|
||||
let srv =
|
||||
actix_test::start_with(actix_test::config().openssl(openssl_config()), move || {
|
||||
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
||||
HttpResponse::Ok()
|
||||
.encoding(actix_web::http::ContentEncoding::Identity)
|
||||
.body(bytes)
|
||||
})))
|
||||
});
|
||||
|
||||
// body
|
||||
let mut e = BrotliEncoder::new(Vec::new(), 3);
|
||||
@ -794,7 +795,7 @@ mod plus_rustls {
|
||||
.map(char::from)
|
||||
.collect::<String>();
|
||||
|
||||
let srv = test::start_with(test::config().rustls(rustls_config()), || {
|
||||
let srv = actix_test::start_with(actix_test::config().rustls(rustls_config()), || {
|
||||
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
||||
HttpResponse::Ok()
|
||||
.encoding(actix_web::http::ContentEncoding::Identity)
|
||||
@ -827,7 +828,7 @@ mod plus_rustls {
|
||||
async fn test_server_cookies() {
|
||||
use actix_web::{http, HttpMessage};
|
||||
|
||||
let srv = test::start(|| {
|
||||
let srv = actix_test::start(|| {
|
||||
App::new().default_service(web::to(|| {
|
||||
HttpResponse::Ok()
|
||||
.cookie(
|
||||
@ -880,7 +881,7 @@ async fn test_server_cookies() {
|
||||
async fn test_slow_request() {
|
||||
use std::net;
|
||||
|
||||
let srv = test::start_with(test::config().client_timeout(200), || {
|
||||
let srv = actix_test::start_with(actix_test::config().client_timeout(200), || {
|
||||
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok)))
|
||||
});
|
||||
|
||||
@ -898,7 +899,7 @@ async fn test_slow_request() {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_normalize() {
|
||||
let srv = test::start_with(test::config().h1(), || {
|
||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||
App::new()
|
||||
.wrap(NormalizePath::new(TrailingSlash::Trim))
|
||||
.service(web::resource("/one").route(web::to(|| HttpResponse::Ok().finish())))
|
||||
@ -907,3 +908,51 @@ async fn test_normalize() {
|
||||
let response = srv.get("/one/").send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_data_drop() {
|
||||
use std::sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
struct TestData(Arc<AtomicUsize>);
|
||||
|
||||
impl TestData {
|
||||
fn new(inner: Arc<AtomicUsize>) -> Self {
|
||||
let _ = inner.fetch_add(1, Ordering::SeqCst);
|
||||
Self(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for TestData {
|
||||
fn clone(&self) -> Self {
|
||||
let inner = self.0.clone();
|
||||
let _ = inner.fetch_add(1, Ordering::SeqCst);
|
||||
Self(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TestData {
|
||||
fn drop(&mut self) {
|
||||
self.0.fetch_sub(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
let num = Arc::new(AtomicUsize::new(0));
|
||||
let data = TestData::new(num.clone());
|
||||
assert_eq!(num.load(Ordering::SeqCst), 1);
|
||||
|
||||
let srv = actix_test::start(move || {
|
||||
let data = data.clone();
|
||||
|
||||
App::new()
|
||||
.data(data)
|
||||
.service(web::resource("/").to(|_data: web::Data<TestData>| async { "ok" }))
|
||||
});
|
||||
|
||||
assert!(srv.get("/").send().await.unwrap().status().is_success());
|
||||
srv.stop().await;
|
||||
|
||||
assert_eq!(num.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user