1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00
actix-web/tests/test_server.rs

680 lines
24 KiB
Rust
Raw Normal View History

2018-11-20 19:55:50 +01:00
use std::io::{Read, Write};
use std::time::Duration;
use std::{net, thread};
2018-10-07 06:07:32 +02:00
2019-01-27 19:59:07 +01:00
use actix_http_test::TestServer;
2018-12-11 03:08:33 +01:00
use actix_service::NewService;
2018-10-09 00:24:51 +02:00
use bytes::Bytes;
2019-02-06 20:44:15 +01:00
use futures::future::{self, ok, Future};
2018-10-09 00:24:51 +02:00
use futures::stream::once;
2018-10-07 06:07:32 +02:00
2019-02-19 03:46:30 +01:00
use actix_http::body::Body;
2018-11-19 02:52:56 +01:00
use actix_http::{
2019-03-09 19:39:06 +01:00
body, client, http, Error, HttpMessage as HttpMessage2, HttpService, KeepAlive,
Request, Response,
2018-11-19 02:52:56 +01:00
};
2018-10-07 06:07:32 +02:00
#[test]
2019-02-06 20:44:15 +01:00
fn test_h1() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build()
2018-11-19 06:48:20 +01:00
.keep_alive(KeepAlive::Disabled)
.client_timeout(1000)
.client_disconnect(1000)
2019-03-09 19:39:06 +01:00
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-10-07 06:07:32 +02:00
});
2018-11-19 06:48:20 +01:00
let req = client::ClientRequest::get(srv.url("/")).finish().unwrap();
let response = srv.send_request(req).unwrap();
assert!(response.status().is_success());
2018-10-07 06:07:32 +02:00
}
2019-03-07 07:56:34 +01:00
#[test]
fn test_h1_2() {
let mut srv = TestServer::new(|| {
HttpService::build()
.keep_alive(KeepAlive::Disabled)
.client_timeout(1000)
.client_disconnect(1000)
.finish(|req: Request| {
assert_eq!(req.version(), http::Version::HTTP_11);
future::ok::<_, ()>(Response::Ok().finish())
})
.map(|_| ())
});
let req = client::ClientRequest::get(srv.url("/")).finish().unwrap();
let response = srv.send_request(req).unwrap();
assert!(response.status().is_success());
}
2019-02-06 20:44:15 +01:00
#[cfg(feature = "ssl")]
2019-03-11 23:09:42 +01:00
fn ssl_acceptor<T>() -> std::io::Result<actix_server::ssl::OpensslAcceptor<T, ()>> {
2019-02-06 20:44:15 +01:00
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
// load ssl keys
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder
.set_private_key_file("tests/key.pem", SslFiletype::PEM)
.unwrap();
builder
.set_certificate_chain_file("tests/cert.pem")
.unwrap();
builder.set_alpn_select_callback(|_, protos| {
const H2: &[u8] = b"\x02h2";
if protos.windows(3).any(|window| window == H2) {
Ok(b"h2")
} else {
Err(openssl::ssl::AlpnError::NOACK)
}
});
builder.set_alpn_protos(b"\x02h2")?;
Ok(actix_server::ssl::OpensslAcceptor::new(builder.build()))
}
#[cfg(feature = "ssl")]
#[test]
fn test_h2() -> std::io::Result<()> {
let openssl = ssl_acceptor()?;
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(move || {
2019-02-06 20:44:15 +01:00
openssl
.clone()
.map_err(|e| println!("Openssl error: {}", e))
.and_then(
2019-03-09 19:39:06 +01:00
HttpService::build()
.h2(|_| future::ok::<_, Error>(Response::Ok().finish()))
2019-02-06 20:44:15 +01:00
.map_err(|_| ()),
)
});
let req = client::ClientRequest::get(srv.surl("/")).finish().unwrap();
let response = srv.send_request(req).unwrap();
assert!(response.status().is_success());
Ok(())
}
#[cfg(feature = "ssl")]
#[test]
2019-03-07 07:56:34 +01:00
fn test_h2_1() -> std::io::Result<()> {
let openssl = ssl_acceptor()?;
let mut srv = TestServer::new(move || {
openssl
.clone()
.map_err(|e| println!("Openssl error: {}", e))
.and_then(
HttpService::build()
.finish(|req: Request| {
assert_eq!(req.version(), http::Version::HTTP_2);
future::ok::<_, Error>(Response::Ok().finish())
})
.map_err(|_| ()),
)
});
2019-02-06 20:44:15 +01:00
2019-03-07 07:56:34 +01:00
let req = client::ClientRequest::get(srv.surl("/")).finish().unwrap();
let response = srv.send_request(req).unwrap();
assert!(response.status().is_success());
Ok(())
}
#[cfg(feature = "ssl")]
#[test]
fn test_h2_body() -> std::io::Result<()> {
2019-02-06 20:44:15 +01:00
let data = "HELLOWORLD".to_owned().repeat(64 * 1024);
let openssl = ssl_acceptor()?;
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(move || {
2019-02-06 20:44:15 +01:00
openssl
.clone()
.map_err(|e| println!("Openssl error: {}", e))
.and_then(
2019-03-09 19:39:06 +01:00
HttpService::build()
.h2(|mut req: Request<_>| {
2019-02-06 20:44:15 +01:00
req.body()
.limit(1024 * 1024)
.and_then(|body| Ok(Response::Ok().body(body)))
})
.map_err(|_| ()),
)
});
let req = client::ClientRequest::get(srv.surl("/"))
.body(data.clone())
.unwrap();
2019-02-13 22:52:11 +01:00
let mut response = srv.send_request(req).unwrap();
2019-02-06 20:44:15 +01:00
assert!(response.status().is_success());
let body = srv.block_on(response.body().limit(1024 * 1024)).unwrap();
assert_eq!(&body, data.as_bytes());
Ok(())
}
2018-10-07 06:07:32 +02:00
#[test]
fn test_slow_request() {
2019-02-19 03:46:30 +01:00
let srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build()
2018-11-19 06:48:20 +01:00
.client_timeout(100)
2019-03-09 19:39:06 +01:00
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-10-07 06:07:32 +02:00
});
2018-11-19 06:48:20 +01:00
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
2018-10-07 06:07:32 +02:00
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"));
}
#[test]
2019-03-09 19:39:06 +01:00
fn test_http1_malformed_request() {
2019-02-19 03:46:30 +01:00
let srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-10-07 06:07:32 +02:00
});
2018-11-19 06:48:20 +01:00
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
2018-10-07 06:07:32 +02:00
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"));
}
2018-10-07 06:32:01 +02:00
2018-11-20 19:55:50 +01:00
#[test]
2019-03-09 19:39:06 +01:00
fn test_http1_keepalive() {
2019-02-19 03:46:30 +01:00
let srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-11-20 19:55:50 +01: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");
}
#[test]
2019-03-09 19:39:06 +01:00
fn test_http1_keepalive_timeout() {
2019-02-19 03:46:30 +01:00
let srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build()
2018-11-20 19:55:50 +01:00
.keep_alive(1)
2019-03-09 19:39:06 +01:00
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-11-20 19:55:50 +01: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);
}
#[test]
2019-03-09 19:39:06 +01:00
fn test_http1_keepalive_close() {
2019-02-19 03:46:30 +01:00
let srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-11-20 19:55:50 +01: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);
}
#[test]
2019-03-09 19:39:06 +01:00
fn test_http10_keepalive_default_close() {
2019-02-19 03:46:30 +01:00
let srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-11-20 19:55:50 +01: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);
2018-11-20 20:23:05 +01:00
assert_eq!(&data[..17], b"HTTP/1.0 200 OK\r\n");
2018-11-20 19:55:50 +01:00
let mut data = vec![0; 1024];
let res = stream.read(&mut data).unwrap();
assert_eq!(res, 0);
}
#[test]
2019-03-09 19:39:06 +01:00
fn test_http10_keepalive() {
2019-02-19 03:46:30 +01:00
let srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-11-20 19:55:50 +01: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);
2018-11-20 20:23:05 +01:00
assert_eq!(&data[..17], b"HTTP/1.0 200 OK\r\n");
2018-11-20 19:55:50 +01: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);
2018-11-20 20:23:05 +01:00
assert_eq!(&data[..17], b"HTTP/1.0 200 OK\r\n");
2018-11-20 19:55:50 +01:00
let mut data = vec![0; 1024];
let res = stream.read(&mut data).unwrap();
assert_eq!(res, 0);
}
#[test]
2019-03-09 19:39:06 +01:00
fn test_http1_keepalive_disabled() {
2019-02-19 03:46:30 +01:00
let srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build()
2018-11-20 19:55:50 +01:00
.keep_alive(KeepAlive::Disabled)
2019-03-09 19:39:06 +01:00
.h1(|_| future::ok::<_, ()>(Response::Ok().finish()))
2018-11-20 19:55:50 +01: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);
}
2018-10-07 06:32:01 +02:00
#[test]
fn test_content_length() {
use actix_http::http::{
header::{HeaderName, HeaderValue},
StatusCode,
};
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|req: Request| {
2018-11-19 06:48:20 +01:00
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]))
2018-12-06 23:32:52 +01:00
})
2018-10-07 06:32:01 +02:00
});
let header = HeaderName::from_static("content-length");
let value = HeaderValue::from_static("0");
{
for i in 0..4 {
2018-11-19 23:57:12 +01:00
let req = client::ClientRequest::get(srv.url(&format!("/{}", i)))
.finish()
.unwrap();
2018-11-19 06:48:20 +01:00
let response = srv.send_request(req).unwrap();
2018-10-07 06:32:01 +02:00
assert_eq!(response.headers().get(&header), None);
2018-11-19 06:48:20 +01:00
let req = client::ClientRequest::head(srv.url(&format!("/{}", i)))
.finish()
.unwrap();
2018-11-19 06:48:20 +01:00
let response = srv.send_request(req).unwrap();
assert_eq!(response.headers().get(&header), None);
2018-10-07 06:32:01 +02:00
}
for i in 4..6 {
2018-11-19 06:48:20 +01:00
let req = client::ClientRequest::get(srv.url(&format!("/{}", i)))
2018-10-09 00:24:51 +02:00
.finish()
.unwrap();
2018-11-19 06:48:20 +01:00
let response = srv.send_request(req).unwrap();
2018-10-07 06:32:01 +02:00
assert_eq!(response.headers().get(&header), Some(&value));
}
}
}
2018-10-09 00:24:51 +02:00
2019-03-09 19:39:06 +01:00
// TODO: fix
// #[test]
// fn test_h2_content_length() {
// use actix_http::http::{
// header::{HeaderName, HeaderValue},
// StatusCode,
// };
// let openssl = ssl_acceptor().unwrap();
// let mut srv = TestServer::new(move || {
// openssl
// .clone()
// .map_err(|e| println!("Openssl error: {}", e))
// .and_then(
// HttpService::build()
// .h2(|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]))
// })
// .map_err(|_| ()),
// )
// });
// let header = HeaderName::from_static("content-length");
// let value = HeaderValue::from_static("0");
// {
// for i in 0..4 {
// let req = client::ClientRequest::get(srv.surl(&format!("/{}", i)))
// .finish()
// .unwrap();
// let response = srv.send_request(req).unwrap();
// assert_eq!(response.headers().get(&header), None);
// let req = client::ClientRequest::head(srv.surl(&format!("/{}", i)))
// .finish()
// .unwrap();
// let response = srv.send_request(req).unwrap();
// assert_eq!(response.headers().get(&header), None);
// }
// for i in 4..6 {
// let req = client::ClientRequest::get(srv.surl(&format!("/{}", i)))
// .finish()
// .unwrap();
// let response = srv.send_request(req).unwrap();
// assert_eq!(response.headers().get(&header), Some(&value));
// }
// }
// }
2018-10-09 00:24:51 +02:00
#[test]
fn test_headers() {
let data = STR.repeat(10);
let data2 = data.clone();
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(move || {
2018-11-19 06:48:20 +01:00
let data = data.clone();
2019-03-09 19:39:06 +01:00
HttpService::build().h1(move |_| {
2018-11-19 06:48:20 +01:00
let mut builder = Response::Ok();
for idx in 0..90 {
builder.header(
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 ",
);
}
future::ok::<_, ()>(builder.body(data.clone()))
2019-03-09 19:39:06 +01:00
})
2018-10-09 00:24:51 +02:00
});
2018-11-19 06:48:20 +01:00
let mut connector = srv.new_connector();
2018-11-19 02:52:56 +01:00
2018-11-19 06:48:20 +01:00
let req = srv.get().finish().unwrap();
2018-10-09 00:24:51 +02:00
2019-02-13 22:52:11 +01:00
let mut response = srv.block_on(req.send(&mut connector)).unwrap();
2018-10-09 00:24:51 +02:00
assert!(response.status().is_success());
// read response
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
2018-10-09 00:24:51 +02:00
assert_eq!(bytes, Bytes::from(data2));
}
2019-03-09 19:39:06 +01:00
#[test]
fn test_h2_headers() {
let data = STR.repeat(10);
let data2 = data.clone();
let openssl = ssl_acceptor().unwrap();
let mut srv = TestServer::new(move || {
let data = data.clone();
openssl
.clone()
.map_err(|e| println!("Openssl error: {}", e))
.and_then(
HttpService::build().h2(move |_| {
let mut builder = Response::Ok();
for idx in 0..90 {
builder.header(
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 ",
);
}
future::ok::<_, ()>(builder.body(data.clone()))
}).map_err(|_| ()))
});
let mut connector = srv.new_connector();
let req = client::ClientRequest::get(srv.surl("/")).finish().unwrap();
let mut response = srv.block_on(req.send(&mut connector)).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.block_on(response.body()).unwrap();
assert_eq!(bytes, Bytes::from(data2));
}
2018-10-09 00:24:51 +02: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";
#[test]
fn test_body() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
2018-10-09 00:24:51 +02:00
});
2018-11-19 06:48:20 +01:00
let req = srv.get().finish().unwrap();
2019-02-13 22:52:11 +01:00
let mut response = srv.send_request(req).unwrap();
2018-10-09 00:24:51 +02:00
assert!(response.status().is_success());
// read response
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
2018-10-09 00:24:51 +02:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_head_empty() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
2018-10-09 00:24:51 +02:00
});
2018-11-19 06:48:20 +01:00
let req = client::ClientRequest::head(srv.url("/")).finish().unwrap();
2019-02-13 22:52:11 +01:00
let mut response = srv.send_request(req).unwrap();
2018-10-09 00:24:51 +02:00
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
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
2018-10-09 00:24:51 +02:00
assert!(bytes.is_empty());
}
#[test]
fn test_head_binary() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| {
2018-11-19 06:48:20 +01:00
ok::<_, ()>(Response::Ok().content_length(STR.len() as u64).body(STR))
2018-12-06 23:32:52 +01:00
})
2018-10-09 00:24:51 +02:00
});
2018-11-19 06:48:20 +01:00
let req = client::ClientRequest::head(srv.url("/")).finish().unwrap();
2019-02-13 22:52:11 +01:00
let mut response = srv.send_request(req).unwrap();
2018-10-09 00:24:51 +02:00
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
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
2018-10-09 00:24:51 +02:00
assert!(bytes.is_empty());
}
#[test]
fn test_head_binary2() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
2018-10-09 00:24:51 +02:00
});
2018-11-19 06:48:20 +01:00
let req = client::ClientRequest::head(srv.url("/")).finish().unwrap();
let response = srv.send_request(req).unwrap();
2018-10-09 00:24:51 +02:00
assert!(response.status().is_success());
{
let len = response
.headers()
.get(http::header::CONTENT_LENGTH)
.unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
}
#[test]
fn test_body_length() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| {
2018-11-19 06:48:20 +01:00
let body = once(Ok(Bytes::from_static(STR.as_ref())));
ok::<_, ()>(
Response::Ok()
.body(Body::from_message(body::SizedStream::new(STR.len(), body))),
)
2018-12-06 23:32:52 +01:00
})
2018-10-09 00:24:51 +02:00
});
2018-11-19 06:48:20 +01:00
let req = srv.get().finish().unwrap();
2019-02-13 22:52:11 +01:00
let mut response = srv.send_request(req).unwrap();
2018-10-09 00:24:51 +02:00
assert!(response.status().is_success());
// read response
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
2018-10-09 00:24:51 +02:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_body_chunked_explicit() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| {
2018-11-19 06:48:20 +01:00
let body = once::<_, Error>(Ok(Bytes::from_static(STR.as_ref())));
ok::<_, ()>(Response::Ok().streaming(body))
2018-12-06 23:32:52 +01:00
})
2018-10-09 00:24:51 +02:00
});
2018-11-19 06:48:20 +01:00
let req = srv.get().finish().unwrap();
2019-02-13 22:52:11 +01:00
let mut response = srv.send_request(req).unwrap();
2018-10-09 00:24:51 +02:00
assert!(response.status().is_success());
// read response
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
2018-10-09 00:24:51 +02:00
// decode
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_body_chunked_implicit() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(|_| {
2018-11-19 06:48:20 +01:00
let body = once::<_, Error>(Ok(Bytes::from_static(STR.as_ref())));
ok::<_, ()>(Response::Ok().streaming(body))
2018-12-06 23:32:52 +01:00
})
2018-10-09 00:24:51 +02:00
});
2018-11-19 06:48:20 +01:00
let req = srv.get().finish().unwrap();
2019-02-13 22:52:11 +01:00
let mut response = srv.send_request(req).unwrap();
2018-10-09 00:24:51 +02:00
assert!(response.status().is_success());
// read response
2018-11-19 06:48:20 +01:00
let bytes = srv.block_on(response.body()).unwrap();
2018-10-09 00:24:51 +02:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
2018-11-21 16:49:24 +01:00
use actix_server_config::ServerConfig;
use actix_service::fn_cfg_factory;
2018-11-21 16:49:24 +01:00
#[test]
fn test_response_http_error_handling() {
2019-02-19 03:46:30 +01:00
let mut srv = TestServer::new(|| {
2019-03-09 19:39:06 +01:00
HttpService::build().h1(fn_cfg_factory(|_: &ServerConfig| {
Ok::<_, ()>(|_| {
let broken_header = Bytes::from_static(b"\0\0\0");
ok::<_, ()>(
Response::Ok()
.header(http::header::CONTENT_TYPE, broken_header)
.body(STR),
)
})
}))
2018-11-21 16:49:24 +01:00
});
let req = srv.get().finish().unwrap();
2019-02-13 22:52:11 +01:00
let mut response = srv.send_request(req).unwrap();
2018-11-21 16:49:24 +01:00
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
// read response
let bytes = srv.block_on(response.body()).unwrap();
assert!(bytes.is_empty());
}