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
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
//! The `models` module provides all the structures required to model a Pact.

use std::collections::HashMap;
use std::collections::BTreeMap;
use serde_json;
use serde_json::Value;
use hex::FromHex;
use super::strip_whitespace;
use regex::Regex;
use semver::Version;
use itertools::Itertools;
use std::io::{self, Error, ErrorKind};
use std::io::prelude::*;
use std::fs;
use std::fs::File;
use std::path::Path;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use hyper::client::Client;
use std::str;
use base64::{encode, decode};

/// Version of the library
pub const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");

/// Enum defining the pact specification versions supported by the library
#[derive(Debug, Clone, PartialEq)]
#[allow(non_camel_case_types)]
pub enum PactSpecification {
    /// Unknown or unsupported specification version
    Unknown,
    /// First version of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-1)
    V1,
    /// Second version of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-1.1)
    V1_1,
    /// Version two of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-2)
    V2,
    /// Version three of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-3)
    V3,
    /// Version four of the pact specification (https://github.com/pact-foundation/pact-specification/tree/version-4)
    V4
}

impl PactSpecification {
    /// Returns the semantic version string of the specification version.
    pub fn version_str(&self) -> String {
        match *self {
            PactSpecification::V1 => s!("1.0.0"),
            PactSpecification::V1_1 => s!("1.1.0"),
            PactSpecification::V2 => s!("2.0.0"),
            PactSpecification::V3 => s!("3.0.0"),
            PactSpecification::V4 => s!("4.0.0"),
            _ => s!("unknown")
        }
    }

    /// Returns a descriptive string of the specification version.
    pub fn to_string(&self) -> String {
        match *self {
            PactSpecification::V1 => s!("V1"),
            PactSpecification::V1_1 => s!("V1.1"),
            PactSpecification::V2 => s!("V2"),
            PactSpecification::V3 => s!("V3"),
            PactSpecification::V4 => s!("V4"),
            _ => s!("unknown")
        }
    }
}

/// Struct that defines the consumer of the pact.
#[derive(Debug, Clone)]
pub struct Consumer {
    /// Each consumer should have a unique name to identify it.
    pub name: String
}

impl Consumer {
    /// Builds a `Consumer` from the `Json` struct.
    pub fn from_json(pact_json: &Value) -> Consumer {
        let val = match pact_json.get("name") {
            Some(v) => match v.clone() {
                Value::String(s) => s,
                _ => v.to_string()
            },
            None => "consumer".to_string()
        };
        Consumer { name: val.clone() }
    }

    /// Converts this `Consumer` to a `Value` struct.
    pub fn to_json(&self) -> Value {
        json!({ s!("name") : json!(self.name.clone()) })
    }
}

/// Struct that defines a provider of a pact.
#[derive(Debug, Clone)]
pub struct Provider {
    /// Each provider should have a unique name to identify it.
    pub name: String
}

impl Provider {
    /// Builds a `Provider` from a `Value` struct.
    pub fn from_json(pact_json: &Value) -> Provider {
        let val = match pact_json.get("name") {
            Some(v) => match v.clone() {
                Value::String(s) => s,
                _ => v.to_string()
            },
            None => "provider".to_string()
        };
        Provider { name: val.clone() }
    }

    /// Converts this `Provider` to a `Value` struct.
    pub fn to_json(&self) -> Value {
        json!({ s!("name") : json!(self.name.clone()) })
    }
}

/// Enum that defines the four main states that a body of a request and response can be in a pact
/// file.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub enum OptionalBody {
    /// A body is missing if it is not present in the pact file
    Missing,
    /// An empty body that is present in the pact file.
    Empty,
    /// A JSON body that is the null value. This state is to protect other language implementations
    /// from null values. It is treated as `Empty`.
    Null,
    /// A non-empty body that is present in the pact file.
    Present(Vec<u8>)
}

impl OptionalBody {

    /// If the body is present in the pact file and not empty or null.
    pub fn is_present(&self) -> bool {
        match *self {
            OptionalBody::Present(_) => true,
            _ => false
        }
    }

    /// Returns the body if present, otherwise returns the empty Vec.
    pub fn value(&self) -> Vec<u8> {
        match *self {
            OptionalBody::Present(ref s) => s.clone(),
            _ => vec![]
        }
    }

  /// Returns the body if present as a string, otherwise returns the empty string.
  pub fn str_value(&self) -> &str {
    match *self {
      OptionalBody::Present(ref s) => str::from_utf8(s).unwrap_or(""),
      _ => ""
    }
  }
}

impl From<String> for OptionalBody {
  fn from(s: String) -> Self {
    if s.is_empty() {
      OptionalBody::Empty
    } else {
      OptionalBody::Present(Vec::from(s.as_bytes()))
    }
  }
}

impl <'a> From<&'a str> for OptionalBody {
  fn from(s: &'a str) -> Self {
    if s.is_empty() {
      OptionalBody::Empty
    } else {
      OptionalBody::Present(Vec::from(s.as_bytes()))
    }
  }
}

