-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.rs
658 lines (586 loc) · 24.5 KB
/
server.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use std::pin::Pin;
use std::process::ExitCode;
use anyhow::anyhow;
use futures::future::Future;
use futures::stream::StreamExt;
use futures::executor::block_on;
use futures::task::{Context, Poll};
use http::{Error, StatusCode};
use hyper::{Body, Request as HyperRequest, Response as HyperResponse, Server};
use itertools::Itertools;
use maplit::*;
use pact_matching::{CoreMatchingContext, DiffConfig, Mismatch};
use pact_models::generators::GeneratorTestMode;
use pact_models::prelude::*;
use pact_models::prelude::v4::*;
use pact_models::v4::http_parts::{HttpRequest, HttpResponse};
use pact_models::v4::V4InteractionType;
use regex::Regex;
use tower_service::Service;
use tracing::{debug, error, info, warn};
use crate::pact_support;
#[derive(Clone)]
pub struct ServerHandler {
sources: Vec<V4Pact>,
auto_cors: bool,
cors_referer: bool,
provider_state: Option<Regex>,
provider_state_header_name: Option<String>,
empty_provider_states: bool
}
impl ServerHandler {
pub fn new(
sources: Vec<V4Pact>,
auto_cors: bool,
cors_referer: bool,
provider_state: Option<Regex>,
provider_state_header_name: Option<String>,
empty_provider_states: bool
) -> ServerHandler {
ServerHandler {
sources,
auto_cors,
cors_referer,
provider_state,
provider_state_header_name,
empty_provider_states
}
}
pub fn start_server(self, port: u16) -> Result<(), ExitCode> {
let addr = ([0, 0, 0, 0], port).into();
match Server::try_bind(&addr) {
Ok(builder) => {
let server = builder
.serve(hyper::service::make_service_fn(|_| {
let inner = self.clone();
async {
Ok::<_, hyper::Error>(inner)
}
}));
info!("Server started on port {}", server.local_addr().port());
block_on(server).map_err(|err| {
error!("error occurred scheduling server future on Tokio runtime: {}", err);
ExitCode::from(2)
})?;
Ok(())
},
Err(err) => {
error!("could not start server: {}", err);
Err(ExitCode::FAILURE)
}
}
}
}
impl Service<HyperRequest<Body>> for ServerHandler {
type Response = HyperResponse<Body>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: HyperRequest<Body>) -> Self::Future {
let auto_cors = self.auto_cors;
let cors_referer = self.cors_referer;
let sources = self.sources.clone();
let provider_state = self.provider_state.clone();
let provider_state_header_name = self.provider_state_header_name.clone();
let empty_provider_states = self.empty_provider_states;
Box::pin(async move {
let (parts, body) = req.into_parts();
let provider_state = match provider_state_header_name {
Some(name) => {
let parts_value = &parts;
let provider_state_header = parts_value.headers.get(name);
match provider_state_header {
Some(header) => Some(Regex::new(header.to_str().unwrap()).unwrap()),
None => provider_state
}
},
None => provider_state
};
let bytes = hyper::body::to_bytes(body).await;
let body = match bytes {
Ok(contents) => if contents.is_empty() {
OptionalBody::Empty
} else {
OptionalBody::Present(contents, None, None)
},
Err(err) => {
warn!("Failed to read request body: {}", err);
OptionalBody::Empty
}
};
let request = pact_support::hyper_request_to_pact_request(parts, body);
let response = handle_request(request, auto_cors, cors_referer,
sources, provider_state, empty_provider_states).await;
pact_support::pact_response_to_hyper_response(&response)
})
}
}
fn method_supports_payload(request: &HttpRequest) -> bool {
matches!(request.method.to_uppercase().as_str(), "POST" | "PUT" | "PATCH")
}
async fn find_matching_request(
request: &HttpRequest,
auto_cors: bool,
cors_referer: bool,
sources: Vec<V4Pact>,
provider_state: Option<Regex>,
empty_provider_states: bool
) -> anyhow::Result<HttpResponse> {
match &provider_state {
Some(state) => info!("Filtering interactions by provider state regex '{}'", state),
None => ()
}
// Get a subset of all interactions across all pacts that match the method and path
let interactions = sources.iter()
.flat_map(|source| {
source.filter_interactions(V4InteractionType::Synchronous_HTTP)
.iter()
.map(|i| (i.as_v4_http().unwrap(), source.clone()))
.collect_vec()
})
.filter(|(http, _)| {
let path_context = CoreMatchingContext::new(DiffConfig::NoUnexpectedKeys,
&http.request.matching_rules.rules_for_category("path").unwrap_or_default(),
&hashmap! {}
);
pact_matching::match_method(&http.request.method, &request.method).is_ok() &&
pact_matching::match_path(&http.request.path, &request.path, &path_context).is_ok()
})
.filter(|(i, _)| {
let ps = &i.provider_states;
match provider_state {
Some(ref regex) => empty_provider_states && ps.is_empty() ||
ps.iter().any(|state|
empty_provider_states && state.name.is_empty() || regex.is_match(state.name.as_str())),
None => true
}
});
// Match all interactions from the sublist against the incoming request
let results = futures::stream::iter(interactions)
.filter_map(|(i, pact)| async move {
let result = pact_matching::match_request(i.request.clone(), request.clone(), &pact.boxed(), &i.boxed()).await;
let mismatches = result.mismatches();
let all_matched = mismatches.iter().all(|mismatch|{
match mismatch {
Mismatch::MethodMismatch { .. } => false,
Mismatch::PathMismatch { .. } => false,
Mismatch::QueryMismatch { .. } => false,
Mismatch::BodyMismatch { .. } => !(method_supports_payload(request) && request.body.is_present()),
_ => true
}
});
if all_matched {
Some((i.clone(), mismatches.clone()))
} else {
None
}
})
.collect::<Vec<_>>()
.await;
// Find the result with the least number of mismatches
let match_results = results.iter()
.sorted_by(|a, b| Ord::cmp(&a.1.len(), &b.1.len()))
.cloned()
.collect::<Vec<(SynchronousHttp, Vec<Mismatch>)>>();
if match_results.len() > 1 {
warn!("Found more than one pact request for method {} and path '{}', using the first one with the least number of mismatches",
request.method, request.path);
}
match match_results.first() {
Some((interaction, _)) => Ok(pact_matching::generate_response(&interaction.response, &GeneratorTestMode::Provider, &hashmap!{}).await),
None => {
if auto_cors && request.method.to_uppercase() == "OPTIONS" {
let origin = if cors_referer {
match request.headers {
Some(ref h) => h.iter()
.find(|kv| kv.0.to_lowercase() == "referer")
.map(|kv| kv.1.clone().join(", ")).unwrap_or_else(|| "*".to_string()),
None => "*".to_string()
}
} else { "*".to_string() };
Ok(HttpResponse {
headers: Some(hashmap!{
"Access-Control-Allow-Headers".to_string() => vec!["*".to_string()],
"Access-Control-Allow-Methods".to_string() => vec!["GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH".to_string()],
"Access-Control-Allow-Origin".to_string() => vec![origin]
}),
.. HttpResponse::default()
})
} else {
Err(anyhow!("No matching request found for path {}", request.path))
}
}
}
}
async fn handle_request(
request: HttpRequest,
auto_cors: bool,
cors_referrer: bool,
sources: Vec<V4Pact>,
provider_state: Option<Regex>,
empty_provider_states: bool
) -> HttpResponse {
info! ("===> Received {}", request);
debug!(" body: '{}'", request.body.display_string());
debug!(" matching_rules: {:?}", request.matching_rules);
debug!(" generators: {:?}", request.generators);
match find_matching_request(&request, auto_cors, cors_referrer, sources, provider_state,
empty_provider_states).await {
Ok(response) => response,
Err(msg) => {
warn!("{}, sending {}", msg, StatusCode::NOT_FOUND);
let mut response = HttpResponse {
status: StatusCode::NOT_FOUND.as_u16(),
.. HttpResponse::default()
};
if auto_cors {
response.headers = Some(hashmap!{ "Access-Control-Allow-Origin".to_string() => vec!["*".to_string()] })
}
response
}
}
}
#[cfg(test)]
mod test {
use expectest::prelude::*;
use maplit::*;
use pact_models::matchingrules;
use pact_models::matchingrules::MatchingRule;
use pact_models::prelude::*;
use pact_models::prelude::v4::*;
use pact_models::v4::http_parts::{HttpRequest, HttpResponse};
use pact_models::v4::interaction::V4Interaction;
use regex::Regex;
#[tokio::test]
async fn match_request_finds_the_most_appropriate_response() {
let interaction1 = SynchronousHttp::default();
let interaction2 = SynchronousHttp::default();
let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest::default();
expect!(super::find_matching_request(&request1, false, false, vec![pact], None, false).await)
.to(be_ok().value(interaction1.response));
}
#[tokio::test]
async fn match_request_excludes_requests_with_different_methods() {
let interaction1 = SynchronousHttp { request: HttpRequest { method: "PUT".to_string(),
.. HttpRequest::default() }, .. SynchronousHttp::default() };
let interaction2 = SynchronousHttp::default();
let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest { method: "POST".to_string(), .. HttpRequest::default() };
expect!(super::find_matching_request(&request1, false, false, vec![pact], None, false).await)
.to(be_err());
}
#[tokio::test]
async fn match_request_excludes_requests_with_different_paths() {
let interaction1 = SynchronousHttp {
request: HttpRequest { path: "/one".to_string(), .. HttpRequest::default() },
.. SynchronousHttp::default()
};
let interaction2 = SynchronousHttp::default();
let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest { path: "/two".to_string(), .. HttpRequest::default() };
expect!(super::find_matching_request(&request1, false, false, vec![pact], None, false).await)
.to(be_err());
}
#[tokio::test]
async fn match_request_excludes_requests_with_different_query_params() {
let interaction1 = SynchronousHttp { request: HttpRequest {
query: Some(hashmap!{ "A".to_string() => vec![ "B".to_string() ] }),
.. HttpRequest::default() }, .. SynchronousHttp::default() };
let interaction2 = SynchronousHttp::default();
let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest {
query: Some(hashmap!{ "A".to_string() => vec![ "C".to_string() ] }),
.. HttpRequest::default() };
expect!(super::find_matching_request(&request1, false, false, vec![pact], None, false).await)
.to(be_err());
}
#[tokio::test]
async fn match_request_excludes_put_or_post_requests_with_different_bodies() {
let interaction1 = SynchronousHttp { request: HttpRequest {
method: "PUT".to_string(),
body: OptionalBody::Present("{\"a\": 1, \"b\": 2, \"c\": 3}".as_bytes().into(), None, None),
.. HttpRequest::default() },
response: HttpResponse { status: 200, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let interaction2 = SynchronousHttp { request: HttpRequest {
method: "PUT".to_string(),
body: OptionalBody::Present("{\"a\": 2, \"b\": 4, \"c\": 6}".as_bytes().into(), None, None),
matching_rules: matchingrules!{
"body" => {
"$.c" => [ MatchingRule::Integer ]
}
},
.. HttpRequest::default() },
response: HttpResponse { status: 201, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest { method: "PUT".to_string(), body: OptionalBody::Present("{\"a\": 1, \"b\": 2, \"c\": 3}".as_bytes().into(), None, None),
.. HttpRequest::default() };
let request2 = HttpRequest { method: "PUT".to_string(), body: OptionalBody::Present("{\"a\": 2, \"b\": 5, \"c\": 3}".as_bytes().into(), None, None),
.. HttpRequest::default() };
let request3 = HttpRequest { method: "PUT".to_string(), body: OptionalBody::Present("{\"a\": 2, \"b\": 4, \"c\": 16}".as_bytes().into(), None, None),
.. HttpRequest::default() };
let request4 = HttpRequest { method: "PUT".to_string(), headers: Some(hashmap!{ "Content-Type".to_string() => vec!["application/json".to_string()] }),
.. HttpRequest::default() };
expect!(super::find_matching_request(&request1, false, false, vec![pact.clone()], None, false).await).to(be_ok());
expect!(super::find_matching_request(&request2, false, false, vec![pact.clone()], None, false).await).to(be_err());
expect!(super::find_matching_request(&request3, false, false, vec![pact.clone()], None, false).await).to(be_ok());
expect!(super::find_matching_request(&request4, false, false, vec![pact], None, false).await).to(be_ok());
}
#[tokio::test]
async fn match_request_returns_the_closest_match() {
let interaction1 = SynchronousHttp { request: HttpRequest {
body: OptionalBody::Present("{\"a\": 1, \"b\": 2, \"c\": 3}".as_bytes().into(), None, None),
.. HttpRequest::default() },
response: HttpResponse { status: 200, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let interaction2 = SynchronousHttp { request: HttpRequest {
body: OptionalBody::Present("{\"a\": 2, \"b\": 4, \"c\": 6}".as_bytes().into(), None, None),
.. HttpRequest::default() },
response: HttpResponse { status: 201, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let pact1 = V4Pact {
interactions: vec![ interaction1.boxed_v4() ],
.. V4Pact::default()
};
let pact2 = V4Pact {
interactions: vec![ interaction2.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest {
body: OptionalBody::Present("{\"a\": 1, \"b\": 4, \"c\": 6}".as_bytes().into(), None, None),
.. HttpRequest::default() };
expect!(super::find_matching_request(&request1, false, false, vec![pact1, pact2], None, false).await)
.to(be_ok().value(interaction2.response));
}
#[tokio::test]
async fn with_auto_cors_return_200_with_an_option_request() {
let interaction1 = SynchronousHttp::default();
let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest {
method: "OPTIONS".to_string(),
.. HttpRequest::default() };
expect!(super::find_matching_request(&request1, true, false, vec![pact.clone()], None, false).await)
.to(be_ok());
expect!(super::find_matching_request(&request1, false, false, vec![pact], None, false).await)
.to(be_err());
}
#[tokio::test]
async fn match_request_with_query_params() {
let matching_rules = matchingrules!{
"query" => {
"page[0]" => [ MatchingRule::Type ]
}
};
let interaction1 = SynchronousHttp {
request: HttpRequest {
path: "/api/objects".to_string(),
query: Some(hashmap!{ "page".to_string() => vec![ "1".to_string() ] }),
.. HttpRequest::default()
},
.. SynchronousHttp::default()
};
let interaction2 = SynchronousHttp {
request: HttpRequest {
path: "/api/objects".to_string(),
query: Some(hashmap!{ "page".to_string() => vec![ "1".to_string() ] }),
matching_rules,
.. HttpRequest::default()
},
.. SynchronousHttp::default()
};
let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest {
path: "/api/objects".to_string(),
query: Some(hashmap!{ "page".to_string() => vec![ "3".to_string() ] }),
.. HttpRequest::default() };
expect!(super::find_matching_request(&request1, false, false, vec![pact], None, false).await)
.to(be_ok());
}
#[test_log::test(tokio::test)]
async fn match_request_with_repeated_query_params() {
let matching_rules = matchingrules!{
"query" => {
"ids" => [ MatchingRule::MinType(2) ],
"ids[*]" => [ MatchingRule::Type ]
}
};
let interaction = SynchronousHttp {
request: HttpRequest {
path: "/api".to_string(),
query: Some(hashmap!{
"ids".to_string() => vec![
"1".to_string(),
"2".to_string(),
"3".to_string(),
"4".to_string()
]
}),
matching_rules,
.. HttpRequest::default()
},
.. SynchronousHttp::default()
};
let pact = V4Pact {
interactions: vec![ interaction.boxed_v4() ],
.. V4Pact::default()
};
let request1 = HttpRequest {
path: "/api".to_string(),
query: Some(hashmap!{ "ids".to_string() => vec![ "3".to_string() ] }),
.. HttpRequest::default() };
let request2 = HttpRequest {
path: "/api".to_string(),
query: Some(hashmap!{ "ids".to_string() => vec![ "3".to_string(), "1".to_string() ] }),
.. HttpRequest::default() };
let request3 = HttpRequest {
path: "/api".to_string(),
query: Some(hashmap!{ "ids".to_string() => vec![
"1".to_string(),
"2".to_string(),
"3".to_string(),
"4".to_string()
] }),
.. HttpRequest::default() };
let request4 = HttpRequest {
path: "/api".to_string(),
query: Some(hashmap!{ "ids".to_string() => vec![
"id".to_string(),
"id".to_string(),
"id".to_string(),
"id".to_string()
] }),
.. HttpRequest::default() };
let request5 = HttpRequest {
path: "/api".to_string(),
query: Some(hashmap!{ "ids".to_string() => vec![
"1".to_string(),
"2".to_string(),
"3".to_string(),
"4".to_string(),
"5".to_string()
] }),
.. HttpRequest::default() };
expect!(super::find_matching_request(&request1, false, false, vec![pact.clone()], None, false).await)
.to(be_err());
expect!(super::find_matching_request(&request2, false, false, vec![pact.clone()], None, false).await)
.to(be_ok());
expect!(super::find_matching_request(&request3, false, false, vec![pact.clone()], None, false).await)
.to(be_ok());
expect!(super::find_matching_request(&request4, false, false, vec![pact.clone()], None, false).await)
.to(be_ok());
expect!(super::find_matching_request(&request5, false, false, vec![pact.clone()], None, false).await)
.to(be_ok());
}
#[tokio::test]
async fn match_request_filters_interactions_if_provider_state_filter_is_provided() {
let response1 = HttpResponse { status: 201, .. HttpResponse::default() };
let interaction1 = SynchronousHttp {
provider_states: vec![ ProviderState::default("state one") ],
request: HttpRequest::default(),
response: HttpResponse { status: 201, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let response2 = HttpResponse { status: 202, .. HttpResponse::default() };
let interaction2 = SynchronousHttp {
provider_states: vec![ ProviderState::default("state two") ],
request: HttpRequest::default(),
response: HttpResponse { status: 202, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let response3 = HttpResponse { status: 203, .. HttpResponse::default() };
let interaction3 = SynchronousHttp {
provider_states: vec![ ProviderState::default("state one"),
ProviderState::default("state two"),
ProviderState::default("state three") ],
request: HttpRequest::default(),
response: HttpResponse { status: 203, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let interaction4 = SynchronousHttp {
response: HttpResponse { status: 204, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let pact = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4(), interaction3.boxed_v4(), interaction4.boxed_v4() ],
.. V4Pact::default()
};
let request = HttpRequest::default();
expect!(super::find_matching_request(&request, false, false, vec![pact.clone()],
Some(Regex::new("state one").unwrap()), false).await).to(be_ok().value(response1.clone()));
expect!(super::find_matching_request(&request, false, false, vec![pact.clone()],
Some(Regex::new("state two").unwrap()), false).await).to(be_ok().value(response2.clone()));
expect!(super::find_matching_request(&request, false, false, vec![pact.clone()],
Some(Regex::new("state three").unwrap()), false).await).to(be_ok().value(response3.clone()));
expect!(super::find_matching_request(&request, false, false, vec![pact.clone()],
Some(Regex::new("state four").unwrap()), false).await).to(be_err());
expect!(super::find_matching_request(&request, false, false, vec![pact.clone()],
Some(Regex::new("state .*").unwrap()), false).await).to(be_ok().value(response1.clone()));
}
#[tokio::test]
async fn match_request_filters_interactions_if_provider_state_filter_is_provided_and_empty_values_included() {
let interaction1 = SynchronousHttp {
provider_states: vec![ ProviderState::default("state one") ],
request: HttpRequest::default(),
response: HttpResponse { status: 201, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let response2 = HttpResponse { status: 202, .. HttpResponse::default() };
let interaction2 = SynchronousHttp {
provider_states: vec![ ProviderState::default("") ],
request: HttpRequest::default(),
response: HttpResponse { status: 202, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let response3 = HttpResponse { status: 203, .. HttpResponse::default() };
let interaction3 = SynchronousHttp {
request: HttpRequest::default(),
response: HttpResponse { status: 203, .. HttpResponse::default() },
.. SynchronousHttp::default() };
let pact1 = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction2.boxed_v4(), interaction3.boxed_v4() ],
.. V4Pact::default()
};
let pact2 = V4Pact {
interactions: vec![ interaction1.boxed_v4(), interaction3.boxed_v4() ],
.. V4Pact::default()
};
let request = HttpRequest::default();
expect!(super::find_matching_request(&request, false, false, vec![pact1],
Some(Regex::new("any state").unwrap()), true).await).to(be_ok().value(response2.clone()));
expect!(super::find_matching_request(&request, false, false, vec![pact2],
Some(Regex::new("any state").unwrap()), true).await).to(be_ok().value(response3.clone()));
}
#[tokio::test]
async fn handles_repeated_headers_values() {
let interaction = SynchronousHttp {
request: HttpRequest { headers: Some(hashmap!{ "TEST-X".to_string() => vec!["X, Z".to_string()] }), .. HttpRequest::default() },
response: HttpResponse { headers: Some(hashmap!{ "TEST-X".to_string() => vec!["X, Y".to_string()] }), .. HttpResponse::default() },
.. SynchronousHttp::default() };
let pact = V4Pact {
interactions: vec![ interaction.boxed_v4() ],
.. V4Pact::default()
};
let request = HttpRequest { headers: Some(hashmap!{ "TEST-X".to_string() => vec!["X, Y".to_string()] }), .. HttpRequest::default() };
let result = super::find_matching_request(&request, false, false, vec![pact], None, false).await;
expect!(result).to(be_ok().value(interaction.response));
}
}