2019-08-07 00:08:00 +00:00
|
|
|
#![feature(proc_macro_hygiene, async_await)]
|
2018-10-02 17:15:34 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
2019-05-16 20:17:14 +00:00
|
|
|
#[cfg(all(feature = "brotli_compression", feature = "gzip_compression"))]
|
2018-10-02 17:15:34 +00:00
|
|
|
extern crate rocket;
|
2019-05-16 20:17:14 +00:00
|
|
|
|
2018-10-02 17:15:34 +00:00
|
|
|
#[cfg(all(feature = "brotli_compression", feature = "gzip_compression"))]
|
2019-04-27 15:41:49 +00:00
|
|
|
mod compress_responder_tests {
|
2018-10-02 17:15:34 +00:00
|
|
|
use rocket::http::hyper::header::{ContentEncoding, Encoding};
|
|
|
|
use rocket::http::Status;
|
|
|
|
use rocket::http::{ContentType, Header};
|
|
|
|
use rocket::local::Client;
|
|
|
|
use rocket::response::{Content, Response};
|
2019-04-27 15:41:49 +00:00
|
|
|
use rocket_contrib::compression::Compress;
|
2018-10-02 17:15:34 +00:00
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
use std::io::Read;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
use flate2::read::{GzDecoder, GzEncoder};
|
2018-10-02 17:15:34 +00:00
|
|
|
|
|
|
|
const HELLO: &str = r"This is a message to hello with more than 100 bytes \
|
|
|
|
in order to have to read more than one buffer when gzipping. こんにちは!";
|
|
|
|
|
|
|
|
#[get("/")]
|
2019-04-27 15:41:49 +00:00
|
|
|
pub fn index() -> Compress<String> {
|
|
|
|
Compress(String::from(HELLO))
|
2018-10-02 17:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/font")]
|
2019-04-27 15:41:49 +00:00
|
|
|
pub fn font() -> Compress<Content<&'static str>> {
|
|
|
|
Compress(Content(ContentType::WOFF, HELLO))
|
2018-10-02 17:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/image")]
|
2019-04-27 15:41:49 +00:00
|
|
|
pub fn image() -> Compress<Content<&'static str>> {
|
|
|
|
Compress(Content(ContentType::PNG, HELLO))
|
2018-10-02 17:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/already_encoded")]
|
2019-04-27 15:41:49 +00:00
|
|
|
pub fn already_encoded() -> Compress<Response<'static>> {
|
2018-10-02 17:15:34 +00:00
|
|
|
let mut encoder = GzEncoder::new(
|
|
|
|
Cursor::new(String::from(HELLO)),
|
|
|
|
flate2::Compression::default(),
|
|
|
|
);
|
|
|
|
let mut encoded = Vec::new();
|
|
|
|
encoder.read_to_end(&mut encoded).unwrap();
|
2019-04-27 15:41:49 +00:00
|
|
|
Compress(
|
2018-10-02 17:15:34 +00:00
|
|
|
Response::build()
|
|
|
|
.header(ContentEncoding(vec![Encoding::Gzip]))
|
|
|
|
.sized_body(Cursor::new(encoded))
|
|
|
|
.finalize(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/identity")]
|
2019-04-27 15:41:49 +00:00
|
|
|
pub fn identity() -> Compress<Response<'static>> {
|
|
|
|
Compress(
|
2018-10-02 17:15:34 +00:00
|
|
|
Response::build()
|
|
|
|
.header(ContentEncoding(vec![Encoding::Identity]))
|
|
|
|
.sized_body(Cursor::new(String::from(HELLO)))
|
|
|
|
.finalize(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite().mount("/", routes![index, font, image, already_encoded, identity])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prioritizes_brotli() {
|
|
|
|
let client = Client::new(rocket()).expect("valid rocket instance");
|
|
|
|
let mut response = client
|
|
|
|
.get("/")
|
|
|
|
.header(Header::new("Accept-Encoding", "deflate, gzip, br"))
|
|
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x == "br"));
|
|
|
|
let mut body_plain = Cursor::new(Vec::<u8>::new());
|
|
|
|
brotli::BrotliDecompress(
|
|
|
|
&mut Cursor::new(response.body_bytes().unwrap()),
|
|
|
|
&mut body_plain,
|
|
|
|
)
|
|
|
|
.expect("decompress response");
|
|
|
|
assert_eq!(
|
|
|
|
String::from_utf8(body_plain.get_mut().to_vec()).unwrap(),
|
|
|
|
String::from(HELLO)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_br_font() {
|
|
|
|
let client = Client::new(rocket()).expect("valid rocket instance");
|
|
|
|
let mut response = client
|
|
|
|
.get("/font")
|
|
|
|
.header(Header::new("Accept-Encoding", "deflate, gzip, br"))
|
|
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x == "br"));
|
|
|
|
let mut body_plain = Cursor::new(Vec::<u8>::new());
|
|
|
|
brotli::BrotliDecompress(
|
|
|
|
&mut Cursor::new(response.body_bytes().unwrap()),
|
|
|
|
&mut body_plain,
|
|
|
|
)
|
|
|
|
.expect("decompress response");
|
|
|
|
assert_eq!(
|
|
|
|
String::from_utf8(body_plain.get_mut().to_vec()).unwrap(),
|
|
|
|
String::from(HELLO)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fallback_gzip() {
|
|
|
|
let client = Client::new(rocket()).expect("valid rocket instance");
|
|
|
|
let mut response = client
|
|
|
|
.get("/")
|
|
|
|
.header(Header::new("Accept-Encoding", "deflate, gzip"))
|
|
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert!(!response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x == "br"));
|
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x == "gzip"));
|
|
|
|
let mut s = String::new();
|
|
|
|
GzDecoder::new(&response.body_bytes().unwrap()[..])
|
|
|
|
.read_to_string(&mut s)
|
|
|
|
.expect("decompress response");
|
|
|
|
assert_eq!(s, String::from(HELLO));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_does_not_recompress() {
|
|
|
|
let client = Client::new(rocket()).expect("valid rocket instance");
|
|
|
|
let mut response = client
|
|
|
|
.get("/already_encoded")
|
|
|
|
.header(Header::new("Accept-Encoding", "deflate, gzip, br"))
|
|
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert!(!response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x == "br"));
|
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x == "gzip"));
|
|
|
|
let mut s = String::new();
|
|
|
|
GzDecoder::new(&response.body_bytes().unwrap()[..])
|
|
|
|
.read_to_string(&mut s)
|
|
|
|
.expect("decompress response");
|
|
|
|
assert_eq!(s, String::from(HELLO));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_does_not_compress_explicit_identity() {
|
|
|
|
let client = Client::new(rocket()).expect("valid rocket instance");
|
|
|
|
let mut response = client
|
|
|
|
.get("/identity")
|
|
|
|
.header(Header::new("Accept-Encoding", "deflate, gzip, br"))
|
|
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert!(!response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x != "identity"));
|
|
|
|
assert_eq!(
|
|
|
|
String::from_utf8(response.body_bytes().unwrap()).unwrap(),
|
|
|
|
String::from(HELLO)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ignore_exceptions() {
|
|
|
|
let client = Client::new(rocket()).expect("valid rocket instance");
|
|
|
|
let mut response = client
|
|
|
|
.get("/image")
|
|
|
|
.header(Header::new("Accept-Encoding", "deflate, gzip, br"))
|
|
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x == "br"));
|
|
|
|
let mut body_plain = Cursor::new(Vec::<u8>::new());
|
|
|
|
brotli::BrotliDecompress(
|
|
|
|
&mut Cursor::new(response.body_bytes().unwrap()),
|
|
|
|
&mut body_plain,
|
|
|
|
)
|
|
|
|
.expect("decompress response");
|
|
|
|
assert_eq!(
|
|
|
|
String::from_utf8(body_plain.get_mut().to_vec()).unwrap(),
|
|
|
|
String::from(HELLO)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ignores_unimplemented_encodings() {
|
|
|
|
let client = Client::new(rocket()).expect("valid rocket instance");
|
|
|
|
let mut response = client
|
|
|
|
.get("/")
|
|
|
|
.header(Header::new("Accept-Encoding", "deflate"))
|
|
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert!(!response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x != "identity"));
|
|
|
|
assert_eq!(
|
|
|
|
String::from_utf8(response.body_bytes().unwrap()).unwrap(),
|
|
|
|
String::from(HELLO)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_respects_identity_only() {
|
|
|
|
let client = Client::new(rocket()).expect("valid rocket instance");
|
|
|
|
let mut response = client
|
|
|
|
.get("/")
|
|
|
|
.header(Header::new("Accept-Encoding", "identity"))
|
|
|
|
.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert!(!response
|
|
|
|
.headers()
|
|
|
|
.get("Content-Encoding")
|
|
|
|
.any(|x| x != "identity"));
|
|
|
|
assert_eq!(
|
|
|
|
String::from_utf8(response.body_bytes().unwrap()).unwrap(),
|
|
|
|
String::from(HELLO)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|