use std::net::SocketAddr;
use crate::db;
use crate::filters;
#[tokio::main]
pub async fn start() {
let api = filters::todos(db::conn());
let ip_address = super::config("ip_address"); let socket_address: SocketAddr = ip_address.as_str().parse().unwrap(); warp::serve(api).run(socket_address).await;
}
#[cfg(test)]
mod tests {
use warp::http::StatusCode;
use warp::test::request;
use crate::{
filters,
models::Todo,
db,
log,
};
#[tokio::test]
async fn utest_post() {
let db_conn = db::conn();
let api = filters::todos(db_conn);
let resp = request()
.method("POST")
.path("/todos")
.json(&Todo {
id: 0,
text: "unit test 1".into(),
completed: false,
})
.reply(&api)
.await;
assert_eq!(resp.status(), StatusCode::CREATED);
}
#[tokio::test]
async fn utest_put_unknown() {
log::initialize();
let db_conn = db::conn();
let api = filters::todos(db_conn);
let resp = request()
.method("PUT")
.path("/todos/10")
.header("authorization", "Bearer Bearer admin")
.json(&todo1())
.reply(&api)
.await;
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
fn todo1() -> Todo {
Todo {
id: 10,
text: "test 10".into(),
completed: false,
}
}
}