Skip to content

Commit b4b372a

Browse files
committed
snaps
1 parent d531013 commit b4b372a

File tree

1 file changed

+65
-30
lines changed
  • lib/executor/src/headers

1 file changed

+65
-30
lines changed

lib/executor/src/headers/mod.rs

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ mod tests {
2828
HeaderValue::from_str(s).unwrap()
2929
}
3030

31+
trait HeaderMapAsStringExt {
32+
fn to_string(&self) -> String;
33+
}
34+
35+
impl HeaderMapAsStringExt for HeaderMap {
36+
fn to_string(&self) -> String {
37+
let mut buffer = String::new();
38+
39+
for (name, value) in self.iter() {
40+
buffer.push_str(&format!(
41+
"{}: {}\n",
42+
name.as_str(),
43+
value.to_str().unwrap_or("<invalid utf8>")
44+
));
45+
}
46+
47+
buffer
48+
}
49+
}
50+
3151
#[test]
3252
fn test_build_subgraph_headers_propagate_and_set() {
3353
let yaml_str = r#"
@@ -65,8 +85,10 @@ mod tests {
6585
let mut out = HeaderMap::new();
6686
modify_subgraph_request_headers(&plan, "any", &client_details, &mut out).unwrap();
6787

68-
assert_eq!(out.get("x-renamed").unwrap(), &header_value_owned("abc"));
69-
assert_eq!(out.get("x-set").unwrap(), &header_value_owned("set-value"));
88+
insta::assert_snapshot!(out.to_string(), @r#"
89+
x-renamed: abc
90+
x-set: set-value
91+
"#);
7092
}
7193

7294
#[test]
@@ -95,10 +117,9 @@ mod tests {
95117
let mut out = HeaderMap::new();
96118
modify_subgraph_request_headers(&plan, "any", &client_details, &mut out).unwrap();
97119

98-
assert_eq!(
99-
out.get("x-missing").unwrap(),
100-
&header_value_owned("default-value")
101-
);
120+
insta::assert_snapshot!(out.to_string(), @r#"
121+
x-missing: default-value
122+
"#);
102123
}
103124

104125
// Tests that `matching` and `exclude` rules are correctly applied for propagation.
@@ -110,7 +131,7 @@ mod tests {
110131
request:
111132
- propagate:
112133
matching: "^x-.*"
113-
exclude: "^x-secret-.*"
134+
exclude: ["^x-secret-.*"]
114135
"#;
115136
let config = parse_yaml_config(String::from(yaml_str)).unwrap();
116137
let plan = compile_headers_plan(&config.headers).unwrap();
@@ -146,6 +167,10 @@ mod tests {
146167
assert_eq!(out.get("x-forward-this").unwrap(), "value1");
147168
assert!(out.get("x-secret-header").is_none());
148169
assert!(out.get("authorization").is_none());
170+
171+
insta::assert_snapshot!(out.to_string(), @r#"
172+
x-forward-this: value1
173+
"#);
149174
}
150175

151176
// Tests inserting a header with a value from a VRL expression.
@@ -176,7 +201,9 @@ mod tests {
176201
let mut out = HeaderMap::new();
177202
modify_subgraph_request_headers(&plan, "any", &client_details, &mut out).unwrap();
178203

179-
assert_eq!(out.get("x-operation-name").unwrap(), "MyQuery");
204+
insta::assert_snapshot!(out.to_string(), @r#"
205+
x-operation-name: MyQuery
206+
"#);
180207
}
181208

182209
// Tests VRL expression fallback to a default value when a field is null.
@@ -207,7 +234,9 @@ mod tests {
207234
let mut out = HeaderMap::new();
208235
modify_subgraph_request_headers(&plan, "any", &client_details, &mut out).unwrap();
209236

210-
assert_eq!(out.get("x-operation-name").unwrap(), "unknown");
237+
insta::assert_snapshot!(out.to_string(), @r#"
238+
x-operation-name: unknown
239+
"#);
211240
}
212241

213242
// Tests that subgraph-specific rules override global `all` rules.
@@ -245,13 +274,19 @@ mod tests {
245274
let mut out_accounts = HeaderMap::new();
246275
modify_subgraph_request_headers(&plan, "accounts", &client_details, &mut out_accounts)
247276
.unwrap();
248-
assert_eq!(out_accounts.get("x-scope").unwrap(), "accounts");
277+
278+
insta::assert_snapshot!(out_accounts.to_string(), @r#"
279+
x-scope: accounts
280+
"#);
249281

250282
// For any other subgraph, the `all` rule should apply.
251283
let mut out_other = HeaderMap::new();
252284
modify_subgraph_request_headers(&plan, "products", &client_details, &mut out_other)
253285
.unwrap();
254-
assert_eq!(out_other.get("x-scope").unwrap(), "all");
286+
287+
insta::assert_snapshot!(out_other.to_string(), @r#"
288+
x-scope: all
289+
"#);
255290
}
256291

257292
#[test]
@@ -312,10 +347,9 @@ mod tests {
312347
let mut final_headers = HeaderMap::new();
313348
modify_client_response_headers(accumulator, &mut final_headers).unwrap();
314349

315-
assert_eq!(
316-
final_headers.get("x-resp").unwrap(),
317-
&header_value_owned("resp-value-2")
318-
);
350+
insta::assert_snapshot!(final_headers.to_string(), @r#"
351+
x-resp: resp-value-2
352+
"#);
319353
}
320354

321355
// Tests the `first` algorithm for response header propagation.
@@ -376,10 +410,9 @@ mod tests {
376410
let mut final_headers = HeaderMap::new();
377411
modify_client_response_headers(accumulator, &mut final_headers).unwrap();
378412

379-
assert_eq!(
380-
final_headers.get("x-resp").unwrap(),
381-
&header_value_owned("resp-value-1")
382-
);
413+
insta::assert_snapshot!(final_headers.to_string(), @r#"
414+
x-resp: resp-value-1
415+
"#);
383416
}
384417

385418
// Tests the `append` algorithm for response header propagation.
@@ -433,7 +466,9 @@ mod tests {
433466
let mut final_headers = HeaderMap::new();
434467
modify_client_response_headers(accumulator, &mut final_headers).unwrap();
435468

436-
assert_eq!(final_headers.get("x-stuff").unwrap(), "val1, val2");
469+
insta::assert_snapshot!(final_headers.to_string(), @r#"
470+
x-stuff: val1, val2
471+
"#);
437472
}
438473

439474
// Tests that "never-join" headers like set-cookie are appended as separate fields.
@@ -487,10 +522,10 @@ mod tests {
487522
let mut final_headers = HeaderMap::new();
488523
modify_client_response_headers(accumulator, &mut final_headers).unwrap();
489524

490-
let cookies: Vec<_> = final_headers.get_all("set-cookie").iter().collect();
491-
assert_eq!(cookies.len(), 2);
492-
assert_eq!(cookies[0], "a=1");
493-
assert_eq!(cookies[1], "b=2");
525+
insta::assert_snapshot!(final_headers.to_string(), @r#"
526+
set-cookie: a=1
527+
set-cookie: b=2
528+
"#);
494529
}
495530

496531
// Tests inserting a response header with a value from a VRL expression.
@@ -538,10 +573,9 @@ mod tests {
538573
let mut final_headers = HeaderMap::new();
539574
modify_client_response_headers(accumulator, &mut final_headers).unwrap();
540575

541-
assert_eq!(
542-
final_headers.get("x-original-forwarded-for").unwrap(),
543-
"1.2.3.4"
544-
);
576+
insta::assert_snapshot!(final_headers.to_string(), @r#"
577+
x-original-forwarded-for: 1.2.3.4
578+
"#);
545579
}
546580

547581
#[test]
@@ -580,7 +614,8 @@ mod tests {
580614
let mut out = HeaderMap::new();
581615
modify_subgraph_request_headers(&plan, "any", &client_details, &mut out).unwrap();
582616

583-
assert!(out.get("x-remove").is_none());
584-
assert_eq!(out.get("x-keep").unwrap(), &header_value_owned("hi"));
617+
insta::assert_snapshot!(out.to_string(), @r#"
618+
x-keep: hi
619+
"#);
585620
}
586621
}

0 commit comments

Comments
 (0)