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
//! The `pact_verifier` crate provides the core logic to performing verification of providers.
//! It implements the V2 Pact specification (https://github.com/pact-foundation/pact-specification/tree/version-2).

#![warn(missing_docs)]

#[macro_use] extern crate pact_matching;
extern crate ansi_term;
#[macro_use] extern crate log;
extern crate hyper;
#[macro_use] extern crate maplit;
extern crate rustc_serialize;
extern crate itertools;
extern crate regex;
extern crate difference;

#[cfg(test)]
#[macro_use(expect)]
extern crate expectest;
#[cfg(test)]
#[macro_use]
extern crate pact_consumer;
#[cfg(test)]
extern crate env_logger;

mod provider_client;
mod pact_broker;

use std::path::Path;
use std::error::Error;
use std::io;
use std::fs;
use pact_matching::*;
use pact_matching::models::*;
use ansi_term::*;
use ansi_term::Colour::*;
use std::collections::HashMap;
use provider_client::{make_provider_request, make_state_change_request};
use rustc_serialize::json::Json;
use regex::Regex;

/// Source for loading pacts
#[derive(Debug, Clone)]
pub enum PactSource {
    /// Load the pact from a pact file
    File(String),
    /// Load all the pacts from a Directory
    Dir(String),
    /// Load the pact from a URL
    URL(String),
    /// Load all pacts with the provider name from the pact broker url
    BrokerUrl(String, String)
}

/// Information about the Provider to verify
#[derive(Debug, Clone)]
pub struct ProviderInfo {
    /// Provider Name
    pub name: String,
    /// Provider protocol, defaults to HTTP
    pub protocol: String,
    /// Hostname of the provider
    pub host: String,
    /// Port the provider is running on, defaults to 8080
    pub port: u16,
    /// Base path for the provider, defaults to /
    pub path: String,
    /// URL to post state change requests to
    pub state_change_url: Option<String>,
    /// If teardown state change requests should be made (default is false)
    pub state_change_teardown: bool,
    /// If state change request data should be sent in the body (true) or as query parameters (false)
    pub state_change_body: bool
}

impl ProviderInfo {
    /// Create a default provider info
    pub fn default() -> ProviderInfo {
        ProviderInfo {
            name: s!("provider"),
            protocol: s!("http"),
            host: s!("localhost"),
            port: 8080,
            path: s!("/"),
            state_change_url: None,
            state_change_teardown: false,
            state_change_body: true
        }
    }
}

/// Result of performing a match
#[derive(Debug, Clone)]
pub enum MismatchResult {
    /// Response mismatches
    Mismatches(Vec<Mismatch>, Response, Response),
    /// Error occured
    Error(String)
}

fn verify_response_from_provider(provider: &ProviderInfo, interaction: &Interaction) -> Result<(), MismatchResult> {
    let ref expected_response = interaction.response;
    match make_provider_request(provider, &interaction.request) {
        Ok(ref actual_response) => {
            let mismatches = match_response(expected_response.clone(), actual_response.clone());
            if mismatches.is_empty() {
                Ok(())
            } else {
                Err(MismatchResult::Mismatches(mismatches, expected_response.clone(), actual_response.clone()))
            }
        },
        Err(err) => {
            Err(MismatchResult::Error(s!(err.description())))
        }
    }
}

fn execute_state_change(provider_state: &String, provider: &ProviderInfo, setup: bool) -> Result<(), MismatchResult> {
    if setup {
        println!("  Given {}", Style::new().bold().paint(provider_state.clone()));
    }
    let result = match provider.state_change_url {
        Some(_) => {
            let mut state_change_request = Request { method: s!("POST"), .. Request::default_request() };
            if provider.state_change_body {
              let json_body = Json::Object(btreemap!{
                  s!("state") => Json::String(provider_state.clone()),
                  s!("action") => Json::String(if setup {
                    s!("setup")
                  } else {
                    s!("teardown")
                  })
              });
              state_change_request.body = OptionalBody::Present(json_body.to_string());
            } else {
              let mut query = hashmap!{ s!("state") => vec![provider_state.clone()] };
              if setup {
                query.insert(s!("action"), vec![s!("setup")]);
              } else {
                query.insert(s!("action"), vec![s!("teardown")]);
              }
              state_change_request.query = Some(query);
            }
            match make_state_change_request(provider, &state_change_request) {
                Ok(_) => Ok(()),
                Err(err) => Err(MismatchResult::Error(err))
            }
        },
        None => {
            if setup {
                println!("    {}", Yellow.paint("WARNING: State Change ignored as there is no state change URL"));
            }
            Ok(())
        }
    };

    debug!("State Change: \"{}\" -> {:?}", provider_state, result);
    result
}