lazy_static! {
    static ref XMLREGEXP: Regex = Regex::new(r"^\s*<\?xml\s*version.*").unwrap();
    static ref HTMLREGEXP: Regex = Regex::new(r"^\s*(<!DOCTYPE)|(<HTML>).*").unwrap();
    static ref JSONREGEXP: Regex = Regex::new(r#"^\s*(true|false|null|[0-9]+|"\w*|\{\s*(}|"\w+)|\[\s*)"#).unwrap();
    static ref XMLREGEXP2: Regex = Regex::new(r#"^\s*<\w+\s*(:\w+=["”][^"”]+["”])?.*"#).unwrap();

    static ref JSON_CONTENT_TYPE: Regex = Regex::new("application/.*json.*").unwrap();
    static ref XML_CONTENT_TYPE: Regex = Regex::new("application/.*xml").unwrap();
}

/// Enumeration of general content types
#[derive(PartialEq, Debug, Clone, Eq)]
pub enum DetectedContentType {
    /// Json content types
    Json,
    /// XML content types
    Xml,
    /// All other content types
    Text
}

/// Enumeration of the types of differences between requests and responses
#[derive(PartialEq, Debug, Clone, Eq)]
pub enum DifferenceType {
  /// Methods differ
  Method,
  /// Paths differ
  Path,
  /// Headers differ
  Headers,
  /// Query parameters differ
  QueryParameters,
  /// Bodies differ
  Body,
  /// Matching Rules differ
  MatchingRules,
  /// Response status differ
  Status
}

pub mod json_utils;
pub mod xml_utils;
#[macro_use] pub mod matchingrules;
#[macro_use] pub mod generators;

/// Trait to specify an HTTP part of a message. It encapsulates the shared parts of a request and
/// response.
pub trait HttpPart {
    /// Returns the headers of the HTTP part.
    fn headers(&self) -> &Option<HashMap<String, String>>;
    /// Returns the body of the HTTP part.
    fn body(&self) -> &OptionalBody;
    /// Returns the matching rules of the HTTP part.
    fn matching_rules(&self) -> &matchingrules::MatchingRules;
    /// Returns the generators of the HTTP part.
    fn generators(&self) -> &generators::Generators;

    /// Determine the content type of the HTTP part. If a `Content-Type` header is present, the
    /// value of that header will be returned. Otherwise, the body will be inspected.
    fn content_type(&self) -> String {
        match *self.headers() {
            Some(ref h) => match h.iter().find(|kv| kv.0.to_lowercase() == s!("content-type")) {
                Some(kv) => match strip_whitespace::<Vec<&str>>(kv.1, ";").first() {
                    Some(v) => s!(*v),
                    None => self.detect_content_type()
                },
                None => self.detect_content_type()
            },
            None => self.detect_content_type()
        }
    }

    /// Tries to detect the content type of the body by matching some regular expressions against
    /// the first 32 characters. Default to `text/plain` if no match is found.
    fn detect_content_type(&self) -> String {
        match *self.body() {
            OptionalBody::Present(ref body) => {
                let s: String = match str::from_utf8(body) {
                  Ok(s) => s.to_string(),
                  Err(_) => String::new()
                };
                debug!("Detecting content type from contents: '{}'", s);
                if XMLREGEXP.is_match(s.as_str()) {
                    s!("application/xml")
                } else if HTMLREGEXP.is_match(s.to_uppercase().as_str()) {
                    s!("text/html")
                } else if XMLREGEXP2.is_match(s.as_str()) {
                    s!("application/xml")
                } else if JSONREGEXP.is_match(s.as_str()) {
                    s!("application/json")
                } else {
                    s!("text/plain")
                }
            },
            _ => s!("text/plain")
        }
    }

    /// Returns the general content type (ignoring subtypes)
    fn content_type_enum(&self) -> DetectedContentType {
        let content_type = self.content_type();
        if JSON_CONTENT_TYPE.is_match(&content_type[..]) {
            DetectedContentType::Json
        } else if XML_CONTENT_TYPE.is_match(&content_type[..]) {
            DetectedContentType::Xml
        } else {
            DetectedContentType::Text
        }
    }
}

/// Struct that defines the request.
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Eq)]
pub struct Request {
    /// Request method
    pub method: String,
    /// Request path
    pub path: String,
    /// Request query string
    pub query: Option<HashMap<String, Vec<String>>>,
    /// Request headers
    pub headers: Option<HashMap<String, String>>,
    /// Request body
    pub body: OptionalBody,
    /// Request matching rules
    pub matching_rules: matchingrules::MatchingRules,
    /// Request generators
    pub generators: generators::Generators
}

impl HttpPart for Request {
    fn headers(&self) -> &Option<HashMap<String, String>> {
        &self.headers
    }

    fn body(&self) -> &OptionalBody {
        &self.body
    }

    fn matching_rules(&self) -> &matchingrules::MatchingRules {
        &self.matching_rules
    }

    fn generators(&self) -> &generators::Generators {
      &self.generators
    }
}

