|
| 1 | +//! Context query-parameter forwarding for auction providers. |
| 2 | +//! |
| 3 | +//! Provides a config-driven mechanism for ad-server / mediator providers to |
| 4 | +//! forward integration-supplied data (e.g. audience segments) as URL query |
| 5 | +//! parameters without hard-coding integration-specific knowledge. |
| 6 | +
|
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use std::collections::{BTreeMap, HashMap}; |
| 9 | + |
| 10 | +/// A strongly-typed context value forwarded from the JS client payload. |
| 11 | +/// |
| 12 | +/// Replaces raw `serde_json::Value` so that consumers get compile-time |
| 13 | +/// exhaustiveness checks. The `#[serde(untagged)]` attribute preserves |
| 14 | +/// wire-format compatibility — the JS client sends plain JSON arrays, strings, |
| 15 | +/// or numbers which serde maps to the matching variant in declaration order. |
| 16 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 17 | +#[serde(untagged)] |
| 18 | +pub enum ContextValue { |
| 19 | + /// A list of string values (e.g. audience segment IDs). |
| 20 | + StringList(Vec<String>), |
| 21 | + /// A single string value. |
| 22 | + Text(String), |
| 23 | + /// A numeric value. |
| 24 | + Number(f64), |
| 25 | +} |
| 26 | + |
| 27 | +/// Mapping from auction-request context keys to query-parameter names. |
| 28 | +/// |
| 29 | +/// Used by ad-server / mediator providers to forward integration-supplied data |
| 30 | +/// (e.g. audience segments) as URL query parameters without hard-coding |
| 31 | +/// integration-specific knowledge. |
| 32 | +/// |
| 33 | +/// ```toml |
| 34 | +/// [integrations.adserver_mock.context_query_params] |
| 35 | +/// permutive_segments = "permutive" |
| 36 | +/// lockr_ids = "lockr" |
| 37 | +/// ``` |
| 38 | +pub type ContextQueryParams = BTreeMap<String, String>; |
| 39 | + |
| 40 | +/// Build a URL by appending context values as query parameters according to the |
| 41 | +/// provided mapping. |
| 42 | +/// |
| 43 | +/// For each entry in `mapping`, if the corresponding key exists in `context`: |
| 44 | +/// - **Arrays** are serialised as a comma-separated string. |
| 45 | +/// - **Strings / numbers** are serialised as-is. |
| 46 | +/// - Other JSON types are skipped. |
| 47 | +/// |
| 48 | +/// The [`url::Url`] crate is used for construction so all values are |
| 49 | +/// percent-encoded, preventing query-parameter injection. |
| 50 | +/// |
| 51 | +/// Returns the original `base_url` unchanged when no parameters are appended. |
| 52 | +#[must_use] |
| 53 | +pub fn build_url_with_context_params( |
| 54 | + base_url: &str, |
| 55 | + context: &HashMap<String, ContextValue>, |
| 56 | + mapping: &ContextQueryParams, |
| 57 | +) -> String { |
| 58 | + let Ok(mut url) = url::Url::parse(base_url) else { |
| 59 | + log::warn!("build_url_with_context_params: failed to parse base URL, returning as-is"); |
| 60 | + return base_url.to_string(); |
| 61 | + }; |
| 62 | + |
| 63 | + let mut appended = 0usize; |
| 64 | + |
| 65 | + for (context_key, param_name) in mapping { |
| 66 | + if let Some(value) = context.get(context_key) { |
| 67 | + let serialized = serialize_context_value(value); |
| 68 | + if !serialized.is_empty() { |
| 69 | + url.query_pairs_mut().append_pair(param_name, &serialized); |
| 70 | + appended += 1; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if appended > 0 { |
| 76 | + log::info!( |
| 77 | + "build_url_with_context_params: appended {} context query params", |
| 78 | + appended |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + url.to_string() |
| 83 | +} |
| 84 | + |
| 85 | +/// Serialise a single [`ContextValue`] into a string suitable for a query |
| 86 | +/// parameter value. String lists are joined with commas; strings and numbers |
| 87 | +/// are returned directly. |
| 88 | +fn serialize_context_value(value: &ContextValue) -> String { |
| 89 | + match value { |
| 90 | + ContextValue::StringList(items) => items.join(","), |
| 91 | + ContextValue::Text(s) => s.clone(), |
| 92 | + ContextValue::Number(n) => n.to_string(), |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod tests { |
| 98 | + use super::*; |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_build_url_with_context_params_appends_array() { |
| 102 | + let context = HashMap::from([( |
| 103 | + "permutive_segments".to_string(), |
| 104 | + ContextValue::StringList(vec!["10000001".into(), "10000003".into(), "adv".into()]), |
| 105 | + )]); |
| 106 | + let mapping = BTreeMap::from([("permutive_segments".to_string(), "permutive".to_string())]); |
| 107 | + |
| 108 | + let url = build_url_with_context_params( |
| 109 | + "http://localhost:6767/adserver/mediate", |
| 110 | + &context, |
| 111 | + &mapping, |
| 112 | + ); |
| 113 | + assert_eq!( |
| 114 | + url, |
| 115 | + "http://localhost:6767/adserver/mediate?permutive=10000001%2C10000003%2Cadv" |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_build_url_with_context_params_preserves_existing_query() { |
| 121 | + let context = HashMap::from([( |
| 122 | + "permutive_segments".to_string(), |
| 123 | + ContextValue::StringList(vec!["123".into(), "adv".into()]), |
| 124 | + )]); |
| 125 | + let mapping = BTreeMap::from([("permutive_segments".to_string(), "permutive".to_string())]); |
| 126 | + |
| 127 | + let url = build_url_with_context_params( |
| 128 | + "http://localhost:6767/adserver/mediate?debug=true", |
| 129 | + &context, |
| 130 | + &mapping, |
| 131 | + ); |
| 132 | + assert_eq!( |
| 133 | + url, |
| 134 | + "http://localhost:6767/adserver/mediate?debug=true&permutive=123%2Cadv" |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_build_url_with_context_params_no_matching_keys() { |
| 140 | + let context = HashMap::new(); |
| 141 | + let mapping = BTreeMap::from([("permutive_segments".to_string(), "permutive".to_string())]); |
| 142 | + |
| 143 | + let url = build_url_with_context_params( |
| 144 | + "http://localhost:6767/adserver/mediate", |
| 145 | + &context, |
| 146 | + &mapping, |
| 147 | + ); |
| 148 | + assert_eq!(url, "http://localhost:6767/adserver/mediate"); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn test_build_url_with_context_params_empty_array_skipped() { |
| 153 | + let context = HashMap::from([( |
| 154 | + "permutive_segments".to_string(), |
| 155 | + ContextValue::StringList(vec![]), |
| 156 | + )]); |
| 157 | + let mapping = BTreeMap::from([("permutive_segments".to_string(), "permutive".to_string())]); |
| 158 | + |
| 159 | + let url = build_url_with_context_params( |
| 160 | + "http://localhost:6767/adserver/mediate", |
| 161 | + &context, |
| 162 | + &mapping, |
| 163 | + ); |
| 164 | + assert!(!url.contains("permutive=")); |
| 165 | + } |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn test_build_url_with_context_params_multiple_mappings() { |
| 169 | + let context = HashMap::from([ |
| 170 | + ( |
| 171 | + "permutive_segments".to_string(), |
| 172 | + ContextValue::StringList(vec!["seg1".into()]), |
| 173 | + ), |
| 174 | + ( |
| 175 | + "lockr_ids".to_string(), |
| 176 | + ContextValue::Text("lockr-abc-123".into()), |
| 177 | + ), |
| 178 | + ]); |
| 179 | + let mapping = BTreeMap::from([ |
| 180 | + ("lockr_ids".to_string(), "lockr".to_string()), |
| 181 | + ("permutive_segments".to_string(), "permutive".to_string()), |
| 182 | + ]); |
| 183 | + |
| 184 | + let url = build_url_with_context_params( |
| 185 | + "http://localhost:6767/adserver/mediate", |
| 186 | + &context, |
| 187 | + &mapping, |
| 188 | + ); |
| 189 | + assert!(url.contains("permutive=seg1")); |
| 190 | + assert!(url.contains("lockr=lockr-abc-123")); |
| 191 | + } |
| 192 | + |
| 193 | + #[test] |
| 194 | + fn test_build_url_with_context_params_scalar_number() { |
| 195 | + let context = HashMap::from([("count".to_string(), ContextValue::Number(42.0))]); |
| 196 | + let mapping = BTreeMap::from([("count".to_string(), "n".to_string())]); |
| 197 | + |
| 198 | + let url = build_url_with_context_params( |
| 199 | + "http://localhost:6767/adserver/mediate", |
| 200 | + &context, |
| 201 | + &mapping, |
| 202 | + ); |
| 203 | + assert_eq!(url, "http://localhost:6767/adserver/mediate?n=42"); |
| 204 | + } |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn test_serialize_context_value_string_list() { |
| 208 | + assert_eq!( |
| 209 | + serialize_context_value(&ContextValue::StringList(vec![ |
| 210 | + "a".into(), |
| 211 | + "b".into(), |
| 212 | + "3".into() |
| 213 | + ])), |
| 214 | + "a,b,3" |
| 215 | + ); |
| 216 | + } |
| 217 | + |
| 218 | + #[test] |
| 219 | + fn test_serialize_context_value_text() { |
| 220 | + assert_eq!( |
| 221 | + serialize_context_value(&ContextValue::Text("hello".into())), |
| 222 | + "hello" |
| 223 | + ); |
| 224 | + } |
| 225 | + |
| 226 | + #[test] |
| 227 | + fn test_serialize_context_value_number() { |
| 228 | + assert_eq!(serialize_context_value(&ContextValue::Number(99.0)), "99"); |
| 229 | + } |
| 230 | + |
| 231 | + #[test] |
| 232 | + fn test_context_value_deserialize_array() { |
| 233 | + let v: ContextValue = |
| 234 | + serde_json::from_str(r#"["a","b"]"#).expect("should deserialize string list"); |
| 235 | + assert_eq!(v, ContextValue::StringList(vec!["a".into(), "b".into()])); |
| 236 | + } |
| 237 | + |
| 238 | + #[test] |
| 239 | + fn test_context_value_deserialize_string() { |
| 240 | + let v: ContextValue = serde_json::from_str(r#""hello""#).expect("should deserialize text"); |
| 241 | + assert_eq!(v, ContextValue::Text("hello".into())); |
| 242 | + } |
| 243 | + |
| 244 | + #[test] |
| 245 | + fn test_context_value_deserialize_number() { |
| 246 | + let v: ContextValue = serde_json::from_str("42").expect("should deserialize number"); |
| 247 | + assert_eq!(v, ContextValue::Number(42.0)); |
| 248 | + } |
| 249 | +} |
0 commit comments