fn verify_interaction(provider: &ProviderInfo, interaction: &Interaction) -> Result<(), MismatchResult> {
    match interaction.provider_state {
        Some(ref state) => try!(execute_state_change(state, provider, true)),
        None => ()
    }

    let result = verify_response_from_provider(provider, interaction);

    if provider.state_change_teardown {
        match interaction.provider_state {
            Some(ref state) => try!(execute_state_change(state, provider, false)),
            None => ()
        }
    }

    result
}

fn display_result(status: u16, status_result: ANSIGenericString<str>,
    header_results: Option<Vec<(String, String, ANSIGenericString<str>)>>,
    body_result: ANSIGenericString<str>) {
    println!("    returns a response which");
    println!("      has status code {} ({})", Style::new().bold().paint(format!("{}", status)),
        status_result);
    match header_results {
        Some(header_results) => {
            println!("      includes headers");
            for (key, value, result) in header_results {
                println!("        \"{}\" with value \"{}\" ({})", Style::new().bold().paint(key),
                    Style::new().bold().paint(value), result);
            }
        },
        None => ()
    }
    println!("      has a matching body ({})", body_result);
}

fn walkdir(dir: &Path) -> io::Result<Vec<io::Result<Pact>>> {
    let mut pacts = vec![];
    debug!("Scanning {:?}", dir);
    for entry in try!(fs::read_dir(dir)) {
        let entry = try!(entry);
        let path = entry.path();
        if path.is_dir() {
            try!(walkdir(&path));
        } else {
            pacts.push(Pact::read_pact(&path))
        }
    }
    Ok(pacts)
}

fn display_body_mismatch(expected: &Response, actual: &Response, path: &String) {
    match expected.content_type_enum() {
        DetectedContentType::Json => println!("{}", pact_matching::json::display_diff(&expected.body.value(),
            &actual.body.value(), path)),
        _ => ()
    }
}

/// Filter information used to filter the interactions that are verified
pub enum FilterInfo {
    /// No filter, all interactions will be verified
    None,
    /// Filter on the interaction description
    Description(String),
    /// Filter on the interaction provider state
    State(String),
    /// Filter on both the interaction description and provider state
    DescriptionAndState(String, String)
}

impl FilterInfo {

    /// If this filter is filtering on description
    pub fn has_description(&self) -> bool {
        match self {
            &FilterInfo::Description(_) => true,
            &FilterInfo::DescriptionAndState(_, _) => true,
            _ => false
        }
    }

    /// If this filter is filtering on provider state
    pub fn has_state(&self) -> bool {
        match self {
            &FilterInfo::State(_) => true,
            &FilterInfo::DescriptionAndState(_, _) => true,
            _ => false
        }
    }

    /// Value of the state to filter
    pub fn state(&self) -> String {
        match self {
            &FilterInfo::State(ref s) => s.clone(),
            &FilterInfo::DescriptionAndState(_, ref s) => s.clone(),
            _ => s!("")
        }
    }

    /// Value of the description to filter
    pub fn description(&self) -> String {
        match self {
            &FilterInfo::Description(ref s) => s.clone(),
            &FilterInfo::DescriptionAndState(ref s, _) => s.clone(),
            _ => s!("")
        }
    }

