1
0
mirror of https://github.com/actix/examples synced 2025-02-25 18:42:50 +01:00

126 lines
3.8 KiB
Rust
Raw Normal View History

2019-03-26 04:29:00 +01:00
#[macro_use]
extern crate actix_web;
2019-03-12 21:15:53 +01:00
2019-03-26 04:29:00 +01:00
use std::collections::HashMap;
2019-04-01 01:23:31 +02:00
use actix_web::{middleware::Logger, web, App, HttpServer, Responder};
2019-03-26 04:29:00 +01:00
use yarte::Template;
#[derive(Template)]
#[template(path = "index.hbs")]
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-04-01 01:23:31 +02:00
pub 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
fn main() -> std::io::Result<()> {
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")?
.run()
}
#[cfg(test)]
mod test {
use super::*;
use actix_http::HttpService;
use actix_http_test::TestServer;
use actix_web::{http, App};
use bytes::Bytes;
#[test]
fn test() {
let mut srv = TestServer::new(|| HttpService::new(App::new().service(index)));
let req = srv.get("/");
let mut response = srv.block_on(req.send()).unwrap();
2019-04-01 01:23:31 +02:00
assert!(response.status().is_success());
assert_eq!(
response.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html"
);
let bytes = srv.block_on(response.body()).unwrap();
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()
)
);
let req = srv.get("/?name=foo&lastname=bar");
let mut response = srv.block_on(req.send()).unwrap();
2019-04-01 01:23:31 +02:00
assert!(response.status().is_success());
assert_eq!(
response.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html"
);
let bytes = srv.block_on(response.body()).unwrap();
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()
)
);
let req = srv.get("/?name=foo");
let mut response = srv.block_on(req.send()).unwrap();
2019-04-01 01:23:31 +02:00
assert!(response.status().is_server_error());
let req = srv.get("/?lastname=bar");
let mut response = srv.block_on(req.send()).unwrap();
2019-04-01 01:23:31 +02:00
assert!(response.status().is_success());
assert_eq!(
response.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html"
);
let bytes = srv.block_on(response.body()).unwrap();
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
}