From 1d44cbd9c88c743a0c161ac8aa40fab3abf3b6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Mon, 16 May 2022 10:25:59 +0300 Subject: [PATCH] test(server): add test for upload --- src/server.rs | 95 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 10 deletions(-) diff --git a/src/server.rs b/src/server.rs index 690830f..138deb1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -211,26 +211,101 @@ pub fn configure_routes(cfg: &mut web::ServiceConfig) { #[cfg(test)] mod tests { 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] async fn test_index() { 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")) .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_eq!(http::StatusCode::FOUND, resp.status()); } - #[actix_web::test] - async fn test_serve() { - let mut app = test::init_service(App::new().service(serve)).await; - let req = test::TestRequest::default().to_request(); - let resp = test::call_service(&mut app, req).await; - assert_eq!(http::StatusCode::NOT_FOUND, resp.status()); + fn get_multipart_request(data: &str, file_name: &str) -> TestRequest { + let multipart_data = format!( + "\r\n\ + --multipart_bound\r\n\ + Content-Disposition: form-data; name=\"file\"; filename=\"{}\"\r\n\ + 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(×tamp, 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, ×tamp).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(()) + } }