rocket-pinboard/src/handlers/home.rs

53 lines
1.4 KiB
Rust
Raw Normal View History

2018-12-18 15:47:34 +01:00
use std::collections::HashMap;
use rocket::serde::Serialize;
use rocket::{get, State};
use rocket_dyn_templates::Template;
use super::super::{Config, NoteStore};
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct IndexTemplateContext<'a> {
2018-02-15 20:49:42 +01:00
app_version: &'a str,
notes: Vec<HashMap<&'a str, &'a str>>,
title: String,
}
#[get("/")]
pub(crate) async fn index(notes: &State<NoteStore>, config: &State<Config>) -> Template {
let notes = notes.read().unwrap();
let mut note_kvs = vec![];
for note in notes.iter() {
let mut note_kv = HashMap::new();
note_kv.insert("id", note.id.as_ref());
note_kv.insert("name", note.name.as_ref());
note_kvs.push(note_kv);
}
let context = IndexTemplateContext {
app_version: env!("CARGO_PKG_VERSION"),
2018-02-15 20:49:42 +01:00
notes: note_kvs,
title: config.title.clone(),
};
Template::render("index", &context)
}
2018-01-01 21:16:47 +01:00
#[cfg(test)]
mod tests {
use rocket;
use rocket::http::{Accept, Status};
use rocket::local::blocking::Client;
2018-01-01 21:16:47 +01:00
#[test]
fn index() {
2022-10-17 19:18:35 +02:00
let client = Client::untracked(crate::build_rocket(Some("test"))).unwrap();
2018-01-01 21:16:47 +01:00
// Try to get the index and verify the body
let res = client.get("/").header(Accept::HTML).dispatch();
2018-01-01 21:16:47 +01:00
assert_eq!(res.status(), Status::Ok);
let body = res.into_string().unwrap();
assert!(body.contains("<span id=\"note-name\">Test</span>"));
2018-01-01 21:16:47 +01:00
}
}