impl Hash for Request {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.method.hash(state);
        self.path.hash(state);
        if self.query.is_some() {
            for (k, v) in self.query.clone().unwrap() {
                k.hash(state);
                v.hash(state);
            }
        }
        if self.headers.is_some() {
            for (k, v) in self.headers.clone().unwrap() {
                k.hash(state);
                v.hash(state);
            }
        }
        self.body.hash(state);
        self.matching_rules.hash(state);
        self.generators.hash(state);
    }
}

fn headers_from_json(request: &Value) -> Option<HashMap<String, String>> {
    match request.get("headers") {
        Some(v) => match *v {
            Value::Object(ref m) => Some(m.iter().map(|(key, val)| {
                match val {
                    &Value::String(ref s) => (key.clone(), s.clone()),
                    _ => (key.clone(), val.to_string())
                }
            }).collect()),
            _ => None
        },
        None => None
    }
}

fn headers_to_json(headers: &HashMap<String, String>) -> Value {
    json!(headers.iter().fold(BTreeMap::new(), |mut map, kv| {
        map.insert(kv.0.clone(), Value::String(kv.1.clone()));
        map
    }))
}

#[derive(Deserialize)]
#[serde(untagged)]
enum JsonParsable {
    JsonStringValue(String),
    KeyValue(HashMap<String, Value>)
}

fn body_from_json(request: &Value, fieldname: &str, headers: &Option<HashMap<String, String>>) -> OptionalBody {
    let content_type = match headers {
        &Some(ref h) => match h.iter().find(|kv| kv.0.to_lowercase() == s!("content-type")) {
            Some(kv) => {
                match strip_whitespace::<Vec<&str>>(&kv.1, ";").first() {
                    Some(v) => Some(v.to_lowercase()),
                    None => None
                }
            },
            None => None
        },
        &None => None
    };

    match request.get(fieldname) {
        Some(v) => match *v {
            Value::String(ref s) => {
                if s.is_empty() {
                  OptionalBody::Empty
                } else {
                  let content_type = content_type.unwrap_or(s!("text/plain"));
                  if JSON_CONTENT_TYPE.is_match(&content_type) {
                    match serde_json::from_str::<JsonParsable>(&s) {
                      Ok(_) => OptionalBody::Present(s.clone().into()),
                      Err(_) => OptionalBody::Present(format!("\"{}\"", s).into())
                    }
                  } else if content_type.starts_with("text/") {
                    OptionalBody::Present(s.clone().into())
                  } else {
                    match decode(s) {
                      Ok(bytes) => OptionalBody::Present(bytes.clone()),
                      Err(_) => OptionalBody::Present(s.clone().into())
                    }
                  }
                }
            },
            Value::Null => OptionalBody::Null,
            _ => OptionalBody::Present(v.to_string().into())
        },
        None => OptionalBody::Missing
    }
}

/// Converts a query string map into a query string
pub fn build_query_string(query: HashMap<String, Vec<String>>) -> String {
    query.into_iter()
        .sorted_by(|a, b| Ord::cmp(&a.0, &b.0))
        .iter()
        .flat_map(|kv| {
            kv.1.iter()
                .map(|v| format!("{}={}", kv.0, encode_query(v)))
                .collect_vec()
        })
        .join("&")
}

fn query_from_json(query_json: &Value, spec_version: &PactSpecification) -> Option<HashMap<String, Vec<String>>> {
    match query_json {
        &Value::String(ref s) => parse_query_string(s),
        _ => {
            warn!("Only string versions of request query strings are supported with specification version {}, ignoring.",
                spec_version.to_string());
            None
        }
    }
}

fn v3_query_from_json(query_json: &Value, spec_version: &PactSpecification) -> Option<HashMap<String, Vec<String>>> {
    match query_json {
        &Value::String(ref s) => parse_query_string(s),
        &Value::Object(ref map) => Some(map.iter().map(|(k, v)| {
            (k.clone(), match v {
                &Value::String(ref s) => vec![s.clone()],
                &Value::Array(ref array) => array.iter().map(|item| match item {
                    &Value::String(ref s) => s.clone(),
                    _ => v.to_string()
                }).collect(),
                _ => {
                    warn!("Query paramter value '{}' is not valid, ignoring", v);
                    vec![]
                }
            })
        }).collect()),
        _ => {
            warn!("Only string or map versions of request query strings are supported with specification version {}, ignoring.",
                spec_version.to_string());
            None
        }
    }
}

fn query_to_json(query: HashMap<String, Vec<String>>, spec_version: &PactSpecification) -> Value {
    match spec_version {
        &PactSpecification::V3 => Value::Object(query.iter().map(|(k, v)| {
            (k.clone(), Value::Array(v.iter().map(|q| Value::String(q.clone())).collect()))}
        ).collect()),
        _ => Value::String(build_query_string(query))
    }
}

