From e0cd69054ec26342226c7a392513d88604310a03 Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Fri, 5 Jan 2024 10:41:31 +0100 Subject: [PATCH] task: Added version header to client register response --- server/src/client_api.rs | 43 ++++++++++++++++++++++++++++++++-------- server/src/types.rs | 2 +- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/server/src/client_api.rs b/server/src/client_api.rs index 31cf9411..009f279b 100644 --- a/server/src/client_api.rs +++ b/server/src/client_api.rs @@ -5,7 +5,7 @@ use crate::filters::{ use crate::http::feature_refresher::FeatureRefresher; use crate::metrics::client_metrics::MetricsCache; use crate::tokens::cache_key; -use crate::types::{EdgeJsonResult, EdgeResult, EdgeToken, FeatureFilters}; +use crate::types::{self, EdgeJsonResult, EdgeResult, EdgeToken, FeatureFilters}; use actix_web::web::{self, Data, Json, Query}; use actix_web::{get, post, HttpRequest, HttpResponse}; use dashmap::DashMap; @@ -171,7 +171,9 @@ pub async fn register( client_application.into_inner(), metrics_cache, ); - Ok(HttpResponse::Accepted().finish()) + Ok(HttpResponse::Accepted() + .append_header(("X-Edge-Version", types::EDGE_VERSION)) + .finish()) } #[utoipa::path( @@ -476,6 +478,31 @@ mod tests { assert_eq!(saved_app.connect_via, Some(vec![our_app])); } + #[tokio::test] + async fn register_endpoint_returns_version_header() { + let metrics_cache = Arc::new(MetricsCache::default()); + let our_app = ConnectVia { + app_name: "test".into(), + instance_id: Ulid::new().to_string(), + }; + let app = test::init_service( + App::new() + .app_data(Data::new(our_app.clone())) + .app_data(Data::from(metrics_cache.clone())) + .service(web::scope("/api/client").service(register)), + ) + .await; + let mut client_app = ClientApplication::new("test_application", 15); + client_app.instance_id = Some("test_instance".into()); + let req = make_register_post_request(client_app.clone()).await; + let res = test::call_service(&app, req).await; + assert_eq!(res.status(), StatusCode::ACCEPTED); + assert_eq!( + res.headers().get("X-Edge-Version").unwrap(), + types::EDGE_VERSION + ); + } + #[tokio::test] async fn client_features_endpoint_correctly_returns_cached_features() { let features_cache: Arc> = Arc::new(DashMap::default()); @@ -1031,11 +1058,11 @@ mod tests { let res = test::call_service(&app, request).await; assert_eq!(res.status(), StatusCode::OK); let request = test::TestRequest::get() - .uri("/api/client/features") - .insert_header(ContentType::json()) - .insert_header(("ShouldNotWork", production_token.token.clone())) - .to_request(); - let res = test::call_service(&app, request).await; - assert_eq!(res.status(), StatusCode::FORBIDDEN); + .uri("/api/client/features") + .insert_header(ContentType::json()) + .insert_header(("ShouldNotWork", production_token.token.clone())) + .to_request(); + let res = test::call_service(&app, request).await; + assert_eq!(res.status(), StatusCode::FORBIDDEN); } } diff --git a/server/src/types.rs b/server/src/types.rs index 46858f50..a629a40c 100644 --- a/server/src/types.rs +++ b/server/src/types.rs @@ -364,8 +364,8 @@ pub struct BuildInfo { pub build_os: String, pub build_target: String, } - shadow!(build); // Get build information set to build placeholder +pub const EDGE_VERSION: &str = build::PKG_VERSION; impl Default for BuildInfo { fn default() -> Self { BuildInfo {