--- /dev/null
+use actix_web::{get, web, App, HttpRequest, HttpServer, Result};
+use actix_web::http::header;
+use actix_web::middleware::Logger;
+
+#[get("/hello/{name}")]
+async fn hello(params: web::Path<String>, req: HttpRequest) -> Result<String> {
+ let name = params.into_inner();
+
+ match req.headers().get(header::USER_AGENT) {
+ Some(agent) => Ok(format!("Hello {} from {}!", name, agent.to_str().unwrap())),
+ None => Ok(format!("Hello {}!", name))
+ }
+}
+
+#[actix_web::main]
+async fn main() -> std::io::Result<()> {
+ env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));
+
+ HttpServer::new(|| {
+ App::new()
+ .service(hello)
+ .wrap(Logger::default())
+ })
+ .bind(("127.0.0.1", 3030))?
+ .run()
+ .await
+}
+
+#[cfg(test)]
+mod tests {
+ use actix_web::{App, body, test, web};
+ use actix_web::http::header;
+
+ use super::{hello};
+
+ #[actix_web::test]
+ async fn test_hello() {
+ let app = test::init_service(App::new().service(hello)).await;
+
+ // no user-agent
+ let req = test::TestRequest::get().uri("/hello/rust").to_request();
+ let res = test::call_service(&app, req).await;
+ assert!(res.status().is_success());
+ assert_eq!(body::to_bytes(res.into_body()).await.unwrap(),
+ web::Bytes::from_static(b"Hello rust!"));
+
+ // with user-agent
+ let req = test::TestRequest::get()
+ .uri("/hello/rust")
+ .insert_header((header::USER_AGENT, "TestBrowser 0.1"))
+ .to_request();
+ let res = test::call_service(&app, req).await;
+ assert!(res.status().is_success());
+ assert_eq!(body::to_bytes(res.into_body()).await.unwrap(),
+ web::Bytes::from_static(b"Hello rust from TestBrowser 0.1!"));
+ }
+}