mirror of
https://github.com/actix/examples
synced 2025-02-21 08:44:48 +01:00
48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
use actix_web::{middleware, web, App, HttpRequest, HttpServer};
|
|
|
|
async fn index(req: HttpRequest) -> &'static str {
|
|
println!("REQ: {:?}", req);
|
|
"Hello world!"
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async 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(|| async { "Hello world!" }))
|
|
.service(web::resource("/").to(index))
|
|
})
|
|
.bind("127.0.0.1:8080")?
|
|
.run()
|
|
.await
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use actix_web::body::to_bytes;
|
|
use actix_web::dev::Service;
|
|
use actix_web::{http, test, web, App, Error};
|
|
|
|
#[actix_web::test]
|
|
async fn test_index() -> Result<(), Error> {
|
|
let app = App::new().route("/", web::get().to(index));
|
|
let app = test::init_service(app).await;
|
|
|
|
let req = test::TestRequest::get().uri("/").to_request();
|
|
let resp = app.call(req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), http::StatusCode::OK);
|
|
|
|
let response_body = resp.into_body();
|
|
assert_eq!(to_bytes(response_body).await.unwrap(), r##"Hello world!"##);
|
|
|
|
Ok(())
|
|
}
|
|
}
|