Rocket/contrib/lib/tests/compress_responder.rs.disabled

239 lines
7.8 KiB
Plaintext
Raw Normal View History

2018-10-02 17:15:34 +00:00
#[macro_use]
#[cfg(all(feature = "brotli_compression", feature = "gzip_compression"))]
2018-10-02 17:15:34 +00:00
extern crate rocket;
2018-10-02 17:15:34 +00:00
#[cfg(all(feature = "brotli_compression", feature = "gzip_compression"))]
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::blocking::Client;
2018-10-02 17:15:34 +00:00
use rocket::response::{Content, Response};
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("/")]
pub fn index() -> Compress<String> {
Compress(String::from(HELLO))
2018-10-02 17:15:34 +00:00
}
#[get("/font")]
pub fn font() -> Compress<Content<&'static str>> {
Compress(Content(ContentType::WOFF, HELLO))
2018-10-02 17:15:34 +00:00
}
#[get("/image")]
pub fn image() -> Compress<Content<&'static str>> {
Compress(Content(ContentType::PNG, HELLO))
2018-10-02 17:15:34 +00:00
}
#[get("/already_encoded")]
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();
Compress(
2018-10-02 17:15:34 +00:00
Response::build()
.header(ContentEncoding(vec![Encoding::Gzip]))
.sized_body(Cursor::new(encoded))
.finalize(),
)
}
#[get("/identity")]
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::build().mount("/", routes![index, font, image, already_encoded, identity])
2018-10-02 17:15:34 +00:00
}
#[test]
fn test_prioritizes_brotli() {
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
let client = Client::debug(rocket()).expect("valid rocket instance");
2018-10-02 17:15:34 +00:00
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.into_bytes().unwrap()),
2018-10-02 17:15:34 +00:00
&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() {
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
let client = Client::debug(rocket()).expect("valid rocket instance");
2018-10-02 17:15:34 +00:00
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.into_bytes().unwrap()),
2018-10-02 17:15:34 +00:00
&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() {
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
let client = Client::debug(rocket()).expect("valid rocket instance");
2018-10-02 17:15:34 +00:00
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.into_bytes().unwrap()[..])
2018-10-02 17:15:34 +00:00
.read_to_string(&mut s)
.expect("decompress response");
assert_eq!(s, String::from(HELLO));
}
#[test]
fn test_does_not_recompress() {
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
let client = Client::debug(rocket()).expect("valid rocket instance");
2018-10-02 17:15:34 +00:00
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.into_bytes().unwrap()[..])
2018-10-02 17:15:34 +00:00
.read_to_string(&mut s)
.expect("decompress response");
assert_eq!(s, String::from(HELLO));
}
#[test]
fn test_does_not_compress_explicit_identity() {
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
let client = Client::debug(rocket()).expect("valid rocket instance");
2018-10-02 17:15:34 +00:00
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.into_bytes().unwrap()).unwrap(),
2018-10-02 17:15:34 +00:00
String::from(HELLO)
);
}
#[test]
fn test_ignore_exceptions() {
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
let client = Client::debug(rocket()).expect("valid rocket instance");
2018-10-02 17:15:34 +00:00
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.into_bytes().unwrap()),
2018-10-02 17:15:34 +00:00
&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() {
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
let client = Client::debug(rocket()).expect("valid rocket instance");
2018-10-02 17:15:34 +00:00
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.into_bytes().unwrap()).unwrap(),
2018-10-02 17:15:34 +00:00
String::from(HELLO)
);
}
#[test]
fn test_respects_identity_only() {
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
let client = Client::debug(rocket()).expect("valid rocket instance");
2018-10-02 17:15:34 +00:00
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.into_bytes().unwrap()).unwrap(),
2018-10-02 17:15:34 +00:00
String::from(HELLO)
);
}
}