test(server): add test for upload

This commit is contained in:
Orhun Parmaksız 2022-05-16 10:25:59 +03:00
parent 74237d56c5
commit 1d44cbd9c8
No known key found for this signature in database
GPG key ID: F83424824B3E4B90

View file

@ -211,26 +211,101 @@ pub fn configure_routes(cfg: &mut web::ServiceConfig) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use actix_web::{http, test, App}; use actix_web::body::BodySize;
use actix_web::body::MessageBody;
use actix_web::dev::ServiceResponse;
use actix_web::error::Error;
use actix_web::http::header;
use actix_web::test::{self, TestRequest};
use actix_web::web::Data;
use actix_web::{http, App};
use byte_unit::Byte;
use std::str;
#[actix_web::test] #[actix_web::test]
async fn test_index() { async fn test_index() {
let mut app = test::init_service(App::new().service(index)).await; let mut app = test::init_service(App::new().service(index)).await;
let req = test::TestRequest::default() let request = TestRequest::default()
.insert_header(("content-type", "text/plain")) .insert_header(("content-type", "text/plain"))
.to_request(); .to_request();
let resp = test::call_service(&mut app, req).await; let resp = test::call_service(&mut app, request).await;
assert!(resp.status().is_redirection()); assert!(resp.status().is_redirection());
assert_eq!(http::StatusCode::FOUND, resp.status()); assert_eq!(http::StatusCode::FOUND, resp.status());
} }
#[actix_web::test] fn get_multipart_request(data: &str, file_name: &str) -> TestRequest {
async fn test_serve() { let multipart_data = format!(
let mut app = test::init_service(App::new().service(serve)).await; "\r\n\
let req = test::TestRequest::default().to_request(); --multipart_bound\r\n\
let resp = test::call_service(&mut app, req).await; Content-Disposition: form-data; name=\"file\"; filename=\"{}\"\r\n\
assert_eq!(http::StatusCode::NOT_FOUND, resp.status()); Content-Type: text/plain; charset=utf-8\r\nContent-Length: {}\r\n\r\n\
{}\r\n\
--multipart_bound--\r\n",
file_name,
data.bytes().len(),
data,
);
TestRequest::post()
.insert_header((
header::CONTENT_TYPE,
header::HeaderValue::from_static("multipart/mixed; boundary=\"multipart_bound\""),
))
.set_payload(multipart_data)
} }
// TODO: add test for upload async fn assert_body(response: ServiceResponse, expected: &str) {
assert_eq!(http::StatusCode::OK, response.status());
let body = response.into_body();
match body.size() {
BodySize::Sized(size) => {
assert_eq!(size, expected.as_bytes().len() as u64);
let body_bytes = actix_web::body::to_bytes(body).await.unwrap();
let body_text = str::from_utf8(&body_bytes).unwrap();
assert_eq!(expected, body_text);
}
_ => {}
}
}
#[actix_web::test]
async fn test_upload() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.max_content_length = Byte::from_bytes(100);
let mut app = test::init_service(
App::new()
.app_data(Data::new(RwLock::new(config)))
.app_data(Data::new(Client::default()))
.service(serve)
.service(upload),
)
.await;
let file_name = "upload_test.txt";
let timestamp = util::get_system_time()?.as_secs().to_string();
let response = test::call_service(
&mut app,
get_multipart_request(&timestamp, file_name).to_request(),
)
.await;
assert_body(response, &format!("http://localhost:8080/{}\n", file_name)).await;
let serve_request = TestRequest::get()
.uri(&format!("/{}", file_name))
.to_request();
let response = test::call_service(&mut app, serve_request).await;
assert_eq!(http::StatusCode::OK, response.status());
assert_body(response, &timestamp).await;
fs::remove_file(file_name)?;
let serve_request = TestRequest::get()
.uri(&format!("/{}", file_name))
.to_request();
let response = test::call_service(&mut app, serve_request).await;
assert_eq!(http::StatusCode::NOT_FOUND, response.status());
Ok(())
}
} }