|
| 1 | +use std::sync::Arc; |
| 2 | +use warp::Filter; |
| 3 | + |
| 4 | +use crate::http_server::routes::middlewares; |
| 5 | +use crate::DependencyContainer; |
| 6 | + |
| 7 | +pub fn routes( |
| 8 | + dependency_manager: Arc<DependencyContainer>, |
| 9 | +) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { |
| 10 | + post_statistics(dependency_manager) |
| 11 | +} |
| 12 | + |
| 13 | +/// POST /statistics/snapshot |
| 14 | +fn post_statistics( |
| 15 | + dependency_manager: Arc<DependencyContainer>, |
| 16 | +) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { |
| 17 | + warp::path!("statistics" / "snapshot") |
| 18 | + .and(warp::post()) |
| 19 | + .and(warp::body::json()) |
| 20 | + .and(middlewares::with_event_transmitter( |
| 21 | + dependency_manager.clone(), |
| 22 | + )) |
| 23 | + .and_then(handlers::post_snapshot_statistics) |
| 24 | +} |
| 25 | + |
| 26 | +mod handlers { |
| 27 | + use std::{convert::Infallible, sync::Arc}; |
| 28 | + |
| 29 | + use mithril_common::messages::SnapshotMessage; |
| 30 | + use reqwest::StatusCode; |
| 31 | + |
| 32 | + use crate::event_store::{EventMessage, TransmitterService}; |
| 33 | + use crate::http_server::routes::reply; |
| 34 | + |
| 35 | + pub async fn post_snapshot_statistics( |
| 36 | + snapshot_message: SnapshotMessage, |
| 37 | + event_transmitter: Arc<TransmitterService<EventMessage>>, |
| 38 | + ) -> Result<impl warp::Reply, Infallible> { |
| 39 | + let headers: Vec<(&str, &str)> = Vec::new(); |
| 40 | + |
| 41 | + match event_transmitter.send_event_message( |
| 42 | + "HTTP::statistics", |
| 43 | + "snapshot_downloaded", |
| 44 | + &snapshot_message, |
| 45 | + headers, |
| 46 | + ) { |
| 47 | + Err(e) => Ok(reply::internal_server_error(e)), |
| 48 | + Ok(_) => Ok(reply::empty(StatusCode::CREATED)), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use super::*; |
| 56 | + |
| 57 | + use mithril_common::messages::SnapshotMessage; |
| 58 | + use mithril_common::test_utils::apispec::APISpec; |
| 59 | + |
| 60 | + use warp::{http::Method, test::request}; |
| 61 | + |
| 62 | + use crate::{ |
| 63 | + dependency_injection::DependenciesBuilder, http_server::SERVER_BASE_PATH, Configuration, |
| 64 | + }; |
| 65 | + |
| 66 | + fn setup_router( |
| 67 | + dependency_manager: Arc<DependencyContainer>, |
| 68 | + ) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone { |
| 69 | + let cors = warp::cors() |
| 70 | + .allow_any_origin() |
| 71 | + .allow_headers(vec!["content-type"]) |
| 72 | + .allow_methods(vec![Method::GET, Method::POST, Method::OPTIONS]); |
| 73 | + |
| 74 | + warp::any() |
| 75 | + .and(warp::path(SERVER_BASE_PATH)) |
| 76 | + .and(routes(dependency_manager).with(cors)) |
| 77 | + } |
| 78 | + |
| 79 | + #[tokio::test] |
| 80 | + async fn post_statistics_ok() { |
| 81 | + let config = Configuration::new_sample(); |
| 82 | + let mut builder = DependenciesBuilder::new(config); |
| 83 | + let mut rx = builder.get_event_transmitter_receiver().await.unwrap(); |
| 84 | + let dependency_manager = builder.build_dependency_container().await.unwrap(); |
| 85 | + let snapshot_message = SnapshotMessage::dummy(); |
| 86 | + |
| 87 | + let method = Method::POST.as_str(); |
| 88 | + let path = "/statistics/snapshot"; |
| 89 | + |
| 90 | + let response = request() |
| 91 | + .method(method) |
| 92 | + .json(&snapshot_message) |
| 93 | + .path(&format!("/{SERVER_BASE_PATH}{path}")) |
| 94 | + .reply(&setup_router(Arc::new(dependency_manager))) |
| 95 | + .await; |
| 96 | + |
| 97 | + APISpec::verify_conformity( |
| 98 | + APISpec::get_all_spec_files(), |
| 99 | + method, |
| 100 | + path, |
| 101 | + "application/json", |
| 102 | + &snapshot_message, |
| 103 | + &response, |
| 104 | + ); |
| 105 | + |
| 106 | + let _ = rx.try_recv().unwrap(); |
| 107 | + } |
| 108 | +} |
0 commit comments