use tide::http::{headers, Method, Request, StatusCode, Url};
use tide::Response;
const TEXT: &str = concat![
"Chunk one\n",
"data data\n",
"\n",
"Chunk two\n",
"data data\n",
"\n",
"Chunk three\n",
"data data\n",
];
const BR_COMPRESSED: &[u8] = &[
139, 31, 128, 67, 104, 117, 110, 107, 32, 111, 110, 101, 10, 100, 97, 116, 97, 32, 100, 97,
116, 97, 10, 10, 67, 104, 117, 110, 107, 32, 116, 119, 111, 10, 100, 97, 116, 97, 32, 100, 97,
116, 97, 10, 10, 67, 104, 117, 110, 107, 32, 116, 104, 114, 101, 101, 10, 100, 97, 116, 97, 32,
100, 97, 116, 97, 10, 3,
];
#[async_std::test]
async fn brotli_compressed() {
let mut app = tide::new();
app.with(
tide_compress::CompressMiddleware::builder()
.threshold(16)
.build(),
);
app.at("/").get(|_| async {
let mut res = Response::new(StatusCode::Ok);
res.set_body(TEXT.to_owned());
Ok(res)
});
let mut req = Request::new(Method::Get, Url::parse("http://_/").unwrap());
req.insert_header(headers::ACCEPT_ENCODING, "br");
let mut res: tide::http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), 200);
assert!(res.header(headers::CONTENT_LENGTH).is_none());
assert_eq!(res[headers::CONTENT_ENCODING], "br");
assert_eq!(res[headers::VARY], "accept-encoding");
assert_eq!(res.body_bytes().await.unwrap(), BR_COMPRESSED);
}
const GZIPPED: &[u8] = &[
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x6d, 0xca, 0xb1, 0x09, 0x00, 0x30, 0x08, 0x05, 0xd1, 0xfe, 0x4f, 0xe1, 0x2e, 0x4e, 0x22, 0x44,
0x10, 0x02, 0x0a, 0xc1, 0x90, 0xf5, 0x43, 0x52, 0x59, 0xd8, 0x5c, 0xf1, 0x38, 0xb6, 0xed, 0x93,
0xc2, 0x15, 0x43, 0x52, 0xe8, 0x05, 0xe0, 0x8f, 0x79, 0xa2, 0x41, 0x5b, 0x5a, 0xdf, 0x0b,
0xde, 0xf2, 0xd7, 0x81, 0x40, 0x00, 0x00, 0x00, ];
#[async_std::test]
async fn gzip_compressed() {
let mut app = tide::new();
app.with(
tide_compress::CompressMiddleware::builder()
.threshold(16)
.build(),
);
app.at("/").get(|_| async move {
let mut res = Response::new(StatusCode::Ok);
res.set_body(TEXT.to_owned());
res.insert_header(headers::CONTENT_ENCODING, "identity");
Ok(res)
});
let mut req = Request::new(Method::Get, Url::parse("http://_/").unwrap());
req.insert_header(headers::ACCEPT_ENCODING, "gzip");
let mut res: tide::http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), 200);
assert!(res.header(headers::CONTENT_LENGTH).is_none());
assert_eq!(res[headers::CONTENT_ENCODING], "gzip");
assert_eq!(res[headers::VARY], "accept-encoding");
assert_eq!(res.body_bytes().await.unwrap(), GZIPPED);
}
#[cfg(feature = "deflate")]
const DEFLATED: &[u8] = &[
0x6d, 0xca, 0xb1, 0x09, 0x00, 0x30, 0x08, 0x05, 0xd1, 0xfe, 0x4f, 0xe1, 0x2e, 0x4e, 0x22, 0x44,
0x10, 0x02, 0x0a, 0xc1, 0x90, 0xf5, 0x43, 0x52, 0x59, 0xd8, 0x5c, 0xf1, 0x38, 0xb6, 0xed, 0x93,
0xc2, 0x15, 0x43, 0x52, 0xe8, 0x05, 0xe0, 0x8f, 0x79, 0xa2, 0x41, 0x5b, 0x5a, 0xdf, 0x0b,
];
#[cfg(feature = "deflate")]
#[async_std::test]
async fn deflate_compressed() {
let mut app = tide::new();
app.with(
tide_compress::CompressMiddleware::builder()
.threshold(16)
.build(),
);
app.at("/").get(|_| async {
let mut res = Response::new(StatusCode::Ok);
res.set_body(TEXT.to_owned());
Ok(res)
});
let mut req = Request::new(Method::Get, Url::parse("http://_/").unwrap());
req.insert_header(headers::ACCEPT_ENCODING, "deflate");
let mut res: tide::http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), 200);
assert!(res.header(headers::CONTENT_LENGTH).is_none());
assert_eq!(res[headers::CONTENT_ENCODING], "deflate");
assert_eq!(res[headers::VARY], "accept-encoding");
assert_eq!(res.body_bytes().await.unwrap(), DEFLATED);
}