@@ -28,6 +28,26 @@ mod tests {
28
28
HeaderValue :: from_str ( s) . unwrap ( )
29
29
}
30
30
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
+
31
51
#[ test]
32
52
fn test_build_subgraph_headers_propagate_and_set ( ) {
33
53
let yaml_str = r#"
@@ -65,8 +85,10 @@ mod tests {
65
85
let mut out = HeaderMap :: new ( ) ;
66
86
modify_subgraph_request_headers ( & plan, "any" , & client_details, & mut out) . unwrap ( ) ;
67
87
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
+ "# ) ;
70
92
}
71
93
72
94
#[ test]
@@ -95,10 +117,9 @@ mod tests {
95
117
let mut out = HeaderMap :: new ( ) ;
96
118
modify_subgraph_request_headers ( & plan, "any" , & client_details, & mut out) . unwrap ( ) ;
97
119
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
+ "# ) ;
102
123
}
103
124
104
125
// Tests that `matching` and `exclude` rules are correctly applied for propagation.
@@ -110,7 +131,7 @@ mod tests {
110
131
request:
111
132
- propagate:
112
133
matching: "^x-.*"
113
- exclude: "^x-secret-.*"
134
+ exclude: [ "^x-secret-.*"]
114
135
"# ;
115
136
let config = parse_yaml_config ( String :: from ( yaml_str) ) . unwrap ( ) ;
116
137
let plan = compile_headers_plan ( & config. headers ) . unwrap ( ) ;
@@ -146,6 +167,10 @@ mod tests {
146
167
assert_eq ! ( out. get( "x-forward-this" ) . unwrap( ) , "value1" ) ;
147
168
assert ! ( out. get( "x-secret-header" ) . is_none( ) ) ;
148
169
assert ! ( out. get( "authorization" ) . is_none( ) ) ;
170
+
171
+ insta:: assert_snapshot!( out. to_string( ) , @r#"
172
+ x-forward-this: value1
173
+ "# ) ;
149
174
}
150
175
151
176
// Tests inserting a header with a value from a VRL expression.
@@ -176,7 +201,9 @@ mod tests {
176
201
let mut out = HeaderMap :: new ( ) ;
177
202
modify_subgraph_request_headers ( & plan, "any" , & client_details, & mut out) . unwrap ( ) ;
178
203
179
- assert_eq ! ( out. get( "x-operation-name" ) . unwrap( ) , "MyQuery" ) ;
204
+ insta:: assert_snapshot!( out. to_string( ) , @r#"
205
+ x-operation-name: MyQuery
206
+ "# ) ;
180
207
}
181
208
182
209
// Tests VRL expression fallback to a default value when a field is null.
@@ -207,7 +234,9 @@ mod tests {
207
234
let mut out = HeaderMap :: new ( ) ;
208
235
modify_subgraph_request_headers ( & plan, "any" , & client_details, & mut out) . unwrap ( ) ;
209
236
210
- assert_eq ! ( out. get( "x-operation-name" ) . unwrap( ) , "unknown" ) ;
237
+ insta:: assert_snapshot!( out. to_string( ) , @r#"
238
+ x-operation-name: unknown
239
+ "# ) ;
211
240
}
212
241
213
242
// Tests that subgraph-specific rules override global `all` rules.
@@ -245,13 +274,19 @@ mod tests {
245
274
let mut out_accounts = HeaderMap :: new ( ) ;
246
275
modify_subgraph_request_headers ( & plan, "accounts" , & client_details, & mut out_accounts)
247
276
. 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
+ "# ) ;
249
281
250
282
// For any other subgraph, the `all` rule should apply.
251
283
let mut out_other = HeaderMap :: new ( ) ;
252
284
modify_subgraph_request_headers ( & plan, "products" , & client_details, & mut out_other)
253
285
. 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
+ "# ) ;
255
290
}
256
291
257
292
#[ test]
@@ -312,10 +347,9 @@ mod tests {
312
347
let mut final_headers = HeaderMap :: new ( ) ;
313
348
modify_client_response_headers ( accumulator, & mut final_headers) . unwrap ( ) ;
314
349
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
+ "# ) ;
319
353
}
320
354
321
355
// Tests the `first` algorithm for response header propagation.
@@ -376,10 +410,9 @@ mod tests {
376
410
let mut final_headers = HeaderMap :: new ( ) ;
377
411
modify_client_response_headers ( accumulator, & mut final_headers) . unwrap ( ) ;
378
412
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
+ "# ) ;
383
416
}
384
417
385
418
// Tests the `append` algorithm for response header propagation.
@@ -433,7 +466,9 @@ mod tests {
433
466
let mut final_headers = HeaderMap :: new ( ) ;
434
467
modify_client_response_headers ( accumulator, & mut final_headers) . unwrap ( ) ;
435
468
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
+ "# ) ;
437
472
}
438
473
439
474
// Tests that "never-join" headers like set-cookie are appended as separate fields.
@@ -487,10 +522,10 @@ mod tests {
487
522
let mut final_headers = HeaderMap :: new ( ) ;
488
523
modify_client_response_headers ( accumulator, & mut final_headers) . unwrap ( ) ;
489
524
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
+ "# ) ;
494
529
}
495
530
496
531
// Tests inserting a response header with a value from a VRL expression.
@@ -538,10 +573,9 @@ mod tests {
538
573
let mut final_headers = HeaderMap :: new ( ) ;
539
574
modify_client_response_headers ( accumulator, & mut final_headers) . unwrap ( ) ;
540
575
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
+ "# ) ;
545
579
}
546
580
547
581
#[ test]
@@ -580,7 +614,8 @@ mod tests {
580
614
let mut out = HeaderMap :: new ( ) ;
581
615
modify_subgraph_request_headers ( & plan, "any" , & client_details, & mut out) . unwrap ( ) ;
582
616
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
+ "# ) ;
585
620
}
586
621
}
0 commit comments