pact_consumer 1.4.3

Pact-Rust module that provides support for writing consumer pact tests
Documentation
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
use std::collections::HashMap;

use pact_models::bodies::OptionalBody;
use pact_models::expression_parser::DataType;
use pact_models::generators::{Generator, GeneratorCategory, Generators};
use pact_models::headers::parse_header;
use pact_models::matchingrules::MatchingRules;
use pact_models::path_exp::DocPath;

use crate::prelude::*;

/// Various methods shared between `RequestBuilder` and `ResponseBuilder`.
pub trait HttpPartBuilder {
    /// (Implementation detail.) This function fetches the mutable state that's
    /// needed to update this builder's `headers`. You should not need to use
    /// this under normal circumstances.
    ///
    /// This function has two return values because its job is to split a single
    /// `&mut` into two `&mut` pointing to sub-objects, which has to be done
    /// carefully in Rust.
    #[doc(hidden)]
    fn headers_and_matching_rules_mut(&mut self) -> (&mut HashMap<String, Vec<String>>, &mut MatchingRules);

    /// (Implementation detail.) This function fetches the mutable state that's
    /// needed to update this builder's `generators`. You should not need to use
    /// this under normal circumstances.
    #[doc(hidden)]
    fn generators(&mut self) -> &mut Generators;

    /// (Implementation detail.) This function fetches the mutable state that's
    /// needed to update this builder's `body`. You should not need to use this
    /// under normal circumstances.
    ///
    /// This function has two return values because its job is to split a single
    /// `&mut` into two `&mut` pointing to sub-objects, which has to be done
    /// carefully in Rust.
    #[doc(hidden)]
    fn body_and_matching_rules_mut(&mut self) -> (&mut OptionalBody, &mut MatchingRules);

    /// Specify a header pattern.
    ///
    /// ```
    /// use pact_consumer::prelude::*;
    /// use pact_consumer::*;
    /// use pact_consumer::builders::RequestBuilder;
    /// use regex::Regex;
    ///
    /// RequestBuilder::default()
    ///     .header("X-Simple", "value")
    ///     .header("X-Digits", term!("^[0-9]+$", "123"));
    /// ```
    #[allow(clippy::option_map_unit_fn)]
    fn header<N, V>(&mut self, name: N, value: V) -> &mut Self
    where
        N: Into<String>,
        V: Into<StringPattern>,
    {
      let name = name.into();
      let value = value.into();
      let example = parse_header(name.as_str(), value.to_example().as_str());
      {
        let (headers, rules) = self.headers_and_matching_rules_mut();
        let entry = headers.keys().cloned().find(|k| k.to_lowercase() == name.to_lowercase());
        if let Some(key) = entry {
          headers.get_mut(&key).map(|val| {
            val.extend(example);
          });
        } else {
          headers.insert(name.clone(), example);
        }
        let mut path = DocPath::root();
        path.push_field(name);
        value.extract_matching_rules(path, rules.add_category("header"))
      }
      self
    }

    /// Specify a header pattern and a generator from provider state.
    ///
    /// ```
    /// use pact_consumer::prelude::*;
    /// use pact_consumer::*;
    /// use pact_consumer::builders::RequestBuilder;
    /// use regex::Regex;
    ///
    /// RequestBuilder::default()
    ///     .header_from_provider_state("X-Simple", "providerState", "value")
    ///     .header_from_provider_state("X-Digits", "providerState", term!("^[0-9]+$", "123"));
    /// ```
    #[allow(clippy::option_map_unit_fn)]
    fn header_from_provider_state<N, E, V>(&mut self, name: N, expression: E, value: V) -> &mut Self
      where
        N: Into<String>,
        E: Into<String>,
        V: Into<StringPattern>,
    {
      let expression = expression.into();
      let sub_category = name.into();
      self.header(&sub_category, value);
      let mut sub_category_path = DocPath::root();
      sub_category_path.push_field(sub_category);
      {
        let generators = self.generators();
        generators.add_generator_with_subcategory(
          &GeneratorCategory::HEADER,
          sub_category_path,
          Generator::ProviderStateGenerator(expression, Some(DataType::STRING)),
        )
      }
      self
    }

