2022-07-23 17:27:01 +02:00
|
|
|
use actix_files::{Files, NamedFile};
|
2020-10-06 22:56:28 +02:00
|
|
|
use actix_web::{
|
|
|
|
http::{
|
|
|
|
header::{self, HeaderValue},
|
|
|
|
StatusCode,
|
|
|
|
},
|
|
|
|
test::{self, TestRequest},
|
2022-07-23 17:27:01 +02:00
|
|
|
web, App,
|
2020-10-06 22:56:28 +02:00
|
|
|
};
|
|
|
|
|
2021-10-19 18:30:32 +02:00
|
|
|
#[actix_web::test]
|
2020-10-06 22:56:28 +02:00
|
|
|
async fn test_utf8_file_contents() {
|
|
|
|
// use default ISO-8859-1 encoding
|
2021-02-07 02:00:40 +01:00
|
|
|
let srv = test::init_service(App::new().service(Files::new("/", "./tests"))).await;
|
2020-10-06 22:56:28 +02:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/utf8.txt").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let res = test::call_service(&srv, req).await;
|
2020-10-06 22:56:28 +02:00
|
|
|
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
res.headers().get(header::CONTENT_TYPE),
|
2022-01-12 18:53:36 +01:00
|
|
|
Some(&HeaderValue::from_static("text/plain; charset=utf-8")),
|
2020-10-06 22:56:28 +02:00
|
|
|
);
|
|
|
|
|
2022-01-12 18:53:36 +01:00
|
|
|
// disable UTF-8 attribute
|
2021-02-12 00:03:17 +01:00
|
|
|
let srv =
|
2023-07-17 03:38:12 +02:00
|
|
|
test::init_service(App::new().service(Files::new("/", "./tests").prefer_utf8(false))).await;
|
2020-10-06 22:56:28 +02:00
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/utf8.txt").to_request();
|
2021-02-07 02:00:40 +01:00
|
|
|
let res = test::call_service(&srv, req).await;
|
2020-10-06 22:56:28 +02:00
|
|
|
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
res.headers().get(header::CONTENT_TYPE),
|
2022-01-12 18:53:36 +01:00
|
|
|
Some(&HeaderValue::from_static("text/plain")),
|
2020-10-06 22:56:28 +02:00
|
|
|
);
|
|
|
|
}
|
2022-07-23 17:27:01 +02:00
|
|
|
|
|
|
|
#[actix_web::test]
|
|
|
|
async fn partial_range_response_encoding() {
|
|
|
|
let srv = test::init_service(App::new().default_service(web::to(|| async {
|
|
|
|
NamedFile::open_async("./tests/test.binary").await.unwrap()
|
|
|
|
})))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// range request without accept-encoding returns no content-encoding header
|
|
|
|
let req = TestRequest::with_uri("/")
|
|
|
|
.append_header((header::RANGE, "bytes=10-20"))
|
|
|
|
.to_request();
|
|
|
|
let res = test::call_service(&srv, req).await;
|
|
|
|
assert_eq!(res.status(), StatusCode::PARTIAL_CONTENT);
|
|
|
|
assert!(!res.headers().contains_key(header::CONTENT_ENCODING));
|
|
|
|
|
|
|
|
// range request with accept-encoding returns a content-encoding header
|
|
|
|
let req = TestRequest::with_uri("/")
|
|
|
|
.append_header((header::RANGE, "bytes=10-20"))
|
|
|
|
.append_header((header::ACCEPT_ENCODING, "identity"))
|
|
|
|
.to_request();
|
|
|
|
let res = test::call_service(&srv, req).await;
|
|
|
|
assert_eq!(res.status(), StatusCode::PARTIAL_CONTENT);
|
|
|
|
assert_eq!(
|
|
|
|
res.headers().get(header::CONTENT_ENCODING).unwrap(),
|
|
|
|
"identity"
|
|
|
|
);
|
|
|
|
}
|