impl Request {
    /// Builds a `Request` from a `Value` struct.
    pub fn from_json(request_json: &Value, spec_version: &PactSpecification) -> Request {
        let method_val = match request_json.get("method") {
            Some(v) => match *v {
                Value::String(ref s) => s.to_uppercase(),
                _ => v.to_string().to_uppercase()
            },
            None => "GET".to_string()
        };
        let path_val = match request_json.get("path") {
            Some(v) => match *v {
                Value::String(ref s) => s.clone(),
                _ => v.to_string()
            },
            None => "/".to_string()
        };
        let query_val = match request_json.get("query") {
            Some(v) => match spec_version {
                &PactSpecification::V3 => v3_query_from_json(v, spec_version),
                _ => query_from_json(v, spec_version)
            },
            None => None
        };
        let headers = headers_from_json(request_json);
        Request {
            method: method_val,
            path: path_val,
            query: query_val,
            headers: headers.clone(),
            body: body_from_json(request_json, "body", &headers),
            matching_rules: matchingrules::matchers_from_json(request_json, &Some(s!("requestMatchingRules"))),
            generators: generators::generators_from_json(request_json)
        }
    }

    /// Converts this `Request` to a `Value` struct.
    pub fn to_json(&self, spec_version: &PactSpecification) -> Value {
        let mut json = json!({
            s!("method") : Value::String(self.method.to_uppercase()),
            s!("path") : Value::String(self.path.clone())
        });
        {
            let map = json.as_object_mut().unwrap();
            if self.query.is_some() {
                map.insert(s!("query"), query_to_json(self.query.clone().unwrap(), spec_version));
            }
            if self.headers.is_some() {
                map.insert(s!("headers"), headers_to_json(&self.headers.clone().unwrap()));
            }
            match self.body {
                OptionalBody::Present(ref body) => {
                    if self.content_type() == "application/json" {
                        match serde_json::from_slice(body) {
                            Ok(json_body) => { map.insert(s!("body"), json_body); },
                            Err(err) => {
                                warn!("Failed to parse json body: {}", err);
                                map.insert(s!("body"), Value::String(encode(body)));
                            }
                        }
                    } else {
                        match str::from_utf8(body) {
                        Ok(s) =>map.insert(s!("body"), Value::String(s.to_string())),
                        Err(_) => map.insert(s!("body"), Value::String(encode(body)))
                      };
                    }
                },
                OptionalBody::Empty => { map.insert(s!("body"), Value::String(s!(""))); },
                OptionalBody::Missing => (),
                OptionalBody::Null => { map.insert(s!("body"), Value::Null); }
            }
            if self.matching_rules.is_not_empty() {
                map.insert(s!("matchingRules"), matchingrules::matchers_to_json(
                &self.matching_rules.clone(), spec_version));
            }
            if self.generators.is_not_empty() {
              map.insert(s!("generators"), generators::generators_to_json(
                &self.generators.clone(), spec_version));
            }
        }
        json
    }

    /// Returns the default request: a GET request to the root.
    pub fn default_request() -> Request {
        Request {
            method: s!("GET"),
            path: s!("/"),
            query: None,
            headers: None,
            body: OptionalBody::Missing,
            matching_rules: matchingrules::MatchingRules::default(),
            generators: generators::Generators::default()
        }
    }

    /// Return a description of all the differences from the other request
    pub fn differences_from(&self, other: &Request) -> Vec<(DifferenceType, String)> {
        let mut differences = vec![];
        if self.method != other.method {
            differences.push((DifferenceType::Method, format!("Request method {} != {}", self.method, other.method)));
        }
        if self.path != other.path {
            differences.push((DifferenceType::Path, format!("Request path {} != {}", self.path, other.path)));
        }
        if self.query != other.query {
            differences.push((DifferenceType::QueryParameters, format!("Request query {:?} != {:?}", self.query, other.query)));
        }
        if self.headers != other.headers {
            differences.push((DifferenceType::Headers, format!("Request headers {:?} != {:?}", self.headers, other.headers)));
        }
        if self.body != other.body {
            differences.push((DifferenceType::Body, format!("Request body '{:?}' != '{:?}'", self.body, other.body)));
        }
        if self.matching_rules != other.matching_rules {
            differences.push((DifferenceType::MatchingRules, format!("Request matching rules {:?} != {:?}", self.matching_rules, other.matching_rules)));
        }
        differences
    }
}

/// Struct that defines the response.
#[derive(PartialEq, Debug, Clone, Eq)]
pub struct Response {
    /// Response status
    pub status: u16,
    /// Response headers
    pub headers: Option<HashMap<String, String>>,
    /// Response body
    pub body: OptionalBody,
    /// Response matching rules
    pub matching_rules: matchingrules::MatchingRules,
    /// Response generators
    pub generators: generators::Generators
}

impl Response {

    /// Build a `Response` from a `Value` struct.
    pub fn from_json(response: &Value, _: &PactSpecification) -> Response {
        let status_val = match response.get("status") {
            Some(v) => v.as_u64().unwrap() as u16,
            None => 200
        };
        let headers = headers_from_json(response);
        Response {
            status: status_val,
            headers: headers.clone(),
            body: body_from_json(response, "body", &headers),
            matching_rules:  matchingrules::matchers_from_json(response, &Some(s!("responseMatchingRules"))),
            generators:  generators::generators_from_json(response)
        }
    }