    /// If the filter matches the interaction provider state using a regular expression. If the
    /// filter value is the empty string, then it will match interactions with no provider state.
    ///
    /// # Panics
    /// If the state filter value can't be parsed as a regular expression
    pub fn match_state(&self, interaction: &Interaction) -> bool {
        match interaction.provider_state.clone() {
            Some(state) => {
                if self.state().is_empty() {
                    false
                } else {
                    let re = Regex::new(&self.state()).unwrap();
                    re.is_match(&state)
                }
            },
            None => self.has_state() && self.state().is_empty()
        }
    }

    /// If the filter matches the interaction description using a regular expression
    ///
    /// # Panics
    /// If the description filter value can't be parsed as a regular expression
    pub fn match_description(&self, interaction: &Interaction) -> bool {
        let re = Regex::new(&self.description()).unwrap();
        re.is_match(&interaction.description)
    }

}

fn filter_interaction(interaction: &Interaction, filter: &FilterInfo) -> bool {
    if filter.has_description() && filter.has_state() {
      filter.match_description(interaction) && filter.match_state(interaction)
    } else if filter.has_description() {
      filter.match_description(interaction)
    } else if filter.has_state() {
      filter.match_state(interaction)
    } else {
      true
    }
}

fn filter_consumers(consumers: &Vec<String>, res: &Result<Pact, String>) -> bool {
    consumers.is_empty() || res.is_err() || consumers.contains(&res.clone().unwrap().consumer.name)
}

