From c96844ab14e30e65d5d4b3aa0c9c1c76528469da Mon Sep 17 00:00:00 2001 From: "William R. Arellano" Date: Tue, 7 Mar 2023 21:11:41 -0500 Subject: [PATCH] misc: add unit test for expected behaviour jpeg --- actix-web/src/middleware/compress.rs | 29 +++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/actix-web/src/middleware/compress.rs b/actix-web/src/middleware/compress.rs index 51b44c6ef..2c81698ef 100644 --- a/actix-web/src/middleware/compress.rs +++ b/actix-web/src/middleware/compress.rs @@ -17,7 +17,7 @@ use pin_project_lite::pin_project; use crate::{ body::{EitherBody, MessageBody}, http::{ - header::{self, AcceptEncoding, Encoding, HeaderValue}, + header::{self, AcceptEncoding, ContentType, Encoding, HeaderValue}, StatusCode, }, service::{ServiceRequest, ServiceResponse}, @@ -324,4 +324,31 @@ mod tests { assert!(vary_headers.contains(&HeaderValue::from_static("x-test"))); assert!(vary_headers.contains(&HeaderValue::from_static("accept-encoding"))); } + + #[actix_rt::test] + async fn prevents_compression_jpeg() { + const D: &str = "test image"; + const DATA: &str = const_str::repeat!(D, 100); + let app = test::init_service({ + App::new().wrap(Compress::default()).route( + "/image", + web::get().to(move || { + let mut builder = HttpResponse::Ok(); + builder.body(DATA); + builder.insert_header(ContentType::jpeg()); + builder + }), + ) + }) + .await; + let req = test::TestRequest::default() + .uri("/image") + .insert_header((header::ACCEPT_ENCODING, "gzip")) + .to_request(); + let res = test::call_service(&app, req).await; + assert_eq!(res.status(), StatusCode::OK); + assert_eq!(res.headers().get(header::CONTENT_TYPE).unwrap(), "gzip"); + let bytes = test::read_body(res).await; + assert_eq!(bytes, DATA.as_bytes()); + } }