    /// Returns a default response: Status 200
    pub fn default_response() -> Response {
        Response {
            status: 200,
            headers: None,
            body: OptionalBody::Missing,
            matching_rules: matchingrules::MatchingRules::default(),
            generators: generators::Generators::default()
        }
    }

    /// Converts this response to a `Value` struct.
    #[allow(unused_variables)]
    pub fn to_json(&self, spec_version: &PactSpecification) -> Value {
        let mut json = json!({
            s!("status") : json!(self.status)
        });
        {
            let map = json.as_object_mut().unwrap();
            if self.headers.is_some() {
                map.insert(s!("headers"), headers_to_json(&self.headers.clone().unwrap()));
            }
            match self.body {
                OptionalBody::Present(ref body) => {
                    if self.content_type() == "application/json" {
                        match serde_json::from_slice(body) {
                            Ok(json_body) => { map.insert(s!("body"), json_body); },
                            Err(err) => {
                                warn!("Failed to parse json body: {}", err);
                                map.insert(s!("body"), Value::String(encode(body)));
                            }
                        }
                    } else {
                      match str::from_utf8(body) {
                        Ok(s) => map.insert(s!("body"), Value::String(s.to_string())),
                        Err(_) => map.insert(s!("body"), Value::String(encode(body)))
                      };
                    }
                },
                OptionalBody::Empty => { map.insert(s!("body"), Value::String(s!(""))); },
                OptionalBody::Missing => (),
                OptionalBody::Null => { map.insert(s!("body"), Value::Null); }
            }
            if self.matching_rules.is_not_empty() {
                map.insert(s!("matchingRules"), matchingrules::matchers_to_json(
              &self.matching_rules.clone(), spec_version));
            }
            if self.generators.is_not_empty() {
              map.insert(s!("generators"), generators::generators_to_json(
                &self.generators.clone(), spec_version));
            }
        }
        json
    }

    /// Return a description of all the differences from the other response
    pub fn differences_from(&self, other: &Response) -> Vec<(DifferenceType, String)> {
        let mut differences = vec![];
        if self.status != other.status {
            differences.push((DifferenceType::Status, format!("Response status {} != {}", self.status, other.status)));
        }
        if self.headers != other.headers {
            differences.push((DifferenceType::Headers, format!("Response headers {:?} != {:?}", self.headers, other.headers)));
        }
        if self.body != other.body {
            differences.push((DifferenceType::Body, format!("Response body '{:?}' != '{:?}'", self.body, other.body)));
        }
        if self.matching_rules != other.matching_rules {
            differences.push((DifferenceType::MatchingRules, format!("Response matching rules {:?} != {:?}", self.matching_rules, other.matching_rules)));
        }
        differences
    }
}

impl HttpPart for Response {
    fn headers(&self) -> &Option<HashMap<String, String>> {
        &self.headers
    }

    fn body(&self) -> &OptionalBody {
        &self.body
    }

    fn matching_rules(&self) -> &matchingrules::MatchingRules {
        &self.matching_rules
    }

    fn generators(&self) -> &generators::Generators {
      &self.generators
    }
}

impl Hash for Response {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.status.hash(state);
        if self.headers.is_some() {
            for (k, v) in self.headers.clone().unwrap() {
                k.hash(state);
                v.hash(state);
            }
        }
        self.body.hash(state);
        self.matching_rules.hash(state);
        self.generators.hash(state);
    }
}

pub mod provider_states;

/// Struct that defined an interaction conflict
#[derive(Debug, Clone)]
pub struct PactConflict {
    /// Description of the interactions
    pub interaction: String,
    /// Conflict description
    pub description: String
}

/// Struct that defines an interaction (request and response pair)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Interaction {
    /// Description of this interaction. This needs to be unique in the pact file.
    pub description: String,
    /// Optional provider states for the interaction.
    /// See http://docs.pact.io/documentation/provider_states.html for more info on provider states.
    pub provider_states: Vec<provider_states::ProviderState>,
    /// Request of the interaction
    pub request: Request,
    /// Response of the interaction
    pub response: Response
}

impl Interaction {
    /// Constructs an `Interaction` from the `Value` struct.
    pub fn from_json(index: usize, pact_json: &Value, spec_version: &PactSpecification) -> Interaction {
        let description = match pact_json.get("description") {
            Some(v) => match *v {
                Value::String(ref s) => s.clone(),
                _ => v.to_string()
            },
            None => format!("Interaction {}", index)
        };
        let provider_states = provider_states::ProviderState::from_json(pact_json);
        let request = match pact_json.get("request") {
            Some(v) => Request::from_json(v, spec_version),
            None => Request::default_request()
        };
        let response = match pact_json.get("response") {
            Some(v) => Response::from_json(v, spec_version),
            None => Response::default_response()
        };
        Interaction {
             description,
             provider_states,
             request,
             response
        }
    }

