LINE1 10 20 30 40 50 60 701use actix_web::{App, test, web};2use serde_json::Value;34const KEY: &str = "test-key";567const AUTH: (&str, &str) = ("Authorization", "Bearer test-key");89macro_rules! app {10 () => {{11 let db = dial::open(":memory:").unwrap();12 test::init_service(13 App::new()14 .app_data(web::Data::new(db))15 .app_data(web::Data::new(dial::ApiKey(KEY.to_string())))16 .configure(dial::configure),17 )18 .await19 }};20}2122#[actix_web::test]23async fn publish_then_subscribe_with_cursor() {24 let app = app!();2526 for body in ["one", "two", "three"] {27 let req = test::TestRequest::post()28 .uri("/publish/orders")29 .insert_header(AUTH)30 .set_payload(body)31 .to_request();32 let resp = test::call_service(&app, req).await;33 assert_eq!(resp.status(), 201);34 }3536 37 let req = test::TestRequest::post()38 .uri("/publish/billing")39 .insert_header(AUTH)40 .set_payload("other")41 .to_request();42 test::call_service(&app, req).await;4344 let req = test::TestRequest::get()45 .uri("/subscribe/orders")46 .insert_header(AUTH)47 .to_request();48 let msgs: Vec<Value> = test::call_and_read_body_json(&app, req).await;49 let payloads: Vec<&str> = msgs50 .iter()51 .map(|m| m["payload"].as_str().unwrap())52 .collect();53 assert_eq!(payloads, ["one", "two", "three"]);5455 56 let last = msgs[2]["id"].as_i64().unwrap();57 let req = test::TestRequest::get()58 .uri(&format!("/subscribe/orders?after={last}"))59 .insert_header(AUTH)60 .to_request();61 let msgs: Vec<Value> = test::call_and_read_body_json(&app, req).await;62 assert!(msgs.is_empty());6364 65 let req = test::TestRequest::post()66 .uri("/publish/orders")67 .insert_header(AUTH)68 .set_payload("four")69 .to_request();70 test::call_service(&app, req).await;71 let req = test::TestRequest::get()72 .uri(&format!("/subscribe/orders?after={last}"))73 .insert_header(AUTH)74 .to_request();75 let msgs: Vec<Value> = test::call_and_read_body_json(&app, req).await;76 assert_eq!(msgs.len(), 1);77 assert_eq!(msgs[0]["payload"], "four");78}7980#[actix_web::test]81async fn subscribe_respects_limit() {82 let app = app!();8384 for i in 0..5 {85 let req = test::TestRequest::post()86 .uri("/publish/t")87 .insert_header(AUTH)88 .set_payload(format!("m{i}"))89 .to_request();90 test::call_service(&app, req).await;91 }9293 let req = test::TestRequest::get()94 .uri("/subscribe/t?limit=2")95 .insert_header(AUTH)96 .to_request();97 let msgs: Vec<Value> = test::call_and_read_body_json(&app, req).await;98 assert_eq!(msgs.len(), 2);99 assert_eq!(msgs[0]["payload"], "m0");100 assert_eq!(msgs[1]["payload"], "m1");101}102103#[actix_web::test]104async fn health_check_needs_no_key() {105 let app = app!();106 let req = test::TestRequest::get().uri("/up").to_request();107 let resp = test::call_service(&app, req).await;108 assert_eq!(resp.status(), 200);109}110111#[actix_web::test]112async fn publish_and_subscribe_reject_missing_key() {113 let app = app!();114115 let req = test::TestRequest::post()116 .uri("/publish/orders")117 .set_payload("sneaky")118 .to_request();119 let resp = test::call_service(&app, req).await;120 assert_eq!(resp.status(), 401);121122 let req = test::TestRequest::get()123 .uri("/subscribe/orders")124 .to_request();125 let resp = test::call_service(&app, req).await;126 assert_eq!(resp.status(), 401);127}128129#[actix_web::test]130async fn publish_and_subscribe_reject_wrong_key() {131 let app = app!();132133 134 for header in ["Bearer wrong-key", "test-key", "Bearer test-ke"] {135 let req = test::TestRequest::post()136 .uri("/publish/orders")137 .insert_header(("Authorization", header))138 .set_payload("sneaky")139 .to_request();140 let resp = test::call_service(&app, req).await;141 assert_eq!(resp.status(), 401, "publish accepted {header:?}");142143 let req = test::TestRequest::get()144 .uri("/subscribe/orders")145 .insert_header(("Authorization", header))146 .to_request();147 let resp = test::call_service(&app, req).await;148 assert_eq!(resp.status(), 401, "subscribe accepted {header:?}");149 }150}