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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::collections::HashMap;

use bytes::Bytes;
#[cfg(test)]
#[allow(unused_imports)]
use env_logger;
use maplit::*;
use pact_models::bodies::OptionalBody;
use pact_models::content_types::ContentType;
use pact_models::expression_parser::DataType;
use pact_models::generators::{Generator, GeneratorCategory, Generators};
#[cfg(feature = "plugins")] use pact_models::http_parts::HttpPart;
use pact_models::json_utils::body_from_json;
use pact_models::matchingrules::{Category, MatchingRules};
use pact_models::path_exp::DocPath;
use pact_models::request::Request;
use pact_models::v4::http_parts::HttpRequest;
use pact_models::v4::interaction::InteractionMarkup;
#[cfg(feature = "plugins")] use pact_plugin_driver::catalogue_manager::find_content_matcher;
#[cfg(feature = "plugins")] use pact_plugin_driver::content::PluginConfiguration;
#[cfg(test)]
use regex::Regex;
#[cfg(test)]
use serde_json::json;
use serde_json::Value;
#[allow(unused_imports)] use tracing::debug;

use crate::prelude::*;
use crate::util::GetDefaulting;

#[cfg(not(feature = "plugins"))]
#[derive(Clone, Debug)]
struct PluginConfiguration {}

/// Builder for `Request` objects. Normally created via `PactBuilder`.
#[derive(Clone, Debug)]
pub struct RequestBuilder {
  request: HttpRequest,
  #[allow(dead_code)] pub(crate) plugin_config: HashMap<String, PluginConfiguration>,
  interaction_markup: InteractionMarkup
}

impl RequestBuilder {
    /// Specify the request method. Defaults to `"GET"`.
    ///
    /// ```
    /// use pact_consumer::builders::RequestBuilder;
    /// use pact_consumer::prelude::*;
    ///
    /// let request = RequestBuilder::default().method("POST").build();
    /// assert_eq!(request.method, "POST");
    /// ```
    pub fn method<M: Into<String>>(&mut self, method: M) -> &mut Self {
        self.request.method = method.into();
        self
    }

    /// Set the HTTP method to `GET`. This is the default, so we don't actually
    /// care.
    pub fn get(&mut self) -> &mut Self {
        self.method("GET")
    }

    /// Set the HTTP method to `POST`.
    pub fn post(&mut self) -> &mut Self {
        self.method("POST")
    }

    /// Set the HTTP method to `PUT`.
    pub fn put(&mut self) -> &mut Self {
        self.method("PUT")
    }

    /// Set the HTTP method to `DELETE`.
    pub fn delete(&mut self) -> &mut Self {
        self.method("DELETE")
    }

    /// Specify the request path. Defaults to `"/"`.
    pub fn path<P: Into<StringPattern>>(&mut self, path: P) -> &mut Self {
        let path = path.into();
        self.request.path = path.to_example();
        path.extract_matching_rules(
            DocPath::empty(),
            self.request.matching_rules.add_category(Category::PATH),
        );
        self
    }

    /// Specify the request path with generators. Defaults to `"/"`.
    pub fn path_from_provider_state<E, P: Into<StringPattern>>(&mut self, expression: E, path: P) -> &mut Self
        where
          E: Into<String>
    {
        let path = path.into();
        let expression = expression.into();
        self.path(path);
        {
            let generators = self.generators();
            generators.add_generator(&GeneratorCategory::PATH, Generator::ProviderStateGenerator(expression, Some(DataType::STRING)))
        }
        self
    }

    /// Specify a query parameter. You may pass either a single value or
    /// a list of values to represent a repeated parameter.
    ///
    /// ```
    /// use pact_consumer::*;
    /// use pact_consumer::builders::RequestBuilder;
    /// use regex::Regex;
    ///
    /// RequestBuilder::default()
    ///     .query_param("simple", "value")
    ///     .query_param("pattern", term!("^[0-9]+$", "123"));
    /// ```
    ///
    /// To pass multiple parameters with the same name, call `query_param` more
    /// than once with the same `key`.
    pub fn query_param<K, V>(&mut self, key: K, value: V) -> &mut Self
    where
        K: Into<String>,
        V: Into<StringPattern>,
    {
        let key = key.into();
        let value = value.into();

        // Extract our example JSON and add it the `Vec` for the appropriate
        // parameter.
        self.request
            .query
            .get_defaulting()
            .entry(key.clone())
            .or_insert_with(Default::default)
            .push(Some(value.to_example()));

        let mut path = DocPath::root();
        path.push_field(key);

        // Extract our matching rules.
        value.extract_matching_rules(
            path,
            self.request.matching_rules.add_category("query"),
        );

        self
    }

