use actix_web::{middleware, web, App, HttpRequest, HttpServer}; fn index(req: HttpRequest) -> &'static str { println!("REQ: {:?}", req); "Hello world!" } fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); HttpServer::new(|| { App::new() // enable logger .wrap(middleware::Logger::default()) .service(web::resource("/index.html").to(|| "Hello world!")) .service(web::resource("/").to(index)) }) .bind("127.0.0.1:8080")? .run() } #[cfg(test)] mod tests { use super::*; use actix_web::dev::Service; use actix_web::{test, web, App, http, Error}; #[test] fn test_index() -> Result<(), Error> { let app = App::new().route("/", web::get().to(index)); let mut app = test::init_service(app); let req = test::TestRequest::get() .uri("/") .to_request(); let resp = test::block_on(app.call(req)).unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); let response_body = match resp.response().body().as_ref() { Some(actix_web::body::Body::Bytes(bytes)) => bytes, _ => panic!("Response error"), }; assert_eq!(response_body, r##"Hello world!"##); Ok(()) } }