    /// Set the `Content-Type` header.
    fn content_type<CT>(&mut self, content_type: CT) -> &mut Self
    where
        CT: Into<StringPattern>,
    {
        self.header("content-type", content_type)
    }

    /// Set the `Content-Type` header to `text/html`.
    fn html(&mut self) -> &mut Self {
        self.content_type("text/html")
    }

    /// Set the `Content-Type` header to `application/json; charset=utf-8`,
    /// with enough flexibility to cover common variations.
    fn json_utf8(&mut self) -> &mut Self {
        self.content_type(term!(
            "^application/json; charset=(utf|UTF)-8$",
            "application/json; charset=utf-8"
        ))
    }

    /// Specify a body literal. This does not allow using patterns.
    ///
    /// ```
    /// use pact_consumer::prelude::*;
    /// use pact_consumer::builders::RequestBuilder;
    ///
    /// RequestBuilder::default().body("Hello");
    /// ```
    fn body<B: Into<String>>(&mut self, body: B) -> &mut Self {
        let body = body.into();
        {
            let (body_ref, _) = self.body_and_matching_rules_mut();
            *body_ref = OptionalBody::Present(body.into(), None, None);
        }
        self
    }

  /// Specify a body literal with content type. This does not allow using patterns.
  ///
  /// ```
  /// use pact_consumer::prelude::*;
  /// use pact_consumer::builders::RequestBuilder;
  ///
  /// RequestBuilder::default().body2("Hello", "plain/text");
  /// ```
  fn body2<B: Into<String>>(&mut self, body: B, content_type: B) -> &mut Self {
    let body = body.into();
    {
      let (body_ref, _) = self.body_and_matching_rules_mut();
      *body_ref = OptionalBody::Present(body.into(), content_type.into().parse().ok(), None);
    }
    self
  }

    /// Specify the body as `JsonPattern`, possibly including special matching
    /// rules.
    ///
    /// ```
    /// use pact_consumer::prelude::*;
    /// use pact_consumer::*;
    /// use pact_consumer::builders::RequestBuilder;
    ///
    /// RequestBuilder::default().json_body(json_pattern!({
    ///     "message": like!("Hello"),
    /// }));
    /// ```
    fn json_body<B: Into<JsonPattern>>(&mut self, body: B) -> &mut Self {
        let body = body.into();
        {
            let (body_ref, rules) = self.body_and_matching_rules_mut();
            *body_ref = OptionalBody::Present(body.to_example().to_string().into(), Some("application/json".into()), None);
            body.extract_matching_rules(DocPath::root(), rules.add_category("body"));
        }
        self
    }

  /// Specify a text body (text/plain) matching the given pattern.
  ///
  /// ```
  /// use pact_consumer::prelude::*;
  /// use pact_consumer::builders::RequestBuilder;
  /// use pact_consumer::term;
  ///
  /// RequestBuilder::default().body_matching(term!("^(True|False)$", "True"));
  /// ```
  fn body_matching<P: Into<StringPattern>>(&mut self, body: P) -> &mut Self {
    let body = body.into();
    {
      let (body_ref, rules) = self.body_and_matching_rules_mut();
      *body_ref = OptionalBody::Present(body.to_example_bytes().into(), Some("text/plain".into()), None);
      body.extract_matching_rules(DocPath::root(), rules.add_category("body"));
    }
    self
  }

  /// Specify a text body matching the given pattern with a content type.
  ///
  /// ```
  /// use pact_consumer::prelude::*;
  /// use pact_consumer::builders::RequestBuilder;
  /// use pact_consumer::term;
  ///
  /// RequestBuilder::default().body_matching2(term!("^(True|False)$", "True"), "text/ascii");
  /// ```
  fn body_matching2<P: Into<StringPattern>, B: Into<String>>(&mut self, body: P, content_type: B) -> &mut Self {
    let body = body.into();
    {
      let (body_ref, rules) = self.body_and_matching_rules_mut();
      *body_ref = OptionalBody::Present(body.to_example_bytes().into(), content_type.into().parse().ok(), None);
      body.extract_matching_rules(DocPath::root(), rules.add_category("body"));
    }
    self
  }
}

