mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-18 05:41:50 +01:00
Actix-files will always send SizedStream (#1496)
* Fixes #1384 * There is no need to set no_chunking * Test content-length for static files * Update the tests * Add Changelog * Try to simply fix Windows test issues! Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
7fe426f626
commit
b0866a8a0f
@ -3,6 +3,7 @@
|
|||||||
## [Unreleased] - 2020-xx-xx
|
## [Unreleased] - 2020-xx-xx
|
||||||
|
|
||||||
* Bump minimum supported Rust version to 1.40
|
* Bump minimum supported Rust version to 1.40
|
||||||
|
* Support sending Content-Length when Content-Range is specified #1384
|
||||||
|
|
||||||
## [0.2.1] - 2019-12-22
|
## [0.2.1] - 2019-12-22
|
||||||
|
|
||||||
|
@ -952,135 +952,92 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_named_file_content_range_headers() {
|
async fn test_named_file_content_range_headers() {
|
||||||
let mut srv = test::init_service(
|
let srv = test::start(|| {
|
||||||
App::new().service(Files::new("/test", ".").index_file("tests/test.binary")),
|
App::new().service(Files::new("/", "."))
|
||||||
)
|
});
|
||||||
.await;
|
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
let request = TestRequest::get()
|
let response = srv
|
||||||
.uri("/t%65st/tests/test.binary")
|
.get("/tests/test.binary")
|
||||||
.header(header::RANGE, "bytes=10-20")
|
.header(header::RANGE, "bytes=10-20")
|
||||||
.to_request();
|
.send()
|
||||||
|
.await
|
||||||
let response = test::call_service(&mut srv, request).await;
|
|
||||||
let contentrange = response
|
|
||||||
.headers()
|
|
||||||
.get(header::CONTENT_RANGE)
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let content_range = response.headers().get(header::CONTENT_RANGE).unwrap();
|
||||||
assert_eq!(contentrange, "bytes 10-20/100");
|
assert_eq!(content_range.to_str().unwrap(), "bytes 10-20/100");
|
||||||
|
|
||||||
// Invalid range header
|
// Invalid range header
|
||||||
let request = TestRequest::get()
|
let response = srv
|
||||||
.uri("/t%65st/tests/test.binary")
|
.get("/tests/test.binary")
|
||||||
.header(header::RANGE, "bytes=10-5")
|
.header(header::RANGE, "bytes=10-5")
|
||||||
.to_request();
|
.send()
|
||||||
let response = test::call_service(&mut srv, request).await;
|
.await
|
||||||
|
|
||||||
let contentrange = response
|
|
||||||
.headers()
|
|
||||||
.get(header::CONTENT_RANGE)
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let content_range = response.headers().get(header::CONTENT_RANGE).unwrap();
|
||||||
assert_eq!(contentrange, "bytes */100");
|
assert_eq!(content_range.to_str().unwrap(), "bytes */100");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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 srv = test::start(|| {
|
||||||
|
App::new().service(Files::new("/", "."))
|
||||||
let mut srv = test::init_service(
|
});
|
||||||
App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
let request = TestRequest::get()
|
let response = srv
|
||||||
.uri("/t%65st/tests/test.binary")
|
.get("/tests/test.binary")
|
||||||
.header(header::RANGE, "bytes=10-20")
|
.header(header::RANGE, "bytes=10-20")
|
||||||
.to_request();
|
.send()
|
||||||
let _response = test::call_service(&mut srv, request).await;
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let content_length = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
||||||
|
assert_eq!(content_length.to_str().unwrap(), "11");
|
||||||
|
|
||||||
// let contentlength = response
|
// Valid range header, starting from 0
|
||||||
// .headers()
|
let response = srv
|
||||||
// .get(header::CONTENT_LENGTH)
|
.get("/tests/test.binary")
|
||||||
// .unwrap()
|
.header(header::RANGE, "bytes=0-20")
|
||||||
// .to_str()
|
.send()
|
||||||
// .unwrap();
|
.await
|
||||||
// assert_eq!(contentlength, "11");
|
.unwrap();
|
||||||
|
let content_length = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
||||||
// Invalid range header
|
assert_eq!(content_length.to_str().unwrap(), "21");
|
||||||
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
|
// Without range header
|
||||||
let request = TestRequest::get()
|
let mut response = srv.get("/tests/test.binary").send().await.unwrap();
|
||||||
.uri("/t%65st/tests/test.binary")
|
let content_length = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
||||||
// .no_default_headers()
|
assert_eq!(content_length.to_str().unwrap(), "100");
|
||||||
.to_request();
|
|
||||||
let _response = test::call_service(&mut srv, request).await;
|
|
||||||
|
|
||||||
// let contentlength = response
|
// Should be no transfer-encoding
|
||||||
// .headers()
|
let transfer_encoding = response.headers().get(header::TRANSFER_ENCODING);
|
||||||
// .get(header::CONTENT_LENGTH)
|
assert!(transfer_encoding.is_none());
|
||||||
// .unwrap()
|
|
||||||
// .to_str()
|
|
||||||
// .unwrap();
|
|
||||||
// assert_eq!(contentlength, "100");
|
|
||||||
|
|
||||||
// chunked
|
// Check file contents
|
||||||
let request = TestRequest::get()
|
let bytes = response.body().await.unwrap();
|
||||||
.uri("/t%65st/tests/test.binary")
|
|
||||||
.to_request();
|
|
||||||
let response = test::call_service(&mut srv, request).await;
|
|
||||||
|
|
||||||
// with enabled compression
|
|
||||||
// {
|
|
||||||
// let te = response
|
|
||||||
// .headers()
|
|
||||||
// .get(header::TRANSFER_ENCODING)
|
|
||||||
// .unwrap()
|
|
||||||
// .to_str()
|
|
||||||
// .unwrap();
|
|
||||||
// assert_eq!(te, "chunked");
|
|
||||||
// }
|
|
||||||
|
|
||||||
let bytes = test::read_body(response).await;
|
|
||||||
let data = Bytes::from(fs::read("tests/test.binary").unwrap());
|
let data = Bytes::from(fs::read("tests/test.binary").unwrap());
|
||||||
assert_eq!(bytes, data);
|
assert_eq!(bytes, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_head_content_length_headers() {
|
async fn test_head_content_length_headers() {
|
||||||
let mut srv = test::init_service(
|
let srv = test::start(|| {
|
||||||
App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
|
App::new().service(Files::new("/", "."))
|
||||||
)
|
});
|
||||||
.await;
|
|
||||||
|
|
||||||
// Valid range header
|
let response = srv
|
||||||
let request = TestRequest::default()
|
.head("/tests/test.binary")
|
||||||
.method(Method::HEAD)
|
.send()
|
||||||
.uri("/t%65st/tests/test.binary")
|
.await
|
||||||
.to_request();
|
.unwrap();
|
||||||
let _response = test::call_service(&mut srv, request).await;
|
|
||||||
|
|
||||||
// TODO: fix check
|
let content_length = response
|
||||||
// let contentlength = response
|
.headers()
|
||||||
// .headers()
|
.get(header::CONTENT_LENGTH)
|
||||||
// .get(header::CONTENT_LENGTH)
|
.unwrap()
|
||||||
// .unwrap()
|
.to_str()
|
||||||
// .to_str()
|
.unwrap();
|
||||||
// .unwrap();
|
|
||||||
// assert_eq!(contentlength, "100");
|
assert_eq!(content_length, "100");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
@ -388,11 +388,12 @@ impl NamedFile {
|
|||||||
fut: None,
|
fut: None,
|
||||||
counter: 0,
|
counter: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
if offset != 0 || length != self.md.len() {
|
if offset != 0 || length != self.md.len() {
|
||||||
Ok(resp.status(StatusCode::PARTIAL_CONTENT).streaming(reader))
|
resp.status(StatusCode::PARTIAL_CONTENT);
|
||||||
} else {
|
|
||||||
Ok(resp.body(SizedStream::new(length, reader)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(resp.body(SizedStream::new(length, reader)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user