2021-06-17 16:57:58 +00:00
|
|
|
use std::{
|
|
|
|
convert::Infallible,
|
|
|
|
io::{Read, Write},
|
|
|
|
net, thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2021-05-14 15:40:00 +00:00
|
|
|
use actix_http::{
|
2021-12-04 19:40:47 +00:00
|
|
|
body::{self, BodyStream, BoxBody, SizedStream},
|
2021-06-17 16:57:58 +00:00
|
|
|
header, http, Error, HttpMessage, HttpService, KeepAlive, Request, Response,
|
|
|
|
StatusCode,
|
2021-05-14 15:40:00 +00:00
|
|
|
};
|
2019-12-12 17:08:38 +00:00
|
|
|
use actix_http_test::test_server;
|
2021-01-03 23:47:04 +00:00
|
|
|
use actix_rt::time::sleep;
|
2019-12-08 13:25:24 +00:00
|
|
|
use actix_service::fn_service;
|
2021-04-01 14:26:13 +00:00
|
|
|
use actix_utils::future::{err, ok, ready};
|
2019-07-31 20:02:56 +00:00
|
|
|
use bytes::Bytes;
|
2021-05-14 15:40:00 +00:00
|
|
|
use derive_more::{Display, Error};
|
2021-06-17 16:57:58 +00:00
|
|
|
use futures_util::{
|
|
|
|
stream::{once, StreamExt as _},
|
|
|
|
FutureExt as _,
|
|
|
|
};
|
2019-05-29 16:37:42 +00:00
|
|
|
use regex::Regex;
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
|
|
|
.keep_alive(KeepAlive::Disabled)
|
|
|
|
.client_timeout(1000)
|
|
|
|
.client_disconnect(1000)
|
|
|
|
.h1(|req: Request| {
|
|
|
|
assert!(req.peer_addr().is_some());
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(Response::ok())
|
2019-11-26 05:25:50 +00:00
|
|
|
})
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_2() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
|
|
|
.keep_alive(KeepAlive::Disabled)
|
|
|
|
.client_timeout(1000)
|
|
|
|
.client_disconnect(1000)
|
|
|
|
.finish(|req: Request| {
|
|
|
|
assert!(req.peer_addr().is_some());
|
|
|
|
assert_eq!(req.version(), http::Version::HTTP_11);
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(Response::ok())
|
2019-11-26 05:25:50 +00:00
|
|
|
})
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:40:00 +00:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "expect failed")]
|
|
|
|
struct ExpectFailed;
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
impl From<ExpectFailed> for Response<BoxBody> {
|
2021-06-17 16:57:58 +00:00
|
|
|
fn from(_: ExpectFailed) -> Self {
|
|
|
|
Response::new(StatusCode::EXPECTATION_FAILED)
|
2021-05-14 15:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_expect_continue() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
2019-12-08 13:25:24 +00:00
|
|
|
.expect(fn_service(|req: Request| {
|
2019-11-26 05:25:50 +00:00
|
|
|
if req.head().uri.query() == Some("yes=") {
|
|
|
|
ok(req)
|
|
|
|
} else {
|
2021-05-14 15:40:00 +00:00
|
|
|
err(ExpectFailed)
|
2019-11-26 05:25:50 +00:00
|
|
|
}
|
|
|
|
}))
|
2021-06-17 16:57:58 +00:00
|
|
|
.finish(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test HTTP/1.1\r\nexpect: 100-continue\r\n\r\n");
|
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
2021-06-17 16:57:58 +00:00
|
|
|
assert!(data.starts_with("HTTP/1.1 417 Expectation Failed\r\ncontent-length"));
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test?yes= HTTP/1.1\r\nexpect: 100-continue\r\n\r\n");
|
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
|
|
|
assert!(data.starts_with("HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\n"));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-11-26 05:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_expect_continue_h1() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
2019-12-08 13:25:24 +00:00
|
|
|
.expect(fn_service(|req: Request| {
|
2021-01-03 23:47:04 +00:00
|
|
|
sleep(Duration::from_millis(20)).then(move |_| {
|
2019-04-08 18:19:56 +00:00
|
|
|
if req.head().uri.query() == Some("yes=") {
|
2019-11-19 05:08:03 +00:00
|
|
|
ok(req)
|
2019-04-08 18:19:56 +00:00
|
|
|
} else {
|
2021-05-14 15:40:00 +00:00
|
|
|
err(ExpectFailed)
|
2019-04-08 18:19:56 +00:00
|
|
|
}
|
2019-11-26 05:25:50 +00:00
|
|
|
})
|
|
|
|
}))
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(fn_service(|_| ok::<_, Infallible>(Response::ok())))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test HTTP/1.1\r\nexpect: 100-continue\r\n\r\n");
|
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
2021-06-17 16:57:58 +00:00
|
|
|
assert!(data.starts_with("HTTP/1.1 417 Expectation Failed\r\ncontent-length"));
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test?yes= HTTP/1.1\r\nexpect: 100-continue\r\n\r\n");
|
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
|
|
|
assert!(data.starts_with("HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\n"));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-04-08 17:31:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_chunked_payload() {
|
|
|
|
let chunk_sizes = vec![32768, 32, 32768];
|
|
|
|
let total_size: usize = chunk_sizes.iter().sum();
|
|
|
|
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2019-12-08 13:25:24 +00:00
|
|
|
.h1(fn_service(|mut request: Request| {
|
2019-12-02 11:33:11 +00:00
|
|
|
request
|
|
|
|
.take_payload()
|
|
|
|
.map(|res| match res {
|
|
|
|
Ok(pl) => pl,
|
2021-03-08 20:51:50 +00:00
|
|
|
Err(e) => panic!("Error reading payload: {}", e),
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
|
|
|
.fold(0usize, |acc, chunk| ready(acc + chunk.len()))
|
|
|
|
.map(|req_size| {
|
2021-04-14 01:00:14 +00:00
|
|
|
Ok::<_, Error>(
|
|
|
|
Response::ok().set_body(format!("size={}", req_size)),
|
|
|
|
)
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-05-29 16:37:42 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let returned_size = {
|
2019-05-29 16:37:42 +00:00
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
2019-11-26 05:25:50 +00:00
|
|
|
let _ = stream
|
|
|
|
.write_all(b"POST /test HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n");
|
2019-05-29 16:37:42 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
for chunk_size in chunk_sizes.iter() {
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
let random_bytes: Vec<u8> =
|
|
|
|
(0..*chunk_size).map(|_| rand::random::<u8>()).collect();
|
2019-05-29 16:37:42 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
bytes.extend(format!("{:X}\r\n", chunk_size).as_bytes());
|
|
|
|
bytes.extend(&random_bytes[..]);
|
|
|
|
bytes.extend(b"\r\n");
|
|
|
|
let _ = stream.write_all(&bytes);
|
|
|
|
}
|
2019-05-29 16:37:42 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let _ = stream.write_all(b"0\r\n\r\n");
|
|
|
|
stream.shutdown(net::Shutdown::Write).unwrap();
|
2019-05-29 16:37:42 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
2019-05-29 16:37:42 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let re = Regex::new(r"size=(\d+)").unwrap();
|
|
|
|
let size: usize = match re.captures(&data) {
|
|
|
|
Some(caps) => caps.get(1).unwrap().as_str().parse().unwrap(),
|
2021-03-08 20:51:50 +00:00
|
|
|
None => panic!("Failed to find size in HTTP Response: {}", data),
|
2019-05-29 16:37:42 +00:00
|
|
|
};
|
2021-09-10 23:01:01 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
size
|
|
|
|
};
|
2019-05-29 16:37:42 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
assert_eq!(returned_size, total_size);
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-05-29 16:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_slow_request() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
|
|
|
.client_timeout(100)
|
2021-06-17 16:57:58 +00:00
|
|
|
.finish(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n");
|
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
|
|
|
assert!(data.starts_with("HTTP/1.1 408 Request Timeout"));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_http1_malformed_request() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP1.1\r\n");
|
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
|
|
|
assert!(data.starts_with("HTTP/1.1 400 Bad Request"));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_http1_keepalive() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n\r\n");
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let _ = stream.read(&mut data);
|
|
|
|
assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n");
|
|
|
|
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n\r\n");
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let _ = stream.read(&mut data);
|
|
|
|
assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n");
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_http1_keepalive_timeout() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
|
|
|
.keep_alive(1)
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n\r\n");
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let _ = stream.read(&mut data);
|
|
|
|
assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n");
|
|
|
|
thread::sleep(Duration::from_millis(1100));
|
|
|
|
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let res = stream.read(&mut data).unwrap();
|
|
|
|
assert_eq!(res, 0);
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_http1_keepalive_close() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ =
|
|
|
|
stream.write_all(b"GET /test/tests/test HTTP/1.1\r\nconnection: close\r\n\r\n");
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let _ = stream.read(&mut data);
|
|
|
|
assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n");
|
|
|
|
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let res = stream.read(&mut data).unwrap();
|
|
|
|
assert_eq!(res, 0);
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_http10_keepalive_default_close() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.0\r\n\r\n");
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let _ = stream.read(&mut data);
|
|
|
|
assert_eq!(&data[..17], b"HTTP/1.0 200 OK\r\n");
|
|
|
|
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let res = stream.read(&mut data).unwrap();
|
|
|
|
assert_eq!(res, 0);
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_http10_keepalive() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream
|
|
|
|
.write_all(b"GET /test/tests/test HTTP/1.0\r\nconnection: keep-alive\r\n\r\n");
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let _ = stream.read(&mut data);
|
|
|
|
assert_eq!(&data[..17], b"HTTP/1.0 200 OK\r\n");
|
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.0\r\n\r\n");
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let _ = stream.read(&mut data);
|
|
|
|
assert_eq!(&data[..17], b"HTTP/1.0 200 OK\r\n");
|
|
|
|
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let res = stream.read(&mut data).unwrap();
|
|
|
|
assert_eq!(res, 0);
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_http1_keepalive_disabled() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
|
|
|
.keep_alive(KeepAlive::Disabled)
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok()))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n\r\n");
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let _ = stream.read(&mut data);
|
|
|
|
assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n");
|
|
|
|
|
|
|
|
let mut data = vec![0; 1024];
|
|
|
|
let res = stream.read(&mut data).unwrap();
|
|
|
|
assert_eq!(res, 0);
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_content_length() {
|
|
|
|
use actix_http::http::{
|
|
|
|
header::{HeaderName, HeaderValue},
|
|
|
|
StatusCode,
|
|
|
|
};
|
|
|
|
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
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,
|
|
|
|
];
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(Response::new(statuses[indx]))
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let header = HeaderName::from_static("content-length");
|
|
|
|
let value = HeaderValue::from_static("0");
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
{
|
|
|
|
for i in 0..4 {
|
|
|
|
let req = srv.request(http::Method::GET, srv.url(&format!("/{}", i)));
|
|
|
|
let response = req.send().await.unwrap();
|
|
|
|
assert_eq!(response.headers().get(&header), None);
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let req = srv.request(http::Method::HEAD, srv.url(&format!("/{}", i)));
|
|
|
|
let response = req.send().await.unwrap();
|
|
|
|
assert_eq!(response.headers().get(&header), None);
|
|
|
|
}
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
for i in 4..6 {
|
|
|
|
let req = srv.request(http::Method::GET, srv.url(&format!("/{}", i)));
|
|
|
|
let response = req.send().await.unwrap();
|
|
|
|
assert_eq!(response.headers().get(&header), Some(&value));
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
2019-11-26 05:25:50 +00:00
|
|
|
}
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_headers() {
|
|
|
|
let data = STR.repeat(10);
|
|
|
|
let data2 = data.clone();
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let data = data.clone();
|
|
|
|
HttpService::build().h1(move |_| {
|
2021-04-14 01:00:14 +00:00
|
|
|
let mut builder = Response::build(StatusCode::OK);
|
2019-03-26 18:54:35 +00:00
|
|
|
for idx in 0..90 {
|
2021-01-15 02:11:10 +00:00
|
|
|
builder.insert_header((
|
2019-03-26 18:54:35 +00:00
|
|
|
format!("X-TEST-{}", idx).as_str(),
|
|
|
|
"TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST ",
|
2021-01-15 02:11:10 +00:00
|
|
|
));
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(builder.body(data.clone()))
|
2019-12-02 11:33:11 +00:00
|
|
|
}).tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
}).await;
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data2));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World";
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_body() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_head_empty() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.head("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-11-26 05:25:50 +00:00
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_head_binary() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.head("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_head_binary2() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h1(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.head("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-11-26 05:25:50 +00:00
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_body_length() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h1(|_| {
|
2021-06-17 16:57:58 +00:00
|
|
|
let body = once(ok::<_, Infallible>(Bytes::from_static(STR.as_ref())));
|
|
|
|
ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::ok().set_body(SizedStream::new(STR.len() as u64, body)),
|
2019-12-02 11:33:11 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_body_chunked_explicit() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h1(|_| {
|
|
|
|
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
2021-12-04 19:40:47 +00:00
|
|
|
.body(BodyStream::new(body)),
|
2019-12-02 11:33:11 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert_eq!(
|
|
|
|
response
|
|
|
|
.headers()
|
|
|
|
.get(header::TRANSFER_ENCODING)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
"chunked"
|
|
|
|
);
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
|
|
|
|
// decode
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_body_chunked_implicit() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h1(|_| {
|
|
|
|
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
2021-12-04 19:40:47 +00:00
|
|
|
ok::<_, Infallible>(
|
|
|
|
Response::build(StatusCode::OK).body(BodyStream::new(body)),
|
|
|
|
)
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert_eq!(
|
|
|
|
response
|
|
|
|
.headers()
|
|
|
|
.get(header::TRANSFER_ENCODING)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
"chunked"
|
|
|
|
);
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_response_http_error_handling() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2019-12-08 13:25:24 +00:00
|
|
|
.h1(fn_service(|_| {
|
2019-11-26 05:25:50 +00:00
|
|
|
let broken_header = Bytes::from_static(b"\0\0\0");
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header((http::header::CONTENT_TYPE, broken_header))
|
2019-11-26 05:25:50 +00:00
|
|
|
.body(STR),
|
2019-11-19 12:54:19 +00:00
|
|
|
)
|
2019-11-18 14:40:10 +00:00
|
|
|
}))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
|
2019-03-26 18:54:35 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
2021-06-17 16:57:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
bytes,
|
|
|
|
Bytes::from_static(b"error processing HTTP: failed to parse header value")
|
|
|
|
);
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:40:00 +00:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "error")]
|
|
|
|
struct BadRequest;
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
impl From<BadRequest> for Response<BoxBody> {
|
2021-06-17 16:57:58 +00:00
|
|
|
fn from(_: BadRequest) -> Self {
|
2021-12-04 19:40:47 +00:00
|
|
|
Response::bad_request().set_body(BoxBody::new("error"))
|
2021-05-14 15:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_service_error() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
2021-12-04 19:40:47 +00:00
|
|
|
.h1(|_| err::<Response<()>, _>(BadRequest))
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"error"));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 18:54:35 +00:00
|
|
|
}
|
2019-09-01 07:15:02 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h1_on_connect() {
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut srv = test_server(|| {
|
2019-11-26 05:25:50 +00:00
|
|
|
HttpService::build()
|
2021-01-15 04:22:42 +00:00
|
|
|
.on_connect_ext(|_, data| {
|
|
|
|
data.insert(20isize);
|
|
|
|
})
|
2019-11-26 05:25:50 +00:00
|
|
|
.h1(|req: Request| {
|
2020-10-30 02:03:26 +00:00
|
|
|
assert!(req.extensions().contains::<isize>());
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(Response::ok())
|
2019-11-26 05:25:50 +00:00
|
|
|
})
|
2019-12-02 11:33:11 +00:00
|
|
|
.tcp()
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-09-01 07:15:02 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-09-01 07:15:02 +00:00
|
|
|
}
|
2021-11-19 14:04:12 +00:00
|
|
|
|
|
|
|
/// Tests compliance with 304 Not Modified spec in RFC 7232 §4.1.
|
|
|
|
/// https://datatracker.ietf.org/doc/html/rfc7232#section-4.1
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_not_modified_spec_h1() {
|
|
|
|
// TODO: this test needing a few seconds to complete reveals some weirdness with either the
|
|
|
|
// dispatcher or the client, though similar hangs occur on other tests in this file, only
|
|
|
|
// succeeding, it seems, because of the keepalive timer
|
|
|
|
|
|
|
|
static CL: header::HeaderName = header::CONTENT_LENGTH;
|
|
|
|
|
|
|
|
let mut srv = test_server(|| {
|
|
|
|
HttpService::build()
|
|
|
|
.h1(|req: Request| {
|
2021-12-04 19:40:47 +00:00
|
|
|
let res: Response<BoxBody> = match req.path() {
|
2021-11-19 14:04:12 +00:00
|
|
|
// with no content-length
|
|
|
|
"/none" => {
|
2021-12-04 19:40:47 +00:00
|
|
|
Response::with_body(StatusCode::NOT_MODIFIED, body::None::new())
|
|
|
|
.map_into_boxed_body()
|
2021-11-19 14:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// with no content-length
|
2021-12-04 19:40:47 +00:00
|
|
|
"/body" => Response::with_body(StatusCode::NOT_MODIFIED, "1234")
|
|
|
|
.map_into_boxed_body(),
|
2021-11-19 14:04:12 +00:00
|
|
|
|
|
|
|
// with manual content-length header and specific None body
|
|
|
|
"/cl-none" => {
|
2021-12-04 19:40:47 +00:00
|
|
|
let mut res = Response::with_body(
|
|
|
|
StatusCode::NOT_MODIFIED,
|
|
|
|
body::None::new(),
|
|
|
|
);
|
2021-11-19 14:04:12 +00:00
|
|
|
res.headers_mut()
|
|
|
|
.insert(CL.clone(), header::HeaderValue::from_static("24"));
|
2021-12-04 19:40:47 +00:00
|
|
|
res.map_into_boxed_body()
|
2021-11-19 14:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// with manual content-length header and ignore-able body
|
|
|
|
"/cl-body" => {
|
2021-12-04 19:40:47 +00:00
|
|
|
let mut res =
|
|
|
|
Response::with_body(StatusCode::NOT_MODIFIED, "1234");
|
2021-11-19 14:04:12 +00:00
|
|
|
res.headers_mut()
|
|
|
|
.insert(CL.clone(), header::HeaderValue::from_static("4"));
|
2021-12-04 19:40:47 +00:00
|
|
|
res.map_into_boxed_body()
|
2021-11-19 14:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => panic!("unknown route"),
|
|
|
|
};
|
|
|
|
|
|
|
|
ok::<_, Infallible>(res)
|
|
|
|
})
|
|
|
|
.tcp()
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let res = srv.get("/none").send().await.unwrap();
|
|
|
|
assert_eq!(res.status(), http::StatusCode::NOT_MODIFIED);
|
|
|
|
assert_eq!(res.headers().get(&CL), None);
|
|
|
|
assert!(srv.load_body(res).await.unwrap().is_empty());
|
|
|
|
|
|
|
|
let res = srv.get("/body").send().await.unwrap();
|
|
|
|
assert_eq!(res.status(), http::StatusCode::NOT_MODIFIED);
|
|
|
|
assert_eq!(res.headers().get(&CL), None);
|
|
|
|
assert!(srv.load_body(res).await.unwrap().is_empty());
|
|
|
|
|
|
|
|
let res = srv.get("/cl-none").send().await.unwrap();
|
|
|
|
assert_eq!(res.status(), http::StatusCode::NOT_MODIFIED);
|
|
|
|
assert_eq!(
|
|
|
|
res.headers().get(&CL),
|
|
|
|
Some(&header::HeaderValue::from_static("24")),
|
|
|
|
);
|
|
|
|
assert!(srv.load_body(res).await.unwrap().is_empty());
|
|
|
|
|
|
|
|
let res = srv.get("/cl-body").send().await.unwrap();
|
|
|
|
assert_eq!(res.status(), http::StatusCode::NOT_MODIFIED);
|
|
|
|
assert_eq!(
|
|
|
|
res.headers().get(&CL),
|
|
|
|
Some(&header::HeaderValue::from_static("4")),
|
|
|
|
);
|
|
|
|
// server does not prevent payload from being sent but clients may choose not to read it
|
|
|
|
// TODO: this is probably a bug, especially since CL header can differ in length from the body
|
|
|
|
assert!(!srv.load_body(res).await.unwrap().is_empty());
|
|
|
|
|
|
|
|
// TODO: add stream response tests
|
|
|
|
|
|
|
|
srv.stop().await;
|
|
|
|
}
|