#[cfg(test)]
mod tests {
  use std::collections::HashMap;

  use expectest::prelude::*;
  use maplit::hashmap;
  use pact_models::matchingrules::MatchingRule;
  use pact_models::matchingrules_list;
  use regex::Regex;
  use serde_json::json;

  use crate::builders::{HttpPartBuilder, PactBuilder};
  use crate::patterns::{Like, Term};

  #[test_log::test]
  fn header_pattern() {
    let application_regex = Regex::new("application/.*").unwrap();
    let pattern = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.header(
          "Content-Type",
          Term::new(application_regex, "application/json"),
        );
        i
      })
      .build();
    let good = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.header("Content-Type", "application/xml");
        i
      })
      .build();
    let bad = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.header("Content-Type", "text/html");
        i
      })
      .build();
    assert_requests_match!(good, pattern);
    assert_requests_do_not_match!(bad, pattern);
  }

  #[test]
  fn header_generator() {
    let actual = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.header_from_provider_state(
          "Authorization",
          "token",
          "some-token",
        );
        i
      }).build();

    let expected = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.header("Authorization", "from-provider-state");
        i
      })
      .build();

    let good_context = &mut HashMap::new();
    good_context.insert("token", json!("from-provider-state"));
    assert_requests_with_context_match!(actual, expected, good_context);

    let bad_context = &mut HashMap::new();
    bad_context.insert("token", json!("not-from-provider-state"));
    assert_requests_with_context_do_not_match!(actual, expected, bad_context);
  }

  #[test]
  fn body_literal() {
    let pattern = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.body("Hello");
        i
      })
      .build();
    let good = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.body("Hello");
        i
      })
      .build();
    let bad = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.body("Bye");
        i
      })
      .build();
    assert_requests_match!(good, pattern);
    assert_requests_do_not_match!(bad, pattern);
  }

  #[test_log::test]
  fn json_body_pattern() {
    let pattern = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.json_body(json_pattern!({
                "message": Like::new(json_pattern!("Hello")),
            }));
        i
      })
      .build();
    let good = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.json_body(json_pattern!({ "message": "Goodbye" }));
        i
      })
      .build();
    let bad = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.json_body(json_pattern!({ "message": false }));
        i
      })
      .build();
    assert_requests_match!(good, pattern);
    assert_requests_do_not_match!(bad, pattern);
  }

  #[test]
  fn text_body_pattern() {
    let pact = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.body_matching(term!("^(True|False)$", "True"));
        i
      })
      .build()
      .as_v4_pact().unwrap();
    let interaction = pact.interactions.first()
      .unwrap().as_request_response().unwrap();
    expect!(interaction.request.body.value_as_string().unwrap()).to(be_equal_to("True"));
    expect!(interaction.request.matching_rules.rules_for_category("body").unwrap()).to(
      be_equal_to(matchingrules_list! {
        "body"; "$" => [ MatchingRule::Regex("^(True|False)$".to_string()) ]
      })
    );
  }

  #[test]
  fn header_with_different_case_keys() {
    let pattern = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.header("Content-Type", "application/json");
        i.request.header("content-type", "application/xml");
        i
      })
      .build();
    let interactions = pattern.interactions();
    let first_interaction = interactions.first().unwrap().as_request_response().unwrap();
    expect!(first_interaction.request.headers).to(be_some().value(hashmap!{
      "Content-Type".to_string() => vec![ "application/json".to_string(), "application/xml".to_string() ]
    }));
  }

  #[test]
  fn multi_value_header() {
    let pattern = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
        i.request.header("accept", "application/problem+json, application/json, text/plain, */*");
        i
      })
      .build();
    let interactions = pattern.interactions();
    let first_interaction = interactions.first().unwrap().as_request_response().unwrap();
    expect!(first_interaction.request.headers).to(be_some().value(hashmap!{
      "accept".to_string() => vec![
        "application/problem+json".to_string(),
        "application/json".to_string(),
        "text/plain".to_string(),
        "*/*".to_string()
      ]
    }));
  }
}