1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 15:46:48 +00:00
actix-web/tests/test_server.rs

835 lines
27 KiB
Rust
Raw Normal View History

extern crate actix;
extern crate actix_web;
extern crate tokio_core;
2017-12-27 20:58:32 +00:00
extern crate futures;
2018-01-09 20:49:46 +00:00
extern crate h2;
extern crate http;
2018-01-11 05:47:30 +00:00
extern crate bytes;
extern crate flate2;
extern crate brotli2;
extern crate rand;
2017-12-28 17:11:25 +00:00
use std::{net, thread, time};
2018-02-19 21:18:18 +00:00
use std::io::{Read, Write};
2017-12-27 19:22:27 +00:00
use std::sync::{Arc, mpsc};
2017-10-23 00:33:24 +00:00
use std::sync::atomic::{AtomicUsize, Ordering};
2018-01-11 05:47:30 +00:00
use flate2::Compression;
2018-02-19 21:18:18 +00:00
use flate2::read::GzDecoder;
2018-01-11 06:42:26 +00:00
use flate2::write::{GzEncoder, DeflateEncoder, DeflateDecoder};
use brotli2::write::{BrotliEncoder, BrotliDecoder};
2018-01-11 23:26:46 +00:00
use futures::{Future, Stream};
use futures::stream::once;
2018-02-19 21:18:18 +00:00
use h2::client as h2client;
use bytes::{Bytes, BytesMut};
use http::{header, Request};
2018-01-09 20:49:46 +00:00
use tokio_core::net::TcpStream;
use tokio_core::reactor::Core;
use rand::Rng;
2017-12-27 04:07:31 +00:00
use actix::System;
2018-02-19 21:18:18 +00:00
use actix_web::*;
2017-12-27 04:07:31 +00:00
2018-01-11 05:47:30 +00:00
const STR: &str =
"Hello World Hello World Hello World Hello World Hello World \
2018-01-11 06:42:26 +00:00
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World 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 05:47:30 +00:00
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World";
2017-12-27 04:07:31 +00:00
#[test]
fn test_start() {
2017-12-27 19:22:27 +00:00
let _ = test::TestServer::unused_addr();
let (tx, rx) = mpsc::channel();
2017-12-27 04:07:31 +00:00
thread::spawn(move || {
let sys = System::new("test");
let srv = HttpServer::new(
|| vec![Application::new()
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]);
2017-12-27 20:58:32 +00:00
2017-12-27 19:22:27 +00:00
let srv = srv.bind("127.0.0.1:0").unwrap();
2017-12-28 20:38:37 +00:00
let addr = srv.addrs()[0];
2017-12-27 20:58:32 +00:00
let srv_addr = srv.start();
let _ = tx.send((addr, srv_addr));
2017-12-27 04:07:31 +00:00
sys.run();
});
2017-12-27 20:58:32 +00:00
let (addr, srv_addr) = rx.recv().unwrap();
2018-02-19 21:18:18 +00:00
let mut sys = System::new("test-server");
{
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str()).finish().unwrap();
let response = sys.run_until_complete(req.send()).unwrap();
assert!(response.status().is_success());
}
2017-12-27 20:58:32 +00:00
// pause
2018-02-13 06:56:47 +00:00
let _ = srv_addr.send(server::PauseServer).wait();
2017-12-28 17:11:25 +00:00
thread::sleep(time::Duration::from_millis(100));
2017-12-27 20:58:32 +00:00
assert!(net::TcpStream::connect(addr).is_err());
// resume
2018-02-13 06:56:47 +00:00
let _ = srv_addr.send(server::ResumeServer).wait();
2018-02-19 21:18:18 +00:00
{
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str()).finish().unwrap();
let response = sys.run_until_complete(req.send()).unwrap();
assert!(response.status().is_success());
}
2017-12-27 04:07:31 +00:00
}
2018-02-28 04:04:01 +00:00
#[test]
2018-03-04 02:50:00 +00:00
#[cfg(unix)]
2018-02-28 04:04:01 +00:00
fn test_shutdown() {
let _ = test::TestServer::unused_addr();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let sys = System::new("test");
let srv = HttpServer::new(
|| vec![Application::new()
.resource("/", |r| r.method(Method::GET).h(httpcodes::HTTPOk))]);
let srv = srv.bind("127.0.0.1:0").unwrap();
let addr = srv.addrs()[0];
let srv_addr = srv.shutdown_timeout(1).start();
let _ = tx.send((addr, srv_addr));
sys.run();
});
let (addr, srv_addr) = rx.recv().unwrap();
let mut sys = System::new("test-server");
{
let req = client::ClientRequest::get(format!("http://{}/", addr).as_str()).finish().unwrap();
let response = sys.run_until_complete(req.send()).unwrap();
srv_addr.do_send(server::StopServer{graceful: true});
assert!(response.status().is_success());
}
2018-03-02 19:32:37 +00:00
thread::sleep(time::Duration::from_millis(1000));
2018-02-28 04:04:01 +00:00
assert!(net::TcpStream::connect(addr).is_err());
}
#[test]
fn test_simple() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(|app| app.handler(httpcodes::HTTPOk));
let req = srv.get().finish().unwrap();
let response = srv.execute(req.send()).unwrap();
assert!(response.status().is_success());
}
2017-10-23 00:33:24 +00:00
2018-02-28 04:49:53 +00:00
#[test]
fn test_headers() {
2018-03-05 01:54:58 +00:00
let data = STR.repeat(10);
let srv_data = Arc::new(data.clone());
2018-02-28 04:49:53 +00:00
let mut srv = test::TestServer::new(
move |app| {
let data = srv_data.clone();
app.handler(move |_| {
let mut builder = httpcodes::HTTPOk.build();
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 04:49:53 +00: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();
assert_eq!(bytes, Bytes::from(data));
2018-02-28 04:49:53 +00:00
}
2018-01-11 05:47:30 +00:00
#[test]
fn test_body() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-11 05:47:30 +00:00
|app| app.handler(|_| httpcodes::HTTPOk.build().body(STR)));
2018-02-19 21:18:18 +00: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 06:42:26 +00:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_body_gzip() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-11 06:42:26 +00:00
|app| app.handler(
|_| httpcodes::HTTPOk.build()
.content_encoding(headers::ContentEncoding::Gzip)
.body(STR)));
2018-02-19 21:18:18 +00:00
2018-02-24 04:29:35 +00:00
let request = srv.get().disable_decompress().finish().unwrap();
2018-02-19 21:18:18 +00: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 05:47:30 +00:00
}
#[test]
fn test_body_gzip_large() {
2018-03-05 01:54:58 +00:00
let data = STR.repeat(10);
let srv_data = Arc::new(data.clone());
let mut srv = test::TestServer::new(
move |app| {
let data = srv_data.clone();
app.handler(
move |_| httpcodes::HTTPOk.build()
.content_encoding(headers::ContentEncoding::Gzip)
.body(data.as_ref()))});
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));
}
#[test]
fn test_body_gzip_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
2018-03-06 19:02:03 +00:00
.take(70_000)
.collect::<String>();
let srv_data = Arc::new(data.clone());
let mut srv = test::TestServer::new(
move |app| {
let data = srv_data.clone();
app.handler(
move |_| httpcodes::HTTPOk.build()
.content_encoding(headers::ContentEncoding::Gzip)
.body(data.as_ref()))});
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-11 23:26:46 +00:00
#[test]
2018-02-24 04:29:35 +00:00
fn test_body_chunked_implicit() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-11 23:26:46 +00:00
|app| app.handler(|_| {
let body = once(Ok(Bytes::from_static(STR.as_ref())));
httpcodes::HTTPOk.build()
.content_encoding(headers::ContentEncoding::Gzip)
.body(Body::Streaming(Box::new(body)))}));
2018-02-24 04:29:35 +00:00
let request = srv.get().disable_decompress().finish().unwrap();
2018-02-19 21:18:18 +00: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 23:26:46 +00:00
}
2018-01-12 00:22:27 +00:00
#[test]
fn test_body_br_streaming() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-12 00:22:27 +00:00
|app| app.handler(|_| {
let body = once(Ok(Bytes::from_static(STR.as_ref())));
httpcodes::HTTPOk.build()
.content_encoding(headers::ContentEncoding::Br)
.body(Body::Streaming(Box::new(body)))}));
2018-02-24 04:29:35 +00:00
let request = srv.get().disable_decompress().finish().unwrap();
2018-02-19 21:18:18 +00: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 00:22:27 +00:00
2018-02-19 21:18:18 +00:00
// decode br
2018-01-12 00:22:27 +00: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 00:12:38 +00:00
#[test]
fn test_head_empty() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-21 00:12:38 +00:00
|app| app.handler(|_| {
httpcodes::HTTPOk.build()
.content_length(STR.len() as u64).finish()}));
2018-02-19 21:18:18 +00:00
let request = srv.head().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
{
let len = response.headers().get(header::CONTENT_LENGTH).unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
// read response
//let bytes = srv.execute(response.body()).unwrap();
//assert!(bytes.is_empty());
2018-01-21 00:12:38 +00:00
}
#[test]
fn test_head_binary() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-21 00:12:38 +00:00
|app| app.handler(|_| {
httpcodes::HTTPOk.build()
.content_encoding(headers::ContentEncoding::Identity)
.content_length(100).body(STR)}));
2018-02-19 21:18:18 +00:00
let request = srv.head().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
{
let len = response.headers().get(header::CONTENT_LENGTH).unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
// read response
//let bytes = srv.execute(response.body()).unwrap();
//assert!(bytes.is_empty());
2018-01-21 00:12:38 +00:00
}
#[test]
fn test_head_binary2() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-21 00:12:38 +00:00
|app| app.handler(|_| {
httpcodes::HTTPOk.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(STR)
}));
2018-02-19 21:18:18 +00:00
let request = srv.head().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
{
let len = response.headers().get(header::CONTENT_LENGTH).unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
2018-01-21 00:12:38 +00:00
}
2018-01-12 00:22:27 +00:00
#[test]
fn test_body_length() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-12 00:22:27 +00:00
|app| app.handler(|_| {
let body = once(Ok(Bytes::from_static(STR.as_ref())));
httpcodes::HTTPOk.build()
.content_length(STR.len() as u64)
2018-02-24 04:29:35 +00:00
.content_encoding(headers::ContentEncoding::Identity)
2018-01-12 00:22:27 +00:00
.body(Body::Streaming(Box::new(body)))}));
2018-02-19 21:18:18 +00: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 00:22:27 +00:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
2018-01-11 23:26:46 +00:00
#[test]
2018-02-24 04:29:35 +00:00
fn test_body_chunked_explicit() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-11 23:26:46 +00:00
|app| app.handler(|_| {
let body = once(Ok(Bytes::from_static(STR.as_ref())));
httpcodes::HTTPOk.build()
.chunked()
.content_encoding(headers::ContentEncoding::Gzip)
.body(Body::Streaming(Box::new(body)))}));
2018-02-24 04:29:35 +00:00
let request = srv.get().disable_decompress().finish().unwrap();
2018-02-19 21:18:18 +00: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 23:26:46 +00:00
}
2018-01-11 05:47:30 +00:00
#[test]
fn test_body_deflate() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-11 05:47:30 +00:00
|app| app.handler(
|_| httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Deflate)
.body(STR)));
2018-01-11 06:42:26 +00:00
2018-02-19 21:18:18 +00:00
// client request
2018-02-24 04:29:35 +00:00
let request = srv.get().disable_decompress().finish().unwrap();
2018-02-19 21:18:18 +00: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-01-11 06:42:26 +00:00
let mut e = DeflateDecoder::new(Vec::new());
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 05:47:30 +00:00
}
#[test]
fn test_body_brotli() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-11 05:47:30 +00:00
|app| app.handler(
|_| httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Br)
.body(STR)));
2018-01-11 06:42:26 +00:00
2018-02-19 21:18:18 +00:00
// client request
2018-02-24 04:29:35 +00:00
let request = srv.get().disable_decompress().finish().unwrap();
2018-02-19 21:18:18 +00: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 06:42:26 +00: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 05:47:30 +00:00
}
#[test]
fn test_gzip_encoding() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
2018-01-11 05:47:30 +00:00
req.body()
.and_then(|bytes: Bytes| {
Ok(httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(bytes))
}).responder()}
));
2018-02-19 21:18:18 +00:00
// client request
2018-01-11 05:47:30 +00:00
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
2018-02-19 21:18:18 +00:00
let request = srv.post()
.header(header::CONTENT_ENCODING, "gzip")
.body(enc.clone()).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 05:47:30 +00:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_gzip_encoding_large() {
2018-03-05 01:54:58 +00:00
let data = STR.repeat(10);
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
req.body()
.and_then(|bytes: Bytes| {
Ok(httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(bytes))
}).responder()}
));
// client request
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
let request = srv.post()
.header(header::CONTENT_ENCODING, "gzip")
.body(enc.clone()).unwrap();
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 19:02:03 +00:00
fn test_reading_gzip_encoding_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
2018-03-06 19:02:03 +00:00
.take(60_000)
.collect::<String>();
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
req.body()
.and_then(|bytes: Bytes| {
Ok(httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(bytes))
}).responder()}
));
// client request
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
let request = srv.post()
.header(header::CONTENT_ENCODING, "gzip")
.body(enc.clone()).unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.execute(response.body()).unwrap();
2018-03-06 19:02:03 +00:00
assert_eq!(bytes.len(), data.len());
assert_eq!(bytes, Bytes::from(data));
}
2018-01-11 05:47:30 +00:00
#[test]
2018-03-06 19:02:03 +00:00
fn test_reading_deflate_encoding() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
2018-01-11 05:47:30 +00:00
req.body()
.and_then(|bytes: Bytes| {
Ok(httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(bytes))
}).responder()}
));
let mut e = DeflateEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
2018-02-19 21:18:18 +00:00
// client request
let request = srv.post()
.header(header::CONTENT_ENCODING, "deflate")
.body(enc).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 05:47:30 +00:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
2018-03-06 19:02:03 +00:00
fn test_reading_deflate_encoding_large() {
2018-03-05 01:54:58 +00:00
let data = STR.repeat(10);
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
req.body()
.and_then(|bytes: Bytes| {
Ok(httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(bytes))
}).responder()}
));
let mut e = DeflateEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv.post()
.header(header::CONTENT_ENCODING, "deflate")
.body(enc).unwrap();
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 19:02:03 +00:00
#[test]
fn test_reading_deflate_encoding_large_random() {
let data = rand::thread_rng()
.gen_ascii_chars()
.take(160_000)
.collect::<String>();
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
req.body()
.and_then(|bytes: Bytes| {
Ok(httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(bytes))
}).responder()}
));
let mut e = DeflateEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv.post()
.header(header::CONTENT_ENCODING, "deflate")
.body(enc).unwrap();
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-01-11 05:47:30 +00:00
#[test]
fn test_brotli_encoding() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
2018-01-11 05:47:30 +00:00
req.body()
.and_then(|bytes: Bytes| {
Ok(httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(bytes))
}).responder()}
));
let mut e = BrotliEncoder::new(Vec::new(), 5);
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
2018-02-19 21:18:18 +00:00
// client request
let request = srv.post()
.header(header::CONTENT_ENCODING, "br")
.body(enc).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 05:47:30 +00:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[test]
fn test_brotli_encoding_large() {
2018-03-05 01:54:58 +00:00
let data = STR.repeat(10);
let mut srv = test::TestServer::new(|app| app.handler(|req: HttpRequest| {
req.body()
.and_then(|bytes: Bytes| {
Ok(httpcodes::HTTPOk
.build()
.content_encoding(headers::ContentEncoding::Identity)
.body(bytes))
}).responder()}
));
let mut e = BrotliEncoder::new(Vec::new(), 5);
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv.post()
.header(header::CONTENT_ENCODING, "br")
.body(enc).unwrap();
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-11 23:26:46 +00:00
#[test]
fn test_h2() {
let srv = test::TestServer::new(|app| app.handler(|_|{
httpcodes::HTTPOk.build().body(STR)
}));
let addr = srv.addr();
let mut core = Core::new().unwrap();
let handle = core.handle();
let tcp = TcpStream::connect(&addr, &handle);
let tcp = tcp.then(|res| {
2018-02-19 21:18:18 +00:00
h2client::handshake(res.unwrap())
2018-01-11 23:26:46 +00:00
}).then(move |res| {
let (mut client, h2) = res.unwrap();
let request = Request::builder()
.uri(format!("https://{}/", addr).as_str())
.body(())
.unwrap();
let (response, _) = client.send_request(request, false).unwrap();
// Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
response.and_then(|response| {
assert_eq!(response.status(), StatusCode::OK);
let (_, body) = response.into_parts();
body.fold(BytesMut::new(), |mut b, c| -> Result<_, h2::Error> {
b.extend(c);
Ok(b)
})
})
});
2018-01-11 23:38:57 +00:00
let _res = core.run(tcp);
2018-01-12 00:22:27 +00:00
// assert_eq!(_res.unwrap(), Bytes::from_static(STR.as_ref()));
2018-01-11 23:26:46 +00:00
}
#[test]
fn test_application() {
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::with_factory(
|| Application::new().resource("/", |r| r.h(httpcodes::HTTPOk)));
2018-02-19 21:18:18 +00:00
let request = srv.get().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
}
2017-10-23 00:33:24 +00:00
struct MiddlewareTest {
start: Arc<AtomicUsize>,
response: Arc<AtomicUsize>,
finish: Arc<AtomicUsize>,
}
2017-12-27 03:59:41 +00:00
impl<S> middleware::Middleware<S> for MiddlewareTest {
2018-01-10 06:48:35 +00:00
fn start(&self, _: &mut HttpRequest<S>) -> Result<middleware::Started> {
2017-10-23 00:33:24 +00:00
self.start.store(self.start.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
2018-01-10 06:48:35 +00:00
Ok(middleware::Started::Done)
2017-10-23 00:33:24 +00:00
}
2018-01-10 06:48:35 +00:00
fn response(&self, _: &mut HttpRequest<S>, resp: HttpResponse) -> Result<middleware::Response> {
2017-10-23 00:33:24 +00:00
self.response.store(self.response.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
2018-01-10 06:48:35 +00:00
Ok(middleware::Response::Done(resp))
2017-10-23 00:33:24 +00:00
}
2017-12-27 03:59:41 +00:00
fn finish(&self, _: &mut HttpRequest<S>, _: &HttpResponse) -> middleware::Finished {
2017-10-23 00:33:24 +00:00
self.finish.store(self.finish.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
2017-12-27 03:59:41 +00:00
middleware::Finished::Done
2017-10-23 00:33:24 +00:00
}
}
#[test]
fn test_middlewares() {
let num1 = Arc::new(AtomicUsize::new(0));
let num2 = Arc::new(AtomicUsize::new(0));
let num3 = Arc::new(AtomicUsize::new(0));
let act_num1 = Arc::clone(&num1);
let act_num2 = Arc::clone(&num2);
let act_num3 = Arc::clone(&num3);
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
move |app| app.middleware(MiddlewareTest{start: Arc::clone(&act_num1),
response: Arc::clone(&act_num2),
finish: Arc::clone(&act_num3)})
.handler(httpcodes::HTTPOk)
);
2018-02-19 21:18:18 +00:00
let request = srv.get().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
2017-10-23 00:33:24 +00:00
assert_eq!(num1.load(Ordering::Relaxed), 1);
assert_eq!(num2.load(Ordering::Relaxed), 1);
assert_eq!(num3.load(Ordering::Relaxed), 1);
}
2018-01-10 04:00:18 +00:00
#[test]
fn test_resource_middlewares() {
let num1 = Arc::new(AtomicUsize::new(0));
let num2 = Arc::new(AtomicUsize::new(0));
let num3 = Arc::new(AtomicUsize::new(0));
let act_num1 = Arc::clone(&num1);
let act_num2 = Arc::clone(&num2);
let act_num3 = Arc::clone(&num3);
2018-02-19 21:18:18 +00:00
let mut srv = test::TestServer::new(
2018-01-10 04:00:18 +00:00
move |app| app.handler2(
httpcodes::HTTPOk,
MiddlewareTest{start: Arc::clone(&act_num1),
response: Arc::clone(&act_num2),
finish: Arc::clone(&act_num3)})
);
2018-02-19 21:18:18 +00:00
let request = srv.get().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
2018-01-10 04:00:18 +00:00
assert_eq!(num1.load(Ordering::Relaxed), 1);
assert_eq!(num2.load(Ordering::Relaxed), 1);
// assert_eq!(num3.load(Ordering::Relaxed), 1);
}