1
0
mirror of https://github.com/actix/examples synced 2025-02-26 02:52:49 +01:00

130 lines
4.0 KiB
Rust
Raw Normal View History

2019-03-26 04:29:00 +01:00
use std::collections::HashMap;
2019-12-15 18:07:58 +01:00
use actix_web::{get, middleware::Logger, web, App, HttpServer, Responder};
2019-03-26 04:29:00 +01:00
use yarte::Template;
#[derive(Template)]
2019-12-15 18:07:58 +01:00
#[template(path = "index.hbs", err = "Some error message")]
2019-03-26 04:29:00 +01:00
struct IndexTemplate {
2019-04-01 01:23:31 +02:00
query: web::Query<HashMap<String, String>>,
2019-03-26 04:29:00 +01:00
}
#[get("/")]
2019-12-15 18:07:58 +01:00
async fn index(query: web::Query<HashMap<String, String>>) -> impl Responder {
2019-03-26 04:29:00 +01:00
IndexTemplate { query }
}
2019-03-12 21:15:53 +01:00
2019-12-15 18:07:58 +01:00
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
2019-03-12 21:15:53 +01:00
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
// start http server
2019-04-01 01:23:31 +02:00
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
.bind("127.0.0.1:8080")?
2019-12-25 20:48:33 +04:00
.run()
2019-12-15 18:07:58 +01:00
.await
2019-04-01 01:23:31 +02:00
}
#[cfg(test)]
mod test {
use super::*;
2019-12-15 18:07:58 +01:00
use actix_web::{http, test as atest, web::Bytes};
2019-04-01 01:23:31 +02:00
2019-12-15 18:07:58 +01:00
#[actix_rt::test]
async fn test() {
let mut app = atest::init_service(App::new().service(index)).await;
2019-04-01 01:23:31 +02:00
2019-07-01 06:02:14 +02:00
let req = atest::TestRequest::with_uri("/").to_request();
2019-12-15 18:07:58 +01:00
let resp = atest::call_service(&mut app, req).await;
2019-07-01 06:02:14 +02:00
assert!(resp.status().is_success());
2019-04-01 01:23:31 +02:00
assert_eq!(
2019-07-01 06:02:14 +02:00
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html; charset=utf-8"
2019-04-01 01:23:31 +02:00
);
2019-12-15 18:07:58 +01:00
let bytes = atest::read_body(resp).await;
2019-04-01 01:23:31 +02:00
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\" /><title>Actix web</title></head><body>\
<h1 id=\"welcome\" class=\"welcome\">Welcome!</h1><div>\
<h3>What is your name?</h3>\
<form>\
Name: <input type=\"text\" name=\"name\" />\
<br/>Last name: <input type=\"text\" name=\"lastname\" />\
<br/><p><input type=\"submit\"></p></form>\
</div>\
</body></html>"
.as_ref()
)
);
2019-07-01 06:02:14 +02:00
let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request();
2019-12-15 18:07:58 +01:00
let resp = atest::call_service(&mut app, req).await;
2019-07-01 06:02:14 +02:00
assert!(resp.status().is_success());
2019-04-01 01:23:31 +02:00
assert_eq!(
2019-07-01 06:02:14 +02:00
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html; charset=utf-8"
2019-04-01 01:23:31 +02:00
);
2019-12-15 18:07:58 +01:00
let bytes = atest::read_body(resp).await;
2019-04-01 01:23:31 +02:00
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\" /><title>Actix web</title></head>\
<body>\
<h1>Hi, foo bar!</h1><p id=\"hi\" class=\"welcome\">Welcome</p>\
</body></html>"
.as_ref()
)
);
2019-07-01 06:02:14 +02:00
let req = atest::TestRequest::with_uri("/?name=foo").to_request();
2019-12-15 18:07:58 +01:00
let resp = atest::call_service(&mut app, req).await;
2019-07-01 06:02:14 +02:00
assert!(resp.status().is_server_error());
2019-12-15 18:07:58 +01:00
let bytes = atest::read_body(resp).await;
assert_eq!(bytes, Bytes::from_static("Some error message".as_ref()));
2019-07-01 06:02:14 +02:00
let req = atest::TestRequest::with_uri("/?lastname=bar").to_request();
2019-12-15 18:07:58 +01:00
let resp = atest::call_service(&mut app, req).await;
2019-04-01 01:23:31 +02:00
2019-07-01 06:02:14 +02:00
assert!(resp.status().is_success());
2019-04-01 01:23:31 +02:00
assert_eq!(
2019-07-01 06:02:14 +02:00
resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html; charset=utf-8"
2019-04-01 01:23:31 +02:00
);
2019-12-15 18:07:58 +01:00
let bytes = atest::read_body(resp).await;
2019-04-01 01:23:31 +02:00
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\" /><title>Actix web</title></head><body>\
<h1 id=\"welcome\" class=\"welcome\">Welcome!</h1><div>\
<h3>What is your name?</h3>\
<form>\
Name: <input type=\"text\" name=\"name\" />\
<br/>Last name: <input type=\"text\" name=\"lastname\" />\
<br/><p><input type=\"submit\"></p></form>\
</div>\
</body></html>"
.as_ref()
)
);
}
2019-03-12 21:15:53 +01:00
}