    /// Build the specified `Request` object.
    pub fn build(&self) -> Request {
         self.request.as_v3_request()
    }

    /// Build the specified `Request` object in V4 format.
    pub fn build_v4(&self) -> HttpRequest {
        self.request.clone()
    }

  // TODO: This needs to setup rules/generators based on the content type
  fn setup_core_matcher(&mut self, content_type: &ContentType, definition: Value) {
    match definition {
      Value::String(s) => self.request.body = OptionalBody::Present(Bytes::from(s), Some(content_type.clone()), None),
      Value::Object(ref o) => if o.contains_key("contents") {
        self.request.body = body_from_json(&definition, "contents", &None);
      }
      _ => {}
    }
  }

  /// Set the request body using the JSON data. If the body is being supplied by a plugin,
  /// this is what is sent to the plugin to setup the body.
  pub async fn contents(&mut self, content_type: ContentType, definition: Value) -> &mut Self {

    #[cfg(feature = "plugins")]
    {
      match find_content_matcher(&content_type) {
        Some(matcher) => {
          debug!("Found a matcher for '{}': {:?}", content_type, matcher);
          if matcher.is_core() {
            debug!("Matcher is from the core framework");
            self.setup_core_matcher(&content_type, definition);
          } else {
            let request = &mut self.request;
            debug!("Plugin matcher, will get the plugin to provide the request contents");
            match definition {
              Value::Object(attributes) => {
                let map = attributes.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
                match matcher.configure_interaction(&content_type, map).await {
                  Ok((contents, plugin_config)) => {
                    debug!("Interaction contents = {:?}", contents);
                    debug!("Interaction plugin_config = {:?}", plugin_config);

                    let request_contents = contents.iter()
                      .filter(|interaction| interaction.part_name == "request")
                      .next()
                      .or_else(|| contents.iter()
                        .filter(|interaction| interaction.part_name == "")
                        .next());
                    if let Some(contents) = request_contents {
                      request.body = contents.body.clone();
                      if !request.has_header("content-type") {
                        request.add_header("content-type", vec![content_type.to_string().as_str()]);
                      }
                      if let Some(rules) = &contents.rules {
                        request.matching_rules.add_rules("body", rules.clone());
                      }
                      if let Some(generators) = &contents.generators {
                        request.generators.add_generators(generators.clone());
                      }
                      if !contents.plugin_config.is_empty() {
                        let plugin_config = PluginConfiguration {
                          interaction_configuration: hashmap!{
                            "request".to_string() => Value::Object(
                              contents.plugin_config.interaction_configuration
                                .iter()
                                .map(|(k, v)| (k.clone(), v.clone()))
                                .collect()
                            )
                          },
                          pact_configuration: contents.plugin_config.pact_configuration.clone(),
                        };
                        self.plugin_config.insert(matcher.plugin_name(), plugin_config);
                      }
                      self.interaction_markup = InteractionMarkup {
                        markup: contents.interaction_markup.clone(),
                        markup_type: contents.interaction_markup_type.clone()
                      };
                    }

                    if let Some(plugin_config) = plugin_config {
                      let plugin_name = matcher.plugin_name();
                      if self.plugin_config.contains_key(&*plugin_name) {
                        let entry = self.plugin_config.get_mut(&*plugin_name).unwrap();
                        for (k, v) in plugin_config.pact_configuration {
                          entry.pact_configuration.insert(k.clone(), v.clone());
                        }
                      } else {
                        self.plugin_config.insert(plugin_name.to_string(), plugin_config.clone());
                      }
                    }
                  }
                  Err(err) => panic!("Failed to call out to plugin - {}", err)
                }
              }
              _ => panic!("{} is not a valid value for contents", definition)
            }
          }
        }
        None => {
          debug!("No matcher was found, will default to the core framework");
          self.setup_core_matcher(&content_type, definition);
        }
      }
    }

    #[cfg(not(feature = "plugins"))]
    {
      self.setup_core_matcher(&content_type, definition);
    }

    self
  }