    /// Converts this interaction to a `Value` struct.
    pub fn to_json(&self, spec_version: &PactSpecification) -> Value {
        let mut value = json!({
            s!("description"): Value::String(self.description.clone()),
            s!("request"): self.request.to_json(spec_version),
            s!("response"): self.response.to_json(spec_version)
        });
        if !self.provider_states.is_empty() {
            let map = value.as_object_mut().unwrap();
            match spec_version {
                &PactSpecification::V3 => map.insert(s!("providerStates"),
                                                     Value::Array(self.provider_states.iter().map(|p| p.to_json()).collect())),
                _ => map.insert(s!("providerState"), Value::String(
                    self.provider_states.first().unwrap().name.clone()))
            };
        }
        value
    }

    /// Returns list of conflicts if this interaction conflicts with the other interaction.
    ///
    /// Two interactions conflict if they have the same description and provider state, but they request and
    /// responses are not equal
    pub fn conflicts_with(&self, other: &Interaction) -> Vec<PactConflict> {
        if self.description == other.description && self.provider_states == other.provider_states {
            let mut conflicts = self.request.differences_from(&other.request).iter()
                .filter(|difference| match difference.0 {
                  DifferenceType::MatchingRules | DifferenceType::Body => false,
                  _ => true
                })
                .map(|difference| PactConflict { interaction: self.description.clone(), description: difference.1.clone() } )
                .collect::<Vec<PactConflict>>();
            for difference in self.response.differences_from(&other.response) {
              match difference.0 {
                DifferenceType::MatchingRules | DifferenceType::Body => (),
                _ => conflicts.push(PactConflict { interaction: self.description.clone(), description: difference.1.clone() })
              };
            }
            conflicts
        } else {
            vec![]
        }
    }

    /// Creates a default interaction
    pub fn default() -> Interaction {
        Interaction {
             description: s!("Default Interaction"),
             provider_states: vec![],
             request: Request::default_request(),
             response: Response::default_response()
        }
    }
}

pub mod message;

/// Struct that represents a pact between the consumer and provider of a service.
#[derive(Debug, Clone)]
pub struct Pact {
    /// Consumer side of the pact
    pub consumer: Consumer,
    /// Provider side of the pact
    pub provider: Provider,
    /// List of interactions between the consumer and provider.
    pub interactions: Vec<Interaction>,
    /// Metadata associated with this pact file.
    pub metadata: BTreeMap<String, BTreeMap<String, String>>,
    /// Specification version of this pact
    pub specification_version: PactSpecification
}

fn parse_meta_data(pact_json: &Value) -> BTreeMap<String, BTreeMap<String, String>> {
    match pact_json.get("metadata") {
        Some(v) => match *v {
            Value::Object(ref obj) => obj.iter().map(|(k, v)| {
                let val = match *v {
                    Value::Object(ref obj) => obj.iter().map(|(k, v)| {
                        match *v {
                            Value::String(ref s) => (k.clone(), s.clone()),
                            _ => (k.clone(), v.to_string())
                        }
                    }).collect(),
                    _ => btreemap!{}
                };
                let key = match k.as_str() {
                  "pact-specification" => s!("pactSpecification"),
                  "pact-rust" => s!("pactRust"),
                  _ => k.clone()
                };
                (key, val)
            }).collect(),
            _ => btreemap!{}
        },
        None => btreemap!{}
    }
}

fn parse_interactions(pact_json: &Value, spec_version: PactSpecification) -> Vec<Interaction> {
    match pact_json.get("interactions") {
        Some(v) => match *v {
            Value::Array(ref array) => array.iter().enumerate().map(|(index, ijson)| {
                Interaction::from_json(index, ijson, &spec_version)
            }).collect(),
            _ => vec![]
        },
        None => vec![]
    }
}

fn determine_spec_version(file: &String, metadata: &BTreeMap<String, BTreeMap<String, String>>) -> PactSpecification {
    let specification = if metadata.get("pact-specification").is_none()
        { metadata.get("pactSpecification") } else { metadata.get("pact-specification") };
    match specification {
        Some(spec) => {
            match spec.get("version") {
                Some(ver) => match Version::parse(ver) {
                    Ok(ver) => match ver.major {
                        1 => match ver.minor {
                            0 => PactSpecification::V1,
                            1 => PactSpecification::V1_1,
                            _ => {
                                warn!("Unsupported specification version '{}' found in the metadata in the pact file {:?}, will try load it as a V3 specification", ver, file);
                                PactSpecification::Unknown
                            }
                        },
                        2 => PactSpecification::V2,
                        3 => PactSpecification::V3,
                        _ => {
                            warn!("Unsupported specification version '{}' found in the metadata in the pact file {:?}, will try load it as a V3 specification", ver, file);
                            PactSpecification::Unknown
                        }
                    },
                    Err(err) => {
                        warn!("Could not parse specification version '{}' found in the metadata in the pact file {:?}, assuming V3 specification - {}", ver, file, err);
                        PactSpecification::Unknown
                    }
                },
                None => {
                    warn!("No specification version found in the metadata in the pact file {:?}, assuming V3 specification", file);
                    PactSpecification::V3
                }
            }
        },
        None => {
            warn!("No metadata found in pact file {:?}, assuming V3 specification", file);
            PactSpecification::V3
        }
    }
}

