Test content-length for static files

This commit is contained in:
Omid Rad 2020-05-12 21:58:36 +02:00
parent 507c0beca2
commit 8f03466b55
3 changed files with 63 additions and 65 deletions

View File

@ -111,6 +111,7 @@ serde_derive = "1.0"
brotli2 = "0.3.2" brotli2 = "0.3.2"
flate2 = "1.0.13" flate2 = "1.0.13"
criterion = "0.3" criterion = "0.3"
actix-files = "0.2.1"
[profile.release] [profile.release]
lto = true lto = true

View File

@ -992,51 +992,11 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_named_file_content_length_headers() { async fn test_named_file_content_length_headers() {
// use actix_web::body::{MessageBody, ResponseBody};
let mut srv = test::init_service( let mut srv = test::init_service(
App::new().service(Files::new("test", ".").index_file("tests/test.binary")), App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
) )
.await; .await;
// Valid range header
let request = TestRequest::get()
.uri("/t%65st/tests/test.binary")
.header(header::RANGE, "bytes=10-20")
.to_request();
let _response = test::call_service(&mut srv, request).await;
// let contentlength = response
// .headers()
// .get(header::CONTENT_LENGTH)
// .unwrap()
// .to_str()
// .unwrap();
// assert_eq!(contentlength, "11");
// Invalid range header
let request = TestRequest::get()
.uri("/t%65st/tests/test.binary")
.header(header::RANGE, "bytes=10-8")
.to_request();
let response = test::call_service(&mut srv, request).await;
assert_eq!(response.status(), StatusCode::RANGE_NOT_SATISFIABLE);
// Without range header
let request = TestRequest::get()
.uri("/t%65st/tests/test.binary")
// .no_default_headers()
.to_request();
let _response = test::call_service(&mut srv, request).await;
// let contentlength = response
// .headers()
// .get(header::CONTENT_LENGTH)
// .unwrap()
// .to_str()
// .unwrap();
// assert_eq!(contentlength, "100");
// chunked // chunked
let request = TestRequest::get() let request = TestRequest::get()
.uri("/t%65st/tests/test.binary") .uri("/t%65st/tests/test.binary")
@ -1059,30 +1019,6 @@ mod tests {
assert_eq!(bytes, data); assert_eq!(bytes, data);
} }
#[actix_rt::test]
async fn test_head_content_length_headers() {
let mut srv = test::init_service(
App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
)
.await;
// Valid range header
let request = TestRequest::default()
.method(Method::HEAD)
.uri("/t%65st/tests/test.binary")
.to_request();
let _response = test::call_service(&mut srv, request).await;
// TODO: fix check
// let contentlength = response
// .headers()
// .get(header::CONTENT_LENGTH)
// .unwrap()
// .to_str()
// .unwrap();
// assert_eq!(contentlength, "100");
}
#[actix_rt::test] #[actix_rt::test]
async fn test_static_files_with_spaces() { async fn test_static_files_with_spaces() {
let mut srv = test::init_service( let mut srv = test::init_service(

View File

@ -3,7 +3,7 @@ use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use actix_http::http::header::{ use actix_http::http::header::{
ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH, self, ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH,
TRANSFER_ENCODING, TRANSFER_ENCODING,
}; };
use brotli2::write::{BrotliDecoder, BrotliEncoder}; use brotli2::write::{BrotliDecoder, BrotliEncoder};
@ -17,6 +17,7 @@ use rand::{distributions::Alphanumeric, Rng};
use actix_web::dev::BodyEncoding; use actix_web::dev::BodyEncoding;
use actix_web::middleware::Compress; use actix_web::middleware::Compress;
use actix_web::{dev, test, web, App, Error, HttpResponse}; use actix_web::{dev, test, web, App, Error, HttpResponse};
use actix_files::Files;
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \ Hello World Hello World Hello World Hello World Hello World \
@ -889,3 +890,63 @@ async fn test_slow_request() {
// let _ = stream.read_to_string(&mut data); // let _ = stream.read_to_string(&mut data);
// assert!(data.is_empty()); // assert!(data.is_empty());
// } // }
#[actix_rt::test]
async fn test_files_content_length_headers() {
let srv = test::start_with(test::config().h1(), || {
App::new().service(
Files::new("/", ".").index_file("actix-files/tests/test.binary")
)
});
let response = srv
.head("/")
.send().await.unwrap();
let contentlength = response
.headers()
.get(CONTENT_LENGTH)
.unwrap()
.to_str()
.unwrap();
assert_eq!(contentlength, "100");
let response = srv
.head("/")
.header(header::RANGE, "bytes=10-20")
.send().await.unwrap();
let contentlength = response
.headers()
.get(CONTENT_LENGTH)
.unwrap()
.to_str()
.unwrap();
assert_eq!(contentlength, "11");
let response = srv
.head("/")
.header(header::RANGE, "bytes=0-20")
.send().await.unwrap();
let contentlength = response
.headers()
.get(CONTENT_LENGTH)
.unwrap()
.to_str()
.unwrap();
assert_eq!(contentlength, "21");
let response = srv
.head("/")
.header(header::RANGE, "bytes=20-0")
.send().await.unwrap();
let contentlength = response
.headers()
.get(CONTENT_LENGTH)
.unwrap()
.to_str()
.unwrap();
assert_eq!(contentlength, "0");
}