2017-10-16 22:13:32 +02:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
2018-05-29 19:31:37 +02:00
|
|
|
#[cfg(feature = "brotli")]
|
|
|
|
extern crate brotli2;
|
2018-04-14 01:02:01 +02:00
|
|
|
extern crate bytes;
|
|
|
|
extern crate flate2;
|
2017-12-27 21:58:32 +01:00
|
|
|
extern crate futures;
|
2018-01-09 21:49:46 +01:00
|
|
|
extern crate h2;
|
2018-03-31 02:31:18 +02:00
|
|
|
extern crate http as modhttp;
|
2018-03-06 14:26:48 +01:00
|
|
|
extern crate rand;
|
2018-05-25 06:03:16 +02:00
|
|
|
extern crate tokio;
|
|
|
|
extern crate tokio_reactor;
|
|
|
|
extern crate tokio_tcp;
|
2017-10-16 22:13:32 +02:00
|
|
|
|
2018-05-29 19:31:37 +02:00
|
|
|
use std::io::{Read, Write};
|
2018-08-17 05:29:06 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::{thread, time};
|
2018-03-21 16:03:21 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
#[cfg(feature = "brotli")]
|
|
|
|
use brotli2::write::{BrotliDecoder, BrotliEncoder};
|
|
|
|
use bytes::{Bytes, BytesMut};
|
2018-02-19 22:18:18 +01:00
|
|
|
use flate2::read::GzDecoder;
|
2018-08-03 14:56:26 +02:00
|
|
|
use flate2::write::{GzEncoder, ZlibDecoder, ZlibEncoder};
|
2018-04-29 07:55:47 +02:00
|
|
|
use flate2::Compression;
|
2018-01-12 00:26:46 +01:00
|
|
|
use futures::stream::once;
|
2018-05-05 21:18:43 +02:00
|
|
|
use futures::{Future, Stream};
|
2018-02-19 22:18:18 +01:00
|
|
|
use h2::client as h2client;
|
2018-03-31 02:31:18 +02:00
|
|
|
use modhttp::Request;
|
2018-05-29 19:31:37 +02:00
|
|
|
use rand::distributions::Alphanumeric;
|
2018-04-14 01:02:01 +02:00
|
|
|
use rand::Rng;
|
2018-05-25 06:03:16 +02:00
|
|
|
use tokio::executor::current_thread;
|
|
|
|
use tokio::runtime::current_thread::Runtime;
|
|
|
|
use tokio_tcp::TcpStream;
|
2017-10-16 22:13:32 +02:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
use actix_web::*;
|
|
|
|
|
2018-04-14 01:02:01 +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";
|
2018-01-11 06:47:30 +01:00
|
|
|
|
2017-12-27 05:07:31 +01:00
|
|
|
#[test]
|
2018-05-22 06:01:52 +02:00
|
|
|
#[cfg(unix)]
|
2017-12-27 05:07:31 +01:00
|
|
|
fn test_start() {
|
2018-08-17 05:29:06 +02:00
|
|
|
use std::{mpsc, net};
|
|
|
|
|
2017-12-27 20:22:27 +01:00
|
|
|
let _ = test::TestServer::unused_addr();
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
2018-05-29 19:31:37 +02:00
|
|
|
thread::spawn(|| {
|
2018-08-17 05:29:06 +02:00
|
|
|
actix::System::run(move || {
|
2018-05-29 19:31:37 +02:00
|
|
|
let srv = server::new(|| {
|
|
|
|
vec![App::new().resource("/", |r| {
|
|
|
|
r.method(http::Method::GET).f(|_| HttpResponse::Ok())
|
|
|
|
})]
|
|
|
|
});
|
|
|
|
|
|
|
|
let srv = srv.bind("127.0.0.1:0").unwrap();
|
|
|
|
let addr = srv.addrs()[0];
|
|
|
|
let srv_addr = srv.start();
|
2018-06-14 08:37:19 +02:00
|
|
|
let _ = tx.send((addr, srv_addr, System::current()));
|
2018-04-14 01:02:01 +02:00
|
|
|
});
|
2017-12-27 05:07:31 +01:00
|
|
|
});
|
2018-05-30 01:32:39 +02:00
|
|
|
let (addr, srv_addr, sys) = rx.recv().unwrap();
|
2018-06-14 08:37:19 +02:00
|
|
|
System::set_current(sys.clone());
|
2018-02-19 22:18:18 +01:00
|
|
|
|
2018-05-29 19:31:37 +02:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
{
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str())
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
2018-05-29 19:31:37 +02:00
|
|
|
let response = rt.block_on(req.send()).unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|
2017-12-27 21:58:32 +01:00
|
|
|
|
|
|
|
// pause
|
2018-02-13 07:56:47 +01:00
|
|
|
let _ = srv_addr.send(server::PauseServer).wait();
|
2018-04-07 04:45:14 +02:00
|
|
|
thread::sleep(time::Duration::from_millis(200));
|
2018-05-21 06:09:54 +02:00
|
|
|
{
|
|
|
|
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str())
|
|
|
|
.timeout(time::Duration::from_millis(200))
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
2018-05-29 19:31:37 +02:00
|
|
|
assert!(rt.block_on(req.send()).is_err());
|
2018-05-21 06:09:54 +02:00
|
|
|
}
|
2017-12-27 21:58:32 +01:00
|
|
|
|
|
|
|
// resume
|
2018-02-13 07:56:47 +01:00
|
|
|
let _ = srv_addr.send(server::ResumeServer).wait();
|
2018-05-30 01:32:39 +02:00
|
|
|
thread::sleep(time::Duration::from_millis(200));
|
2018-02-19 22:18:18 +01:00
|
|
|
{
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str())
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
2018-05-29 19:31:37 +02:00
|
|
|
let response = rt.block_on(req.send()).unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|
2018-05-30 01:32:39 +02:00
|
|
|
|
2018-06-14 08:37:19 +02:00
|
|
|
let _ = sys.stop();
|
2017-12-27 05:07:31 +01:00
|
|
|
}
|
2017-10-16 22:13:32 +02:00
|
|
|
|
2018-02-28 05:04:01 +01:00
|
|
|
#[test]
|
2018-03-04 03:50:00 +01:00
|
|
|
#[cfg(unix)]
|
2018-02-28 05:04:01 +01:00
|
|
|
fn test_shutdown() {
|
|
|
|
let _ = test::TestServer::unused_addr();
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
2018-05-29 19:31:37 +02:00
|
|
|
thread::spawn(|| {
|
|
|
|
System::run(move || {
|
|
|
|
let srv = server::new(|| {
|
|
|
|
vec![App::new().resource("/", |r| {
|
|
|
|
r.method(http::Method::GET).f(|_| HttpResponse::Ok())
|
|
|
|
})]
|
|
|
|
});
|
|
|
|
|
|
|
|
let srv = srv.bind("127.0.0.1:0").unwrap();
|
|
|
|
let addr = srv.addrs()[0];
|
|
|
|
let srv_addr = srv.shutdown_timeout(1).start();
|
2018-06-14 08:37:19 +02:00
|
|
|
let _ = tx.send((addr, srv_addr, System::current()));
|
2018-04-14 01:02:01 +02:00
|
|
|
});
|
2018-02-28 05:04:01 +01:00
|
|
|
});
|
2018-05-30 01:32:39 +02:00
|
|
|
let (addr, srv_addr, sys) = rx.recv().unwrap();
|
2018-06-14 08:37:19 +02:00
|
|
|
System::set_current(sys.clone());
|
2018-02-28 05:04:01 +01:00
|
|
|
|
2018-05-29 19:31:37 +02:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
2018-02-28 05:04:01 +01:00
|
|
|
{
|
2018-04-14 01:02:01 +02:00
|
|
|
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str())
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
2018-05-29 19:31:37 +02:00
|
|
|
let response = rt.block_on(req.send()).unwrap();
|
2018-04-29 18:09:08 +02:00
|
|
|
srv_addr.do_send(server::StopServer { graceful: true });
|
2018-02-28 05:04:01 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:32:37 +01:00
|
|
|
thread::sleep(time::Duration::from_millis(1000));
|
2018-02-28 05:04:01 +01:00
|
|
|
assert!(net::TcpStream::connect(addr).is_err());
|
2018-05-30 01:32:39 +02:00
|
|
|
|
2018-06-14 08:37:19 +02:00
|
|
|
let _ = sys.stop();
|
2018-02-28 05:04:01 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 04:51:26 +02:00
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn test_panic() {
|
|
|
|
let _ = test::TestServer::unused_addr();
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
thread::spawn(|| {
|
|
|
|
System::run(move || {
|
|
|
|
let srv = server::new(|| {
|
|
|
|
App::new()
|
|
|
|
.resource("/panic", |r| {
|
|
|
|
r.method(http::Method::GET).f(|_| -> &'static str {
|
|
|
|
panic!("error");
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.resource("/", |r| {
|
|
|
|
r.method(http::Method::GET).f(|_| HttpResponse::Ok())
|
|
|
|
})
|
|
|
|
}).workers(1);
|
|
|
|
|
|
|
|
let srv = srv.bind("127.0.0.1:0").unwrap();
|
|
|
|
let addr = srv.addrs()[0];
|
|
|
|
srv.start();
|
|
|
|
let _ = tx.send((addr, System::current()));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let (addr, sys) = rx.recv().unwrap();
|
|
|
|
System::set_current(sys.clone());
|
|
|
|
|
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
{
|
|
|
|
let req = client::ClientRequest::get(format!("http://{}/panic", addr).as_str())
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
|
|
|
let response = rt.block_on(req.send());
|
|
|
|
assert!(response.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str())
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
|
|
|
let response = rt.block_on(req.send());
|
|
|
|
assert!(response.is_err());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str())
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
|
|
|
let response = rt.block_on(req.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = sys.stop();
|
|
|
|
}
|
|
|
|
|
2017-10-16 22:13:32 +02:00
|
|
|
#[test]
|
2017-12-27 01:47:55 +01:00
|
|
|
fn test_simple() {
|
2018-03-31 08:07:33 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok()));
|
2018-02-19 22:18:18 +01:00
|
|
|
let req = srv.get().finish().unwrap();
|
|
|
|
let response = srv.execute(req.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2017-10-16 22:13:32 +02:00
|
|
|
}
|
2017-10-23 02:33:24 +02:00
|
|
|
|
2018-02-28 05:49:53 +01:00
|
|
|
#[test]
|
|
|
|
fn test_headers() {
|
2018-03-05 02:54:58 +01:00
|
|
|
let data = STR.repeat(10);
|
2018-03-04 18:48:23 +01:00
|
|
|
let srv_data = Arc::new(data.clone());
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(move |app| {
|
|
|
|
let data = srv_data.clone();
|
|
|
|
app.handler(move |_| {
|
|
|
|
let mut builder = HttpResponse::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 ",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
builder.body(data.as_ref())
|
|
|
|
})
|
|
|
|
});
|
2018-02-28 05:49:53 +01:00
|
|
|
|
|
|
|
let request = srv.get().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-03-04 18:48:23 +01:00
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2018-02-28 05:49:53 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 06:47:30 +01:00
|
|
|
#[test]
|
|
|
|
fn test_body() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv =
|
|
|
|
test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR)));
|
2018-02-19 22:18:18 +01:00
|
|
|
|
|
|
|
let request = srv.get().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-01-11 07:42:26 +01:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_body_gzip() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-04-29 18:09:08 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Gzip)
|
|
|
|
.body(STR)
|
2018-04-14 01:02:01 +02:00
|
|
|
})
|
|
|
|
});
|
2018-02-19 22:18:18 +01:00
|
|
|
|
2018-02-24 05:29:35 +01:00
|
|
|
let request = srv.get().disable_decompress().finish().unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
|
|
|
|
// decode
|
|
|
|
let mut e = GzDecoder::new(&bytes[..]);
|
|
|
|
let mut dec = Vec::new();
|
|
|
|
e.read_to_end(&mut dec).unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
2018-01-11 06:47:30 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 18:48:23 +01:00
|
|
|
#[test]
|
|
|
|
fn test_body_gzip_large() {
|
2018-03-05 02:54:58 +01:00
|
|
|
let data = STR.repeat(10);
|
2018-03-04 18:48:23 +01:00
|
|
|
let srv_data = Arc::new(data.clone());
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(move |app| {
|
|
|
|
let data = srv_data.clone();
|
|
|
|
app.handler(move |_| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Gzip)
|
|
|
|
.body(data.as_ref())
|
|
|
|
})
|
|
|
|
});
|
2018-03-04 18:48:23 +01:00
|
|
|
|
|
|
|
let request = srv.get().disable_decompress().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
|
|
|
|
// decode
|
|
|
|
let mut e = GzDecoder::new(&bytes[..]);
|
|
|
|
let mut dec = Vec::new();
|
|
|
|
e.read_to_end(&mut dec).unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from(data));
|
|
|
|
}
|
|
|
|
|
2018-03-06 14:26:48 +01:00
|
|
|
#[test]
|
|
|
|
fn test_body_gzip_large_random() {
|
2018-04-29 18:09:08 +02:00
|
|
|
let data = rand::thread_rng()
|
2018-05-29 19:31:37 +02:00
|
|
|
.sample_iter(&Alphanumeric)
|
2018-04-29 18:09:08 +02:00
|
|
|
.take(70_000)
|
|
|
|
.collect::<String>();
|
2018-03-06 14:26:48 +01:00
|
|
|
let srv_data = Arc::new(data.clone());
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(move |app| {
|
|
|
|
let data = srv_data.clone();
|
|
|
|
app.handler(move |_| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Gzip)
|
|
|
|
.body(data.as_ref())
|
|
|
|
})
|
|
|
|
});
|
2018-03-06 14:26:48 +01:00
|
|
|
|
|
|
|
let request = srv.get().disable_decompress().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
|
|
|
|
// decode
|
|
|
|
let mut e = GzDecoder::new(&bytes[..]);
|
|
|
|
let mut dec = Vec::new();
|
|
|
|
e.read_to_end(&mut dec).unwrap();
|
|
|
|
assert_eq!(dec.len(), data.len());
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from(data));
|
|
|
|
}
|
|
|
|
|
2018-01-12 00:26:46 +01:00
|
|
|
#[test]
|
2018-02-24 05:29:35 +01:00
|
|
|
fn test_body_chunked_implicit() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-01-12 00:26:46 +01:00
|
|
|
let body = once(Ok(Bytes::from_static(STR.as_ref())));
|
2018-03-31 08:07:33 +02:00
|
|
|
HttpResponse::Ok()
|
2018-03-31 02:31:18 +02:00
|
|
|
.content_encoding(http::ContentEncoding::Gzip)
|
2018-04-14 01:02:01 +02:00
|
|
|
.body(Body::Streaming(Box::new(body)))
|
|
|
|
})
|
|
|
|
});
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-02-24 05:29:35 +01:00
|
|
|
let request = srv.get().disable_decompress().finish().unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
|
|
|
|
// decode
|
|
|
|
let mut e = GzDecoder::new(&bytes[..]);
|
|
|
|
let mut dec = Vec::new();
|
|
|
|
e.read_to_end(&mut dec).unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
2018-01-12 00:26:46 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
#[cfg(feature = "brotli")]
|
2018-01-12 01:22:27 +01:00
|
|
|
#[test]
|
|
|
|
fn test_body_br_streaming() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-01-12 01:22:27 +01:00
|
|
|
let body = once(Ok(Bytes::from_static(STR.as_ref())));
|
2018-03-31 08:07:33 +02:00
|
|
|
HttpResponse::Ok()
|
2018-03-31 02:31:18 +02:00
|
|
|
.content_encoding(http::ContentEncoding::Br)
|
2018-04-14 01:02:01 +02:00
|
|
|
.body(Body::Streaming(Box::new(body)))
|
|
|
|
})
|
|
|
|
});
|
2018-01-12 01:22:27 +01:00
|
|
|
|
2018-02-24 05:29:35 +01:00
|
|
|
let request = srv.get().disable_decompress().finish().unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-01-12 01:22:27 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
// decode br
|
2018-01-12 01:22:27 +01:00
|
|
|
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
|
|
|
|
e.write_all(bytes.as_ref()).unwrap();
|
|
|
|
let dec = e.finish().unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
2018-01-21 01:12:38 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_head_empty() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-05-17 21:20:20 +02:00
|
|
|
app.handler(|_| HttpResponse::Ok().content_length(STR.len() as u64).finish())
|
2018-04-14 01:02:01 +02:00
|
|
|
});
|
2018-01-21 01:12:38 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
let request = srv.head().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
2018-04-29 18:09:08 +02:00
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// read response
|
2018-07-06 04:11:36 +02:00
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2018-01-21 01:12:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_head_binary() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-03-31 08:07:33 +02:00
|
|
|
HttpResponse::Ok()
|
2018-03-31 02:31:18 +02:00
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_length(100)
|
|
|
|
.body(STR)
|
|
|
|
})
|
|
|
|
});
|
2018-01-21 01:12:38 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
let request = srv.head().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
2018-04-29 18:09:08 +02:00
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// read response
|
2018-07-06 04:11:36 +02:00
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2018-01-21 01:12:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_head_binary2() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-03-31 08:07:33 +02:00
|
|
|
HttpResponse::Ok()
|
2018-03-31 02:31:18 +02:00
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
2018-01-21 01:12:38 +01:00
|
|
|
.body(STR)
|
2018-04-14 01:02:01 +02:00
|
|
|
})
|
|
|
|
});
|
2018-01-21 01:12:38 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
let request = srv.head().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
2018-04-29 18:09:08 +02:00
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
2018-01-21 01:12:38 +01:00
|
|
|
}
|
2018-01-12 01:22:27 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_body_length() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-01-12 01:22:27 +01:00
|
|
|
let body = once(Ok(Bytes::from_static(STR.as_ref())));
|
2018-03-31 08:07:33 +02:00
|
|
|
HttpResponse::Ok()
|
2018-01-12 01:22:27 +01:00
|
|
|
.content_length(STR.len() as u64)
|
2018-03-31 02:31:18 +02:00
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
2018-04-14 01:02:01 +02:00
|
|
|
.body(Body::Streaming(Box::new(body)))
|
|
|
|
})
|
|
|
|
});
|
2018-01-12 01:22:27 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
let request = srv.get().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-01-12 01:22:27 +01:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
|
|
|
|
2018-01-12 00:26:46 +01:00
|
|
|
#[test]
|
2018-02-24 05:29:35 +01:00
|
|
|
fn test_body_chunked_explicit() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-01-12 00:26:46 +01:00
|
|
|
let body = once(Ok(Bytes::from_static(STR.as_ref())));
|
2018-03-31 08:07:33 +02:00
|
|
|
HttpResponse::Ok()
|
2018-01-12 00:26:46 +01:00
|
|
|
.chunked()
|
2018-03-31 02:31:18 +02:00
|
|
|
.content_encoding(http::ContentEncoding::Gzip)
|
2018-04-14 01:02:01 +02:00
|
|
|
.body(Body::Streaming(Box::new(body)))
|
|
|
|
})
|
|
|
|
});
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-02-24 05:29:35 +01:00
|
|
|
let request = srv.get().disable_decompress().finish().unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
|
|
|
|
// decode
|
|
|
|
let mut e = GzDecoder::new(&bytes[..]);
|
|
|
|
let mut dec = Vec::new();
|
|
|
|
e.read_to_end(&mut dec).unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
2018-01-12 00:26:46 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 04:24:36 +02:00
|
|
|
#[test]
|
|
|
|
fn test_body_identity() {
|
2018-08-03 14:56:26 +02:00
|
|
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
2018-07-06 04:24:36 +02:00
|
|
|
e.write_all(STR.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
let enc2 = enc.clone();
|
|
|
|
|
|
|
|
let mut srv = test::TestServer::new(move |app| {
|
|
|
|
let enc3 = enc2.clone();
|
|
|
|
app.handler(move |_| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.header(http::header::CONTENT_ENCODING, "deflate")
|
|
|
|
.body(enc3.clone())
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let request = srv
|
|
|
|
.get()
|
|
|
|
.header("accept-encoding", "deflate")
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
|
|
|
|
// decode deflate
|
|
|
|
assert_eq!(bytes, Bytes::from(STR));
|
|
|
|
}
|
|
|
|
|
2018-01-11 06:47:30 +01:00
|
|
|
#[test]
|
|
|
|
fn test_body_deflate() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-04-29 18:09:08 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Deflate)
|
|
|
|
.body(STR)
|
2018-04-14 01:02:01 +02:00
|
|
|
})
|
|
|
|
});
|
2018-01-11 07:42:26 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
// client request
|
2018-02-24 05:29:35 +01:00
|
|
|
let request = srv.get().disable_decompress().finish().unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
|
|
|
|
// decode deflate
|
2018-08-03 14:56:26 +02:00
|
|
|
let mut e = ZlibDecoder::new(Vec::new());
|
2018-01-11 07:42:26 +01:00
|
|
|
e.write_all(bytes.as_ref()).unwrap();
|
|
|
|
let dec = e.finish().unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
2018-01-11 06:47:30 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
#[cfg(feature = "brotli")]
|
2018-01-11 06:47:30 +01:00
|
|
|
#[test]
|
|
|
|
fn test_body_brotli() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.handler(|_| {
|
2018-04-29 18:09:08 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Br)
|
|
|
|
.body(STR)
|
2018-04-14 01:02:01 +02:00
|
|
|
})
|
|
|
|
});
|
2018-01-11 07:42:26 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
// client request
|
2018-02-24 05:29:35 +01:00
|
|
|
let request = srv.get().disable_decompress().finish().unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
|
|
|
|
// decode brotli
|
2018-01-11 07:42:26 +01:00
|
|
|
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
|
|
|
|
e.write_all(bytes.as_ref()).unwrap();
|
|
|
|
let dec = e.finish().unwrap();
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
2018-01-11 06:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gzip_encoding() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-06-25 06:58:04 +02:00
|
|
|
app.handler(|req: &HttpRequest| {
|
2018-04-14 01:02:01 +02:00
|
|
|
req.body()
|
|
|
|
.and_then(|bytes: Bytes| {
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.body(bytes))
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
})
|
|
|
|
});
|
2018-01-11 06:47:30 +01:00
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
// client request
|
2018-01-11 06:47:30 +01:00
|
|
|
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
|
|
|
e.write_all(STR.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
2018-05-17 21:20:20 +02:00
|
|
|
let request = srv
|
|
|
|
.post()
|
2018-03-31 02:31:18 +02:00
|
|
|
.header(http::header::CONTENT_ENCODING, "gzip")
|
2018-04-14 01:02:01 +02:00
|
|
|
.body(enc.clone())
|
|
|
|
.unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-01-11 06:47:30 +01:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
|
|
|
|
2018-03-04 18:48:23 +01:00
|
|
|
#[test]
|
|
|
|
fn test_gzip_encoding_large() {
|
2018-03-05 02:54:58 +01:00
|
|
|
let data = STR.repeat(10);
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-06-25 06:58:04 +02:00
|
|
|
app.handler(|req: &HttpRequest| {
|
2018-04-14 01:02:01 +02:00
|
|
|
req.body()
|
|
|
|
.and_then(|bytes: Bytes| {
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.body(bytes))
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
})
|
|
|
|
});
|
2018-03-04 18:48:23 +01:00
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
2018-03-06 14:26:48 +01:00
|
|
|
e.write_all(data.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
2018-05-17 21:20:20 +02:00
|
|
|
let request = srv
|
|
|
|
.post()
|
2018-03-31 02:31:18 +02:00
|
|
|
.header(http::header::CONTENT_ENCODING, "gzip")
|
2018-04-14 01:02:01 +02:00
|
|
|
.body(enc.clone())
|
|
|
|
.unwrap();
|
2018-03-06 14:26:48 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-03-06 20:02:03 +01:00
|
|
|
fn test_reading_gzip_encoding_large_random() {
|
2018-04-29 18:09:08 +02:00
|
|
|
let data = rand::thread_rng()
|
2018-05-29 19:31:37 +02:00
|
|
|
.sample_iter(&Alphanumeric)
|
2018-04-29 18:09:08 +02:00
|
|
|
.take(60_000)
|
|
|
|
.collect::<String>();
|
2018-03-06 14:26:48 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-06-25 06:58:04 +02:00
|
|
|
app.handler(|req: &HttpRequest| {
|
2018-04-14 01:02:01 +02:00
|
|
|
req.body()
|
|
|
|
.and_then(|bytes: Bytes| {
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.body(bytes))
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
})
|
|
|
|
});
|
2018-03-06 14:26:48 +01:00
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut e = GzEncoder::new(Vec::new(), Compression::default());
|
2018-03-04 18:48:23 +01:00
|
|
|
e.write_all(data.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
2018-05-17 21:20:20 +02:00
|
|
|
let request = srv
|
|
|
|
.post()
|
2018-03-31 02:31:18 +02:00
|
|
|
.header(http::header::CONTENT_ENCODING, "gzip")
|
2018-04-14 01:02:01 +02:00
|
|
|
.body(enc.clone())
|
|
|
|
.unwrap();
|
2018-03-04 18:48:23 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-03-06 20:02:03 +01:00
|
|
|
assert_eq!(bytes.len(), data.len());
|
2018-03-04 18:48:23 +01:00
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
|
|
|
}
|
|
|
|
|
2018-01-11 06:47:30 +01:00
|
|
|
#[test]
|
2018-03-06 20:02:03 +01:00
|
|
|
fn test_reading_deflate_encoding() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-06-25 06:58:04 +02:00
|
|
|
app.handler(|req: &HttpRequest| {
|
2018-04-14 01:02:01 +02:00
|
|
|
req.body()
|
|
|
|
.and_then(|bytes: Bytes| {
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.body(bytes))
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
})
|
|
|
|
});
|
2018-01-11 06:47:30 +01:00
|
|
|
|
2018-08-03 14:56:26 +02:00
|
|
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
2018-01-11 06:47:30 +01:00
|
|
|
e.write_all(STR.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
// client request
|
2018-05-17 21:20:20 +02:00
|
|
|
let request = srv
|
|
|
|
.post()
|
2018-04-29 18:09:08 +02:00
|
|
|
.header(http::header::CONTENT_ENCODING, "deflate")
|
|
|
|
.body(enc)
|
|
|
|
.unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-01-11 06:47:30 +01:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
|
|
|
|
2018-03-04 18:48:23 +01:00
|
|
|
#[test]
|
2018-03-06 20:02:03 +01:00
|
|
|
fn test_reading_deflate_encoding_large() {
|
2018-03-05 02:54:58 +01:00
|
|
|
let data = STR.repeat(10);
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-06-25 06:58:04 +02:00
|
|
|
app.handler(|req: &HttpRequest| {
|
2018-04-14 01:02:01 +02:00
|
|
|
req.body()
|
|
|
|
.and_then(|bytes: Bytes| {
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.body(bytes))
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
})
|
|
|
|
});
|
2018-03-04 18:48:23 +01:00
|
|
|
|
2018-08-03 14:56:26 +02:00
|
|
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
2018-03-04 18:48:23 +01:00
|
|
|
e.write_all(data.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
|
|
|
// client request
|
2018-05-17 21:20:20 +02:00
|
|
|
let request = srv
|
|
|
|
.post()
|
2018-04-29 18:09:08 +02:00
|
|
|
.header(http::header::CONTENT_ENCODING, "deflate")
|
|
|
|
.body(enc)
|
|
|
|
.unwrap();
|
2018-03-04 18:48:23 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
|
|
|
}
|
|
|
|
|
2018-03-06 20:02:03 +01:00
|
|
|
#[test]
|
|
|
|
fn test_reading_deflate_encoding_large_random() {
|
2018-04-29 18:09:08 +02:00
|
|
|
let data = rand::thread_rng()
|
2018-05-29 19:31:37 +02:00
|
|
|
.sample_iter(&Alphanumeric)
|
2018-04-29 18:09:08 +02:00
|
|
|
.take(160_000)
|
|
|
|
.collect::<String>();
|
2018-03-06 20:02:03 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-06-25 06:58:04 +02:00
|
|
|
app.handler(|req: &HttpRequest| {
|
2018-04-14 01:02:01 +02:00
|
|
|
req.body()
|
|
|
|
.and_then(|bytes: Bytes| {
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.body(bytes))
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
})
|
|
|
|
});
|
2018-03-06 20:02:03 +01:00
|
|
|
|
2018-08-03 14:56:26 +02:00
|
|
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
2018-03-06 20:02:03 +01:00
|
|
|
e.write_all(data.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
|
|
|
// client request
|
2018-05-17 21:20:20 +02:00
|
|
|
let request = srv
|
|
|
|
.post()
|
2018-04-29 18:09:08 +02:00
|
|
|
.header(http::header::CONTENT_ENCODING, "deflate")
|
|
|
|
.body(enc)
|
|
|
|
.unwrap();
|
2018-03-06 20:02:03 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes.len(), data.len());
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
#[cfg(feature = "brotli")]
|
2018-01-11 06:47:30 +01:00
|
|
|
#[test]
|
|
|
|
fn test_brotli_encoding() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-06-25 06:58:04 +02:00
|
|
|
app.handler(|req: &HttpRequest| {
|
2018-04-14 01:02:01 +02:00
|
|
|
req.body()
|
|
|
|
.and_then(|bytes: Bytes| {
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.body(bytes))
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
})
|
|
|
|
});
|
2018-01-11 06:47:30 +01:00
|
|
|
|
|
|
|
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
|
|
|
e.write_all(STR.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
2018-02-19 22:18:18 +01:00
|
|
|
// client request
|
2018-05-17 21:20:20 +02:00
|
|
|
let request = srv
|
|
|
|
.post()
|
2018-04-29 18:09:08 +02:00
|
|
|
.header(http::header::CONTENT_ENCODING, "br")
|
|
|
|
.body(enc)
|
|
|
|
.unwrap();
|
2018-02-19 22:18:18 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-01-11 06:47:30 +01:00
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
#[cfg(feature = "brotli")]
|
2018-03-04 18:48:23 +01:00
|
|
|
#[test]
|
|
|
|
fn test_brotli_encoding_large() {
|
2018-03-05 02:54:58 +01:00
|
|
|
let data = STR.repeat(10);
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-06-25 06:58:04 +02:00
|
|
|
app.handler(|req: &HttpRequest| {
|
2018-04-14 01:02:01 +02:00
|
|
|
req.body()
|
|
|
|
.and_then(|bytes: Bytes| {
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_encoding(http::ContentEncoding::Identity)
|
|
|
|
.body(bytes))
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
})
|
|
|
|
});
|
2018-03-04 18:48:23 +01:00
|
|
|
|
|
|
|
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
|
|
|
e.write_all(data.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
|
|
|
// client request
|
2018-05-17 21:20:20 +02:00
|
|
|
let request = srv
|
|
|
|
.post()
|
2018-04-29 18:09:08 +02:00
|
|
|
.header(http::header::CONTENT_ENCODING, "br")
|
|
|
|
.body(enc)
|
|
|
|
.unwrap();
|
2018-03-04 18:48:23 +01:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
|
|
|
}
|
|
|
|
|
2018-01-12 00:26:46 +01:00
|
|
|
#[test]
|
|
|
|
fn test_h2() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let srv = test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR)));
|
2018-01-12 00:26:46 +01:00
|
|
|
let addr = srv.addr();
|
2018-08-02 01:41:24 +02:00
|
|
|
thread::sleep(time::Duration::from_millis(500));
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-05-25 06:03:16 +02:00
|
|
|
let mut core = Runtime::new().unwrap();
|
|
|
|
let tcp = TcpStream::connect(&addr);
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-05-17 21:20:20 +02:00
|
|
|
let tcp = tcp
|
|
|
|
.then(|res| h2client::handshake(res.unwrap()))
|
2018-04-29 18:09:08 +02:00
|
|
|
.then(move |res| {
|
|
|
|
let (mut client, h2) = res.unwrap();
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
let request = Request::builder()
|
|
|
|
.uri(format!("https://{}/", addr).as_str())
|
|
|
|
.body(())
|
|
|
|
.unwrap();
|
|
|
|
let (response, _) = client.send_request(request, false).unwrap();
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
// Spawn a task to run the conn...
|
2018-05-25 06:03:16 +02:00
|
|
|
current_thread::spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
response.and_then(|response| {
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
let (_, body) = response.into_parts();
|
2018-01-12 00:26:46 +01:00
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
body.fold(BytesMut::new(), |mut b, c| -> Result<_, h2::Error> {
|
|
|
|
b.extend(c);
|
|
|
|
Ok(b)
|
|
|
|
})
|
2018-01-12 00:26:46 +01:00
|
|
|
})
|
2018-04-29 18:09:08 +02:00
|
|
|
});
|
2018-05-25 06:03:16 +02:00
|
|
|
let _res = core.block_on(tcp);
|
2018-05-09 00:44:50 +02:00
|
|
|
// assert_eq!(_res.unwrap(), Bytes::from_static(STR.as_ref()));
|
2018-01-12 00:26:46 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 01:47:55 +01:00
|
|
|
#[test]
|
|
|
|
fn test_application() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::with_factory(|| {
|
|
|
|
App::new().resource("/", |r| r.f(|_| HttpResponse::Ok()))
|
|
|
|
});
|
2018-02-19 22:18:18 +01:00
|
|
|
|
|
|
|
let request = srv.get().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2017-12-27 01:47:55 +01:00
|
|
|
}
|
2018-08-04 00:34:23 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_server_cookies() {
|
|
|
|
use actix_web::http;
|
|
|
|
|
|
|
|
let mut srv = test::TestServer::with_factory(|| {
|
|
|
|
App::new().resource("/", |r| r.f(|_| HttpResponse::Ok().cookie(http::CookieBuilder::new("first", "first_value").http_only(true).finish())
|
|
|
|
.cookie(http::Cookie::new("second", "first_value"))
|
|
|
|
.cookie(http::Cookie::new("second", "second_value"))
|
|
|
|
.finish())
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
let first_cookie = http::CookieBuilder::new("first", "first_value").http_only(true).finish();
|
|
|
|
let second_cookie = http::Cookie::new("second", "second_value");
|
|
|
|
|
|
|
|
let request = srv.get().finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
let cookies = response.cookies().expect("To have cookies");
|
|
|
|
assert_eq!(cookies.len(), 2);
|
|
|
|
if cookies[0] == first_cookie {
|
|
|
|
assert_eq!(cookies[1], second_cookie);
|
|
|
|
} else {
|
|
|
|
assert_eq!(cookies[0], second_cookie);
|
|
|
|
assert_eq!(cookies[1], first_cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
let first_cookie = first_cookie.to_string();
|
|
|
|
let second_cookie = second_cookie.to_string();
|
|
|
|
//Check that we have exactly two instances of raw cookie headers
|
|
|
|
let cookies = response.headers().get_all(http::header::SET_COOKIE)
|
|
|
|
.iter()
|
|
|
|
.map(|header| header.to_str().expect("To str").to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(cookies.len(), 2);
|
|
|
|
if cookies[0] == first_cookie {
|
|
|
|
assert_eq!(cookies[1], second_cookie);
|
|
|
|
} else {
|
|
|
|
assert_eq!(cookies[0], second_cookie);
|
|
|
|
assert_eq!(cookies[1], first_cookie);
|
|
|
|
}
|
|
|
|
}
|