|
| 1 | +use crate::{ |
| 2 | + agent_handler, |
| 3 | + common::{JsonWrapper, API_VERSION}, |
| 4 | + config, errors_handler, keys_handler, notifications_handler, |
| 5 | + quotes_handler, |
| 6 | +}; |
| 7 | +use actix_web::{http, web, HttpRequest, HttpResponse, Responder, Scope}; |
| 8 | +use log::*; |
| 9 | +use thiserror::Error; |
| 10 | + |
| 11 | +pub const SUPPORTED_API_VERSIONS: &[&str] = &[API_VERSION]; |
| 12 | + |
| 13 | +#[derive(Error, Debug, PartialEq)] |
| 14 | +pub enum APIError { |
| 15 | + #[error("API version \"{0}\" not supported")] |
| 16 | + UnsupportedVersion(String), |
| 17 | +} |
| 18 | + |
| 19 | +/// Handles the default case for the API version scope |
| 20 | +async fn api_default(req: HttpRequest) -> impl Responder { |
| 21 | + let error; |
| 22 | + let response; |
| 23 | + let message; |
| 24 | + |
| 25 | + match req.head().method { |
| 26 | + http::Method::GET => { |
| 27 | + error = 400; |
| 28 | + message = |
| 29 | + "Not Implemented: Use /agent, /keys, or /quotes interfaces"; |
| 30 | + response = HttpResponse::BadRequest() |
| 31 | + .json(JsonWrapper::error(error, message)); |
| 32 | + } |
| 33 | + http::Method::POST => { |
| 34 | + error = 400; |
| 35 | + message = |
| 36 | + "Not Implemented: Use /keys or /notifications interfaces"; |
| 37 | + response = HttpResponse::BadRequest() |
| 38 | + .json(JsonWrapper::error(error, message)); |
| 39 | + } |
| 40 | + _ => { |
| 41 | + error = 405; |
| 42 | + message = "Method is not supported"; |
| 43 | + response = HttpResponse::MethodNotAllowed() |
| 44 | + .insert_header(http::header::Allow(vec![ |
| 45 | + http::Method::GET, |
| 46 | + http::Method::POST, |
| 47 | + ])) |
| 48 | + .json(JsonWrapper::error(error, message)); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + warn!( |
| 53 | + "{} returning {} response. {}", |
| 54 | + req.head().method, |
| 55 | + error, |
| 56 | + message |
| 57 | + ); |
| 58 | + |
| 59 | + response |
| 60 | +} |
| 61 | + |
| 62 | +/// Configure the endpoints supported by API version 2.1 |
| 63 | +/// |
| 64 | +/// Version 2.1 is the base API version |
| 65 | +fn configure_api_v2_1(cfg: &mut web::ServiceConfig) { |
| 66 | + _ = cfg |
| 67 | + .service( |
| 68 | + web::scope("/keys") |
| 69 | + .configure(keys_handler::configure_keys_endpoints), |
| 70 | + ) |
| 71 | + .service(web::scope("/notifications").configure( |
| 72 | + notifications_handler::configure_notifications_endpoints, |
| 73 | + )) |
| 74 | + .service( |
| 75 | + web::scope("/quotes") |
| 76 | + .configure(quotes_handler::configure_quotes_endpoints), |
| 77 | + ) |
| 78 | + .default_service(web::to(api_default)) |
| 79 | +} |
| 80 | + |
| 81 | +/// Configure the endpoints supported by API version 2.2 |
| 82 | +/// |
| 83 | +/// The version 2.2 added the /agent/info endpoint |
| 84 | +fn configure_api_v2_2(cfg: &mut web::ServiceConfig) { |
| 85 | + // Configure the endpoints shared with version 2.1 |
| 86 | + configure_api_v2_1(cfg); |
| 87 | + |
| 88 | + // Configure added endpoints |
| 89 | + _ = cfg.service( |
| 90 | + web::scope("/agent") |
| 91 | + .configure(agent_handler::configure_agent_endpoints), |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +/// Get a scope configured for the given API version |
| 96 | +pub(crate) fn get_api_scope(version: &str) -> Result<Scope, APIError> { |
| 97 | + match version { |
| 98 | + "v2.1" => Ok(web::scope(version).configure(configure_api_v2_1)), |
| 99 | + "v2.2" => Ok(web::scope(version).configure(configure_api_v2_2)), |
| 100 | + _ => Err(APIError::UnsupportedVersion(version.into())), |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +#[cfg(test)] |
| 105 | +mod tests { |
| 106 | + use super::*; |
| 107 | + use actix_web::{test, web, App}; |
| 108 | + use serde_json::{json, Value}; |
| 109 | + |
| 110 | + #[actix_rt::test] |
| 111 | + async fn test_configure_api() { |
| 112 | + // Test that invalid version results in error |
| 113 | + let result = get_api_scope("invalid"); |
| 114 | + assert!(result.is_err()); |
| 115 | + if let Err(e) = result { |
| 116 | + assert_eq!(e, APIError::UnsupportedVersion("invalid".into())); |
| 117 | + } |
| 118 | + |
| 119 | + // Test that a valid version is successful |
| 120 | + let version = SUPPORTED_API_VERSIONS.last().unwrap(); //#[allow_ci] |
| 121 | + let result = get_api_scope(version); |
| 122 | + assert!(result.is_ok()); |
| 123 | + let scope = result.unwrap(); //#[allow_ci] |
| 124 | + } |
| 125 | + |
| 126 | + #[actix_rt::test] |
| 127 | + async fn test_api_default() { |
| 128 | + let mut app = test::init_service( |
| 129 | + App::new().service(web::resource("/").to(api_default)), |
| 130 | + ) |
| 131 | + .await; |
| 132 | + |
| 133 | + let req = test::TestRequest::get().uri("/").to_request(); |
| 134 | + |
| 135 | + let resp = test::call_service(&app, req).await; |
| 136 | + assert!(resp.status().is_client_error()); |
| 137 | + |
| 138 | + let result: JsonWrapper<Value> = test::read_body_json(resp).await; |
| 139 | + |
| 140 | + assert_eq!(result.results, json!({})); |
| 141 | + assert_eq!(result.code, 400); |
| 142 | + |
| 143 | + let req = test::TestRequest::post() |
| 144 | + .uri("/") |
| 145 | + .data("some data") |
| 146 | + .to_request(); |
| 147 | + |
| 148 | + let resp = test::call_service(&app, req).await; |
| 149 | + assert!(resp.status().is_client_error()); |
| 150 | + |
| 151 | + let result: JsonWrapper<Value> = test::read_body_json(resp).await; |
| 152 | + |
| 153 | + assert_eq!(result.results, json!({})); |
| 154 | + assert_eq!(result.code, 400); |
| 155 | + |
| 156 | + let req = test::TestRequest::delete().uri("/").to_request(); |
| 157 | + |
| 158 | + let resp = test::call_service(&app, req).await; |
| 159 | + assert!(resp.status().is_client_error()); |
| 160 | + |
| 161 | + let headers = resp.headers(); |
| 162 | + |
| 163 | + assert!(headers.contains_key("allow")); |
| 164 | + assert_eq!( |
| 165 | + headers.get("allow").unwrap().to_str().unwrap(), //#[allow_ci] |
| 166 | + "GET, POST" |
| 167 | + ); |
| 168 | + |
| 169 | + let result: JsonWrapper<Value> = test::read_body_json(resp).await; |
| 170 | + |
| 171 | + assert_eq!(result.results, json!({})); |
| 172 | + assert_eq!(result.code, 405); |
| 173 | + } |
| 174 | +} |
0 commit comments