impl Pact {

    /// Returns the specification version of this pact
    pub fn spec_version(&self) -> PactSpecification {
        determine_spec_version(&s!("<Pact>"), &self.metadata)
    }

    /// Creates a `Pact` from a `Value` struct.
    pub fn from_json(file: &String, pact_json: &Value) -> Pact {
        let metadata = parse_meta_data(pact_json);
        let spec_version = determine_spec_version(file, &metadata);

        let consumer = match pact_json.get("consumer") {
            Some(v) => Consumer::from_json(v),
            None => Consumer { name: s!("consumer") }
        };
        let provider = match pact_json.get("provider") {
            Some(v) => Provider::from_json(v),
            None => Provider { name: s!("provider") }
        };
        Pact {
            consumer,
            provider,
            interactions: parse_interactions(pact_json, spec_version.clone()),
            metadata,
            specification_version: spec_version.clone()
        }
    }

    /// Converts this pact to a `Value` struct.
    pub fn to_json(&self, pact_spec: PactSpecification) -> Value {
        json!({
            s!("consumer"): self.consumer.to_json(),
            s!("provider"): self.provider.to_json(),
            s!("interactions"): Value::Array(self.interactions.iter().map(|i| i.to_json(&pact_spec)).collect()),
            s!("metadata"): json!(self.metadata_to_json(&pact_spec))
        })
    }

    /// Creates a BTreeMap of the metadata of this pact.
    pub fn metadata_to_json(&self, pact_spec: &PactSpecification) -> BTreeMap<String, Value> {
        let mut md_map: BTreeMap<String, Value> = self.metadata.iter()
            .map(|(k, v)| {
                let key = match k.as_str() {
                  "pact-specification" => s!("pactSpecification"),
                  "pact-rust" => s!("pactRust"),
                  _ => k.clone()
                };
                (key, json!(v.iter()
                  .map(|(k, v)| (k.clone(), v.clone()))
                  .collect::<BTreeMap<String, String>>()))
            })
            .collect();

        md_map.insert(s!("pactSpecification"), json!({"version" : pact_spec.version_str()}));
        md_map.insert(s!("pactRust"), json!({"version" : s!(VERSION.unwrap_or("unknown"))}));
        md_map
    }

    /// Merges this pact with the other pact, and returns a new Pact with the interactions sorted.
    /// Returns an error if there is a merge conflict, which will occur if any interaction has the
    /// same description and provider state and the requests and responses are different.
    pub fn merge(&self, pact: &Pact) -> Result<Pact, String> {
        if self.consumer.name == pact.consumer.name && self.provider.name == pact.provider.name {
            let conflicts = iproduct!(self.interactions.clone(), pact.interactions.clone())
                .map(|i| i.0.conflicts_with(&i.1))
                .filter(|conflicts| !conflicts.is_empty())
                .collect::<Vec<Vec<PactConflict>>>();
            let num_conflicts = conflicts.len();
            if num_conflicts > 0 {
                warn!("The following conflicting interactions where found:");
                for interaction_conflicts in conflicts {
                    warn!(" Interaction '{}':", interaction_conflicts.first().unwrap().interaction);
                    for conflict in interaction_conflicts {
                        warn!("   {}", conflict.description);
                    }
                }
                Err(format!("Unable to merge pacts, as there were {} conflict(s) between the interactions",
                    num_conflicts))
            } else {
                Ok(Pact {
                    provider: self.provider.clone(),
                    consumer: self.consumer.clone(),
                    interactions: self.interactions.iter()
                        .chain(pact.interactions.iter())
                        .cloned()
                        .sorted_by(|a, b| {
                            let cmp = Ord::cmp(&a.provider_states.iter().map(|p| p.name.clone()).collect::<Vec<String>>(),
                                &b.provider_states.iter().map(|p| p.name.clone()).collect::<Vec<String>>());
                            if cmp == Ordering::Equal {
                                Ord::cmp(&a.description, &b.description)
                            } else {
                                cmp
                            }
                        }).into_iter()
                        .unique()
                        .collect(),
                    metadata: self.metadata.clone(),
                    specification_version: self.specification_version.clone()
                })
            }
        } else {
            Err(s!("Unable to merge pacts, as they have different consumers or providers"))
        }
    }

    /// Determins the default file name for the pact. This is based on the consumer and
    /// provider names.
    pub fn default_file_name(&self) -> String {
        format!("{}-{}.json", self.consumer.name, self.provider.name)
    }