  /// Configure the interaction contents from a plugin builder
  #[cfg(feature = "plugins")]
  pub async fn contents_for_plugin<B: PluginInteractionBuilder>(&mut self, content_type: ContentType, builder: B) -> &mut Self {
    self.contents(content_type, builder.build()).await
  }

  #[cfg(feature = "plugins")]
  pub(crate) fn plugin_config(&self) -> HashMap<String, PluginConfiguration> {
    self.plugin_config.clone()
  }

  pub(crate) fn interaction_markup(&self) -> InteractionMarkup {
    self.interaction_markup.clone()
  }
}

impl Default for RequestBuilder {
    fn default() -> Self {
        RequestBuilder {
          request: HttpRequest::default(),
          plugin_config: Default::default(),
          interaction_markup: Default::default()
        }
    }
}

impl HttpPartBuilder for RequestBuilder {
  fn headers_and_matching_rules_mut(&mut self) -> (&mut HashMap<String, Vec<String>>, &mut MatchingRules) {
    (
      self.request.headers.get_or_insert(hashmap!{}),
      &mut self.request.matching_rules,
    )
  }

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

  fn body_and_matching_rules_mut(&mut self) -> (&mut OptionalBody, &mut MatchingRules) {
      (
          &mut self.request.body,
          &mut self.request.matching_rules,
      )
  }
}

#[test]
fn path_pattern() {
    let greeting_regex = Regex::new("/greeting/.*").unwrap();
    let pattern = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| {
            i.request.path(Term::new(greeting_regex, "/greeting/hello"));
            i
        })
        .build();
    let good = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| { i.request.path("/greeting/hi"); i })
        .build();
    let bad = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| { i.request.path("/farewell/bye"); i })
        .build();
    assert_requests_match!(good, pattern);
    assert_requests_do_not_match!(bad, pattern);
}

#[test]
fn path_generator() {
    let actual = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
          i.request.path_from_provider_state("/greeting/${greeting}", "/greeting/hi");
          i
      })
      .build();

    let expected = PactBuilder::new("C", "P")
      .interaction("I", "", |mut i| {
          i.request.path("/greeting/hello");
          i
      })
      .build();

    let good_context = &mut HashMap::new();
    good_context.insert("greeting", json!("hello"));
    assert_requests_with_context_match!(actual, expected, good_context);

    let bad_context = &mut HashMap::new();
    bad_context.insert("greeting", json!("goodbye"));
    assert_requests_with_context_do_not_match!(actual, expected, bad_context);
}

#[test]
fn query_param_pattern() {
    let pattern = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| {
            i.request.query_param("greeting", term!("^h.*$", "hello"));
            i
        })
        .build();
    let good = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| { i.request.query_param("greeting", "hi"); i })
        .build();
    let bad = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| { i.request.query_param("greeting", "bye"); i })
        .build();
    assert_requests_match!(good, pattern);
    assert_requests_do_not_match!(bad, pattern);
}

#[test]
fn query_param_with_underscore() {
    let pattern = PactBuilder::new("C", "P")
        .interaction("get a user", "", |mut i| {
            i.request
                .path("/users")
                // This `term!` was being ignored in `pact_matching`, but only
                // if there was an underscore.
                .query_param("user_id", term!("^[0-9]+$", "1"));
            i
        })
        .build();
    let good = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| {
            i.request
                .path("/users")
                // Call with a different ID than we expected.
                .query_param("user_id", "2");
            i
        })
        .build();
    assert_requests_match!(good, pattern);
}

#[test]
fn term_does_not_require_anchors() {
    use crate::prelude::*;

    let pattern = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| {
            // Unfortunately, we appear to need a leading "^" and trailing "$"
            // on this regex, or else it will match the other examples below.
            i.request.path(term!("^/users/[0-9]+$", "/users/12"));
            i
        })
        .build();
    let good = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| { i.request.path("/users/2"); i })
        .build();
    let bad1 = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| { i.request.path("/users/2/posts"); i })
        .build();
    let bad2 = PactBuilder::new("C", "P")
        .interaction("I", "", |mut i| { i.request.path("/account/1/users/2"); i })
        .build();
    assert_requests_match!(good, pattern);
    assert_requests_do_not_match!(bad1, pattern);
    assert_requests_do_not_match!(bad2, pattern);
}