/// Verify the provider with the given pact sources
pub fn verify_provider(provider_info: &ProviderInfo, source: Vec<PactSource>, filter: &FilterInfo,
    consumers: &Vec<String>) -> bool {
    let pacts = source.iter().flat_map(|s| {
        match s {
            &PactSource::File(ref file) => vec![Pact::read_pact(Path::new(&file))
                .map_err(|err| format!("Failed to load pact '{}' - {}", file, err))],
            &PactSource::Dir(ref dir) => match walkdir(Path::new(dir)) {
                Ok(ref pacts) => pacts.iter().map(|p| {
                        match p {
                            &Ok(ref pact) => Ok(pact.clone()),
                            &Err(ref err) => Err(format!("Failed to load pact from '{}' - {}", dir, err))
                        }
                    }).collect(),
                Err(err) => vec![Err(format!("Could not load pacts from directory '{}' - {}", dir, err))]
            },
            &PactSource::URL(ref url) => vec![Pact::from_url(url)
                .map_err(|err| format!("Failed to load pact '{}' - {}", url, err))],
            &PactSource::BrokerUrl(ref provider_name, ref broker_url) => match pact_broker::fetch_pacts_from_broker(broker_url, provider_name) {
                Ok(ref pacts) => pacts.iter().map(|p| {
                        match p {
                            &Ok(ref pact) => Ok(pact.clone()),
                            &Err(ref err) => Err(format!("Failed to load pact from '{}' - {:?}", broker_url, err))
                        }
                    }).collect(),
                Err(err) => vec![Err(format!("Could not load pacts from the pact broker '{}' - {:?}", broker_url, err))]
            }
        }
    })
    .filter(|res| filter_consumers(consumers, res))
    .collect::<Vec<Result<Pact, String>>>();

    let mut verify_provider_result = true;
    let mut all_errors: Vec<(String, MismatchResult)> = vec![];
    for pact in pacts {
        match pact {
            Ok(ref pact) => {
                println!("\nVerifying a pact between {} and {}",
                    Style::new().bold().paint(pact.consumer.name.clone()),
                    Style::new().bold().paint(pact.provider.name.clone()));

                if pact.interactions.is_empty() {
                    println!("         {}", Yellow.paint("WARNING: Pact file has no interactions"));
                } else {
                    let results: HashMap<Interaction, Result<(), MismatchResult>> = pact.interactions.iter()
                    .filter(|interaction| filter_interaction(interaction, filter))
                    .map(|interaction| {
                        (interaction.clone(), verify_interaction(provider_info, interaction))
                    }).collect();

                    for (interaction, result) in results.clone() {
                        let mut description = format!("Verifying a pact between {} and {}",
                            pact.consumer.name.clone(), pact.provider.name.clone());
                        if interaction.provider_state.is_some() {
                            description.push_str(&format!(" Given {}",
                                interaction.provider_state.clone().unwrap()));
                        }
                        description.push_str(" - ");
                        description.push_str(&interaction.description);
                        println!("  {}", interaction.description);
                        match result {
                            Ok(()) => {
                                display_result(interaction.response.status, Green.paint("OK"),
                                    interaction.response.headers.map(|h| h.iter().map(|(k, v)| {
                                        (k.clone(), v.clone(), Green.paint("OK"))
                                    }).collect()), Green.paint("OK"))
                            },
                            Err(ref err) => match err {
                                &MismatchResult::Error(ref err_des) => {
                                    println!("      {}", Red.paint(format!("Request Failed - {}", err_des)));
                                    all_errors.push((description, MismatchResult::Error(err_des.clone())));
                                    verify_provider_result = false;
                                },
                                &MismatchResult::Mismatches(ref mismatches, ref expected_response, ref actual_response) => {
                                    description.push_str(" returns a response which ");
                                    let status_result = if mismatches.iter().any(|m| m.mismatch_type() == s!("StatusMismatch")) {
                                        verify_provider_result = false;
                                        Red.paint("FAILED")
                                    } else {
                                        Green.paint("OK")
                                    };
                                    let header_results = match interaction.response.headers {
                                        Some(ref h) => Some(h.iter().map(|(k, v)| {
                                            (k.clone(), v.clone(), if mismatches.iter().any(|m| {
                                                match m {
                                                    &Mismatch::HeaderMismatch{ ref key, .. } => k == key,
                                                    _ => false
                                                }
                                            }) {
                                                verify_provider_result = false;
                                                Red.paint("FAILED")
                                            } else {
                                                Green.paint("OK")
                                            })
                                        }).collect()),
                                        None => None
                                    };
                                    let body_result = if mismatches.iter().any(|m| m.mismatch_type() == s!("BodyMismatch") ||
                                        m.mismatch_type() == s!("BodyTypeMismatch")) {
                                        verify_provider_result = false;
                                        Red.paint("FAILED")
                                    } else {
                                        Green.paint("OK")
                                    };

                                    display_result(interaction.response.status, status_result, header_results,
                                        body_result);

                                    for mismatch in mismatches.clone() {
                                        all_errors.push((description.clone(),
                                            MismatchResult::Mismatches(vec![mismatch.clone()],
                                                expected_response.clone(), actual_response.clone())));
                                    }
                                }
                            }
                        }
                    }
                    println!("");
                }
            },
            Err(err) => {
                error!("Failed to load pact - {}", Red.paint(format!("{}", err)));
                verify_provider_result = false;
                all_errors.push((s!("Failed to load pact"), MismatchResult::Error(format!("{}", err))));
            }
        }
    };

    if !all_errors.is_empty() {
        println!("\nFailures:\n");

        for (i, &(ref description, ref mismatch)) in all_errors.iter().enumerate() {
            match mismatch {
                &MismatchResult::Error(ref err) => println!("{}) {} - {}\n", i, description, err),
                &MismatchResult::Mismatches(ref mismatch, ref expected_response, ref actual_response) => {
                    let mismatch = mismatch.first().unwrap();
                    println!("{}) {}{}", i, description, mismatch.summary());
                    println!("    {}\n", mismatch.ansi_description());

                    match mismatch {
                        &Mismatch::BodyMismatch{ref path, ..} => display_body_mismatch(expected_response, actual_response, path),
                        _ => ()
                    }
                }
            }
        }

        println!("\nThere were {} pact failures\n", all_errors.len());
    }

    verify_provider_result
}

#[cfg(test)]
mod tests {
    use expectest::prelude::*;
    use super::{FilterInfo, filter_interaction, filter_consumers};
    use pact_matching::models::*;

    #[test]
    fn if_no_interaction_filter_is_defined_returns_true() {
        let interaction = Interaction::default();
        expect!(filter_interaction(&interaction, &FilterInfo::None)).to(be_true());
    }