    /// Reads the pact file and parses the resulting JSON into a `Pact` struct
    pub fn read_pact(file: &Path) -> io::Result<Pact> {
        let mut f = File::open(file)?;
        let pact_json = serde_json::from_reader(&mut f);
        match pact_json {
            Ok(ref json) => Ok(Pact::from_json(&format!("{:?}", file), json)),
            Err(err) => Err(Error::new(ErrorKind::Other, format!("Failed to parse Pact JSON - {}", err)))
        }
    }

    /// Reads the pact file from a URL and parses the resulting JSON into a `Pact` struct
    pub fn from_url(url: &String) -> Result<Pact, String> {
        let client = Client::new();
        match client.get(url).send() {
            Ok(mut res) => if res.status.is_success() {
                    let pact_json = serde_json::de::from_reader(&mut res);
                    match pact_json {
                        Ok(ref json) => Ok(Pact::from_json(url, json)),
                        Err(err) => Err(format!("Failed to parse Pact JSON - {}", err))
                    }
          } else {
                    Err(format!("Request failed with status - {}", res.status))
                },
            Err(err) => Err(format!("Request failed - {}", err))
          }
    }

    /// Writes this pact out to the provided file path. All directories in the path will
    /// automatically created. If an existing pact is found at the path, this pact will be
    /// merged into the pact file.
    pub fn write_pact(&self, path: &Path, pact_spec: PactSpecification) -> io::Result<()> {
        fs::create_dir_all(path.parent().unwrap())?;
        if path.exists() {
            let existing_pact = Pact::read_pact(path)?;
            match existing_pact.merge(self) {
                Ok(ref merged_pact) => {
                    let mut file = File::create(path)?;
                    file.write_all(format!("{}", serde_json::to_string_pretty(&merged_pact.to_json(pact_spec)).unwrap()).as_bytes())?;
                    Ok(())
                },
                Err(ref message) => Err(Error::new(ErrorKind::Other, message.clone()))
            }
        } else {
            let mut file = File::create(path)?;
            file.write_all(format!("{}", serde_json::to_string_pretty(&self.to_json(pact_spec)).unwrap()).as_bytes())?;
            Ok(())
        }
    }

    /// Returns a default Pact struct
    pub fn default() -> Pact {
        Pact {
            consumer: Consumer { name: s!("default_consumer") },
            provider: Provider { name: s!("default_provider") },
            interactions: Vec::new(),
            metadata: btreemap!{
                s!("pact-specification") => btreemap!{ s!("version") => PactSpecification::V3.version_str() },
                s!("pact-rust") => btreemap!{ s!("version") => s!(VERSION.unwrap_or("unknown")) }
            },
            specification_version: PactSpecification::V3
        }
    }
}

fn decode_query(query: &str) -> String {
    let mut chars = query.chars();
    let mut ch = chars.next();
    let mut result = String::new();

    while ch.is_some() {
        let c = ch.unwrap();
        if c == '%' {
            let c1 = chars.next();
            let c2 = chars.next();
            match (c1, c2) {
                (Some(v1), Some(v2)) => {
                    let mut s = String::new();
                    s.push(v1);
                    s.push(v2);
                    let decoded: Result<Vec<u8>, _> = FromHex::from_hex(s.into_bytes());
                    match decoded {
                        Ok(n) => result.push(n[0] as char),
                        Err(_) => {
                            result.push('%');
                            result.push(v1);
                            result.push(v2);
                        }
                    }
                },
                (Some(v1), None) => {
                    result.push('%');
                    result.push(v1);
                },
                _ => result.push('%')
            }
        } else if c == '+' {
            result.push(' ');
        } else {
            result.push(c);
        }

        ch = chars.next();
    }

    result
}

fn encode_query(query: &str) -> String {
    query.chars().map(|ch| {
        match ch {
            ' ' => s!("+"),
            '-' => ch.to_string(),
            'a'...'z' => ch.to_string(),
            'A'...'Z' => ch.to_string(),
            '0'...'9' => ch.to_string(),
            _ => ch.escape_unicode()
                .filter(|u| u.is_digit(16))
                .batching(|it| {
                    match it.next() {
                        None => None,
                        Some(x) => Some((x, it.next().unwrap()))
                    }
                })
                .map(|u| format!("%{}{}", u.0, u.1))
                .collect()
        }
    }).collect()
}

/// Parses a query string into an optional map. The query parameter name will be mapped to
/// a list of values. Where the query parameter is repeated, the order of the values will be
/// preserved.
pub fn parse_query_string(query: &String) -> Option<HashMap<String, Vec<String>>> {
    if !query.is_empty() {
        Some(query.split("&").map(|kv| {
            if kv.is_empty() {
                vec![]
            } else if kv.contains("=") {
                kv.splitn(2, "=").collect::<Vec<&str>>()
            } else {
                vec![kv]
            }
        }).fold(HashMap::new(), |mut map, name_value| {
            if !name_value.is_empty() {
                let name = decode_query(name_value[0]);
                let value = if name_value.len() > 1 {
                    decode_query(name_value[1])
                } else {
                    s!("")
                };
                map.entry(name).or_insert(vec![]).push(value);
            }
            map
        }))
    } else {
        None
    }
}

#[cfg(test)]
mod tests;