mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-26 06:57:43 +02:00
update to latest actix-net
This commit is contained in:
@ -4,8 +4,7 @@ use std::{net, thread};
|
||||
|
||||
use actix_http_test::TestServer;
|
||||
use actix_rt::time::delay_for;
|
||||
use actix_server_config::ServerConfig;
|
||||
use actix_service::{factory_fn_cfg, pipeline, service_fn, ServiceFactory};
|
||||
use actix_service::service_fn;
|
||||
use bytes::Bytes;
|
||||
use futures::future::{self, err, ok, ready, FutureExt};
|
||||
use futures::stream::{once, StreamExt};
|
||||
@ -27,6 +26,7 @@ async fn test_h1() {
|
||||
assert!(req.peer_addr().is_some());
|
||||
future::ok::<_, ()>(Response::Ok().finish())
|
||||
})
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -45,7 +45,7 @@ async fn test_h1_2() {
|
||||
assert_eq!(req.version(), http::Version::HTTP_11);
|
||||
future::ok::<_, ()>(Response::Ok().finish())
|
||||
})
|
||||
.map(|_| ())
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -64,6 +64,7 @@ async fn test_expect_continue() {
|
||||
}
|
||||
}))
|
||||
.finish(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -92,7 +93,8 @@ async fn test_expect_continue_h1() {
|
||||
}
|
||||
})
|
||||
}))
|
||||
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.h1(service_fn(|_| future::ok::<_, ()>(Response::Ok().finish())))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -114,18 +116,20 @@ async fn test_chunked_payload() {
|
||||
let total_size: usize = chunk_sizes.iter().sum();
|
||||
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::build().h1(service_fn(|mut request: Request| {
|
||||
request
|
||||
.take_payload()
|
||||
.map(|res| match res {
|
||||
Ok(pl) => pl,
|
||||
Err(e) => panic!(format!("Error reading payload: {}", e)),
|
||||
})
|
||||
.fold(0usize, |acc, chunk| ready(acc + chunk.len()))
|
||||
.map(|req_size| {
|
||||
Ok::<_, Error>(Response::Ok().body(format!("size={}", req_size)))
|
||||
})
|
||||
}))
|
||||
HttpService::build()
|
||||
.h1(service_fn(|mut request: Request| {
|
||||
request
|
||||
.take_payload()
|
||||
.map(|res| match res {
|
||||
Ok(pl) => pl,
|
||||
Err(e) => panic!(format!("Error reading payload: {}", e)),
|
||||
})
|
||||
.fold(0usize, |acc, chunk| ready(acc + chunk.len()))
|
||||
.map(|req_size| {
|
||||
Ok::<_, Error>(Response::Ok().body(format!("size={}", req_size)))
|
||||
})
|
||||
}))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let returned_size = {
|
||||
@ -167,6 +171,7 @@ async fn test_slow_request() {
|
||||
HttpService::build()
|
||||
.client_timeout(100)
|
||||
.finish(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -179,7 +184,9 @@ async fn test_slow_request() {
|
||||
#[actix_rt::test]
|
||||
async fn test_http1_malformed_request() {
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
HttpService::build()
|
||||
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -192,7 +199,9 @@ async fn test_http1_malformed_request() {
|
||||
#[actix_rt::test]
|
||||
async fn test_http1_keepalive() {
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
HttpService::build()
|
||||
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -213,6 +222,7 @@ async fn test_http1_keepalive_timeout() {
|
||||
HttpService::build()
|
||||
.keep_alive(1)
|
||||
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -230,7 +240,9 @@ async fn test_http1_keepalive_timeout() {
|
||||
#[actix_rt::test]
|
||||
async fn test_http1_keepalive_close() {
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
HttpService::build()
|
||||
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -248,7 +260,9 @@ async fn test_http1_keepalive_close() {
|
||||
#[actix_rt::test]
|
||||
async fn test_http10_keepalive_default_close() {
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
HttpService::build()
|
||||
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -265,7 +279,9 @@ async fn test_http10_keepalive_default_close() {
|
||||
#[actix_rt::test]
|
||||
async fn test_http10_keepalive() {
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
HttpService::build()
|
||||
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -292,6 +308,7 @@ async fn test_http1_keepalive_disabled() {
|
||||
HttpService::build()
|
||||
.keep_alive(KeepAlive::Disabled)
|
||||
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
||||
@ -313,18 +330,20 @@ async fn test_content_length() {
|
||||
};
|
||||
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|req: Request| {
|
||||
let indx: usize = req.uri().path()[1..].parse().unwrap();
|
||||
let statuses = [
|
||||
StatusCode::NO_CONTENT,
|
||||
StatusCode::CONTINUE,
|
||||
StatusCode::SWITCHING_PROTOCOLS,
|
||||
StatusCode::PROCESSING,
|
||||
StatusCode::OK,
|
||||
StatusCode::NOT_FOUND,
|
||||
];
|
||||
future::ok::<_, ()>(Response::new(statuses[indx]))
|
||||
})
|
||||
HttpService::build()
|
||||
.h1(|req: Request| {
|
||||
let indx: usize = req.uri().path()[1..].parse().unwrap();
|
||||
let statuses = [
|
||||
StatusCode::NO_CONTENT,
|
||||
StatusCode::CONTINUE,
|
||||
StatusCode::SWITCHING_PROTOCOLS,
|
||||
StatusCode::PROCESSING,
|
||||
StatusCode::OK,
|
||||
StatusCode::NOT_FOUND,
|
||||
];
|
||||
future::ok::<_, ()>(Response::new(statuses[indx]))
|
||||
})
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let header = HeaderName::from_static("content-length");
|
||||
@ -377,7 +396,7 @@ async fn test_h1_headers() {
|
||||
);
|
||||
}
|
||||
future::ok::<_, ()>(builder.body(data.clone()))
|
||||
})
|
||||
}).tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -413,7 +432,9 @@ 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(|| {
|
||||
HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||
HttpService::build()
|
||||
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -427,7 +448,9 @@ async fn test_h1_body() {
|
||||
#[actix_rt::test]
|
||||
async fn test_h1_head_empty() {
|
||||
let mut srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||
HttpService::build()
|
||||
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.head("/").send().await.unwrap();
|
||||
@ -449,9 +472,11 @@ async fn test_h1_head_empty() {
|
||||
#[actix_rt::test]
|
||||
async fn test_h1_head_binary() {
|
||||
let mut srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| {
|
||||
ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR))
|
||||
})
|
||||
HttpService::build()
|
||||
.h1(|_| {
|
||||
ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR))
|
||||
})
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.head("/").send().await.unwrap();
|
||||
@ -473,7 +498,9 @@ async fn test_h1_head_binary() {
|
||||
#[actix_rt::test]
|
||||
async fn test_h1_head_binary2() {
|
||||
let srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||
HttpService::build()
|
||||
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.head("/").send().await.unwrap();
|
||||
@ -491,12 +518,14 @@ async fn test_h1_head_binary2() {
|
||||
#[actix_rt::test]
|
||||
async fn test_h1_body_length() {
|
||||
let mut srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| {
|
||||
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
||||
ok::<_, ()>(
|
||||
Response::Ok().body(body::SizedStream::new(STR.len() as u64, body)),
|
||||
)
|
||||
})
|
||||
HttpService::build()
|
||||
.h1(|_| {
|
||||
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
||||
ok::<_, ()>(
|
||||
Response::Ok().body(body::SizedStream::new(STR.len() as u64, body)),
|
||||
)
|
||||
})
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -510,14 +539,16 @@ async fn test_h1_body_length() {
|
||||
#[actix_rt::test]
|
||||
async fn test_h1_body_chunked_explicit() {
|
||||
let mut srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| {
|
||||
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
||||
ok::<_, ()>(
|
||||
Response::Ok()
|
||||
.header(header::TRANSFER_ENCODING, "chunked")
|
||||
.streaming(body),
|
||||
)
|
||||
})
|
||||
HttpService::build()
|
||||
.h1(|_| {
|
||||
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
||||
ok::<_, ()>(
|
||||
Response::Ok()
|
||||
.header(header::TRANSFER_ENCODING, "chunked")
|
||||
.streaming(body),
|
||||
)
|
||||
})
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -542,10 +573,12 @@ async fn test_h1_body_chunked_explicit() {
|
||||
#[actix_rt::test]
|
||||
async fn test_h1_body_chunked_implicit() {
|
||||
let mut srv = TestServer::start(|| {
|
||||
HttpService::build().h1(|_| {
|
||||
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
||||
ok::<_, ()>(Response::Ok().streaming(body))
|
||||
})
|
||||
HttpService::build()
|
||||
.h1(|_| {
|
||||
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
||||
ok::<_, ()>(Response::Ok().streaming(body))
|
||||
})
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -568,8 +601,8 @@ async fn test_h1_body_chunked_implicit() {
|
||||
#[actix_rt::test]
|
||||
async fn test_h1_response_http_error_handling() {
|
||||
let mut srv = TestServer::start(|| {
|
||||
HttpService::build().h1(factory_fn_cfg(|_: &ServerConfig| {
|
||||
ok::<_, ()>(pipeline(|_| {
|
||||
HttpService::build()
|
||||
.h1(service_fn(|_| {
|
||||
let broken_header = Bytes::from_static(b"\0\0\0");
|
||||
ok::<_, ()>(
|
||||
Response::Ok()
|
||||
@ -577,7 +610,7 @@ async fn test_h1_response_http_error_handling() {
|
||||
.body(STR),
|
||||
)
|
||||
}))
|
||||
}))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -593,6 +626,7 @@ async fn test_h1_service_error() {
|
||||
let mut srv = TestServer::start(|| {
|
||||
HttpService::build()
|
||||
.h1(|_| future::err::<Response, Error>(error::ErrorBadRequest("error")))
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
@ -612,6 +646,7 @@ async fn test_h1_on_connect() {
|
||||
assert!(req.extensions().contains::<usize>());
|
||||
future::ok::<_, ()>(Response::Ok().finish())
|
||||
})
|
||||
.tcp()
|
||||
});
|
||||
|
||||
let response = srv.get("/").send().await.unwrap();
|
||||
|
Reference in New Issue
Block a user