    #[test]
    fn if_an_interaction_filter_is_defined_returns_false_if_the_description_does_not_match() {
        let interaction = Interaction { description: s!("bob"), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::Description(s!("fred")))).to(be_false());
    }

    #[test]
    fn if_an_interaction_filter_is_defined_returns_true_if_the_description_does_match() {
        let interaction = Interaction { description: s!("bob"), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::Description(s!("bob")))).to(be_true());
    }

    #[test]
    fn uses_regexs_to_match_the_description() {
        let interaction = Interaction { description: s!("bobby"), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::Description(s!("bob.*")))).to(be_true());
    }

    #[test]
    fn if_an_interaction_state_filter_is_defined_returns_false_if_the_state_does_not_match() {
        let interaction = Interaction { provider_state: Some(s!("bob")), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::State(s!("fred")))).to(be_false());
    }

    #[test]
    fn if_an_interaction_state_filter_is_defined_returns_true_if_the_state_does_match() {
        let interaction = Interaction { provider_state: Some(s!("bob")), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::State(s!("bob")))).to(be_true());
    }

    #[test]
    fn uses_regexs_to_match_the_state() {
        let interaction = Interaction { provider_state: Some(s!("bobby")), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::State(s!("bob.*")))).to(be_true());
    }

    #[test]
    fn if_the_state_filter_is_empty_returns_false_if_the_interaction_state_is_defined() {
        let interaction = Interaction { provider_state: Some(s!("bobby")), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::State(s!("")))).to(be_false());
    }

    #[test]
    fn if_the_state_filter_is_empty_returns_true_if_the_interaction_state_is_not_defined() {
        let interaction = Interaction { provider_state: None, .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::State(s!("")))).to(be_true());
    }

    #[test]
    fn if_the_state_filter_and_interaction_filter_is_defined_must_match_both() {
        let interaction = Interaction { description: s!("freddy"), provider_state: Some(s!("bobby")), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::DescriptionAndState(s!(".*ddy"), s!("bob.*")))).to(be_true());
    }

    #[test]
    fn if_the_state_filter_and_interaction_filter_is_defined_is_false_if_the_provider_state_does_not_match() {
        let interaction = Interaction { description: s!("freddy"), provider_state: Some(s!("boddy")), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::DescriptionAndState(s!(".*ddy"), s!("bob.*")))).to(be_false());
    }

    #[test]
    fn if_the_state_filter_and_interaction_filter_is_defined_is_false_if_the_description_does_not_match() {
        let interaction = Interaction { description: s!("frebby"), provider_state: Some(s!("bobby")), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::DescriptionAndState(s!(".*ddy"), s!("bob.*")))).to(be_false());
    }

    #[test]
    fn if_the_state_filter_and_interaction_filter_is_defined_is_false_if_both_do_not_match() {
        let interaction = Interaction { description: s!("joe"), provider_state: Some(s!("authur")), .. Interaction::default() };
        expect!(filter_interaction(&interaction, &FilterInfo::DescriptionAndState(s!(".*ddy"), s!("bob.*")))).to(be_false());
    }

    #[test]
    fn if_no_consumer_filter_is_defined_returns_true() {
        let consumers = vec![];
        let result = Err(s!(""));
        expect!(filter_consumers(&consumers, &result)).to(be_true());
    }

    #[test]
    fn if_a_consumer_filter_is_defined_returns_false_if_the_consumer_name_does_not_match() {
        let consumers = vec![s!("fred"), s!("joe")];
        let result = Ok(Pact { consumer: Consumer { name: s!("bob") }, .. Pact::default() });
        expect!(filter_consumers(&consumers, &result)).to(be_false());
    }

    #[test]
    fn if_a_consumer_filter_is_defined_returns_true_if_the_result_is_an_error() {
        let consumers = vec![s!("fred"), s!("joe")];
        let result = Err(s!(""));
        expect!(filter_consumers(&consumers, &result)).to(be_true());
    }

    #[test]
    fn if_a_consumer_filter_is_defined_returns_true_if_the_consumer_name_does_match() {
        let consumers = vec![s!("fred"), s!("joe"), s!("bob")];
        let result = Ok(Pact { consumer: Consumer { name: s!("bob") }, .. Pact::default() });
        expect!(filter_consumers(&consumers, &result)).to(be_true());
    }
}