mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
Update template yarte (#210)
This commit is contained in:
parent
22c7d747c2
commit
22fd5f3869
@ -31,7 +31,7 @@ members = [
|
||||
"template_askama",
|
||||
"template_handlebars",
|
||||
"template_tera",
|
||||
# "template_yarte",
|
||||
"template_yarte",
|
||||
"todo",
|
||||
# "udp-echo",
|
||||
"unix-socket",
|
||||
|
@ -10,12 +10,13 @@ workspace = ".."
|
||||
[dependencies]
|
||||
env_logger = "0.7"
|
||||
|
||||
yarte = { version = "0.3", features=["with-actix-web"] }
|
||||
yarte = { git = "https://github.com/botika/yarte", features = ["with-actix-web"] }
|
||||
|
||||
actix-web = "1.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "2.0.0-alpha"
|
||||
|
||||
[build-dependencies]
|
||||
yarte = { version = "0.3", features=["with-actix-web"] }
|
||||
yarte = { git = "https://github.com/botika/yarte", features = ["with-actix-web"] }
|
||||
|
||||
[dev-dependencies]
|
||||
bytes = "0.4"
|
||||
|
@ -1,44 +1,42 @@
|
||||
#[macro_use]
|
||||
extern crate actix_web;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{middleware::Logger, web, App, HttpServer, Responder};
|
||||
use actix_web::{get, middleware::Logger, web, App, HttpServer, Responder};
|
||||
use yarte::Template;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.hbs")]
|
||||
#[template(path = "index.hbs", err = "Some error message")]
|
||||
struct IndexTemplate {
|
||||
query: web::Query<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
pub fn index(query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
async fn index(query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||
IndexTemplate { query }
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
// start http server
|
||||
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.start()
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use actix_web::{http, test as atest};
|
||||
use bytes::Bytes;
|
||||
use actix_web::{http, test as atest, web::Bytes};
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let mut app = atest::init_service(App::new().service(index));
|
||||
#[actix_rt::test]
|
||||
async fn test() {
|
||||
let mut app = atest::init_service(App::new().service(index)).await;
|
||||
|
||||
let req = atest::TestRequest::with_uri("/").to_request();
|
||||
let resp = atest::call_service(&mut app, req);
|
||||
let resp = atest::call_service(&mut app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
@ -47,7 +45,7 @@ mod test {
|
||||
"text/html; charset=utf-8"
|
||||
);
|
||||
|
||||
let bytes = atest::read_body(resp);
|
||||
let bytes = atest::read_body(resp).await;
|
||||
assert_eq!(
|
||||
bytes,
|
||||
Bytes::from_static(
|
||||
@ -67,7 +65,7 @@ mod test {
|
||||
);
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request();
|
||||
let resp = atest::call_service(&mut app, req);
|
||||
let resp = atest::call_service(&mut app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
@ -76,7 +74,7 @@ mod test {
|
||||
"text/html; charset=utf-8"
|
||||
);
|
||||
|
||||
let bytes = atest::read_body(resp);
|
||||
let bytes = atest::read_body(resp).await;
|
||||
assert_eq!(
|
||||
bytes,
|
||||
Bytes::from_static(
|
||||
@ -91,12 +89,16 @@ mod test {
|
||||
);
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?name=foo").to_request();
|
||||
let resp = atest::call_service(&mut app, req);
|
||||
let resp = atest::call_service(&mut app, req).await;
|
||||
|
||||
assert!(resp.status().is_server_error());
|
||||
|
||||
let bytes = atest::read_body(resp).await;
|
||||
|
||||
assert_eq!(bytes, Bytes::from_static("Some error message".as_ref()));
|
||||
|
||||
let req = atest::TestRequest::with_uri("/?lastname=bar").to_request();
|
||||
let resp = atest::call_service(&mut app, req);
|
||||
let resp = atest::call_service(&mut app, req).await;
|
||||
|
||||
assert!(resp.status().is_success());
|
||||
|
||||
@ -105,7 +107,7 @@ mod test {
|
||||
"text/html; charset=utf-8"
|
||||
);
|
||||
|
||||
let bytes = atest::read_body(resp);
|
||||
let bytes = atest::read_body(resp).await;
|
||||
assert_eq!(
|
||||
bytes,
|
||||
Bytes::from_static(
|
||||
|
Loading…
Reference in New Issue
Block a user