libdeadmock 0.1.6

API Mocking and Virtualization
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
// Copyright (c) 2018 libdeadmock developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! HTTP request matching for the server.
use bitflags::bitflags;
#[cfg(feature = "headers")]
use crate::config::Header;
use crate::config::{Mapping, Mappings, Request as RequestConfig};
use crate::error::Error;
use crate::error::ErrorKind::MappingNotFound;
#[cfg(feature = "headers")]
use http::header::{HeaderName, HeaderValue};
use http::Request;
use slog::{trace, Logger};
use slog_try::try_trace;
use std::fmt;

#[cfg(feature = "header")]
crate mod header;
#[cfg(feature = "headers")]
crate mod headers;
#[cfg(feature = "method")]
crate mod method;
#[cfg(feature = "url")]
crate mod url;

#[cfg(all(feature = "exact_match", feature = "header"))]
pub use self::header::ExactMatch as ExactMatchHeader;
#[cfg(all(feature = "pattern_match", feature = "header"))]
pub use self::header::PatternMatch as PatternMatchHeader;
#[cfg(all(feature = "exact_match", feature = "headers"))]
pub use self::headers::ExactMatch as ExactMatchHeaders;
#[cfg(all(feature = "pattern_match", feature = "headers"))]
pub use self::headers::PatternMatch as PatternMatchHeaders;
#[cfg(all(feature = "exact_match", feature = "method"))]
pub use self::method::ExactMatch as ExactMatchMethod;
#[cfg(all(feature = "pattern_match", feature = "method"))]
pub use self::method::PatternMatch as PatternMatchMethod;
#[cfg(all(feature = "exact_match", feature = "url"))]
pub use self::url::ExactMatch as ExactMatchUrl;
#[cfg(all(feature = "pattern_match", feature = "url"))]
pub use self::url::PatternMatch as PatternMatchUrl;

bitflags!{
    /// Enabled flags for request matching types
    pub struct Enabled: u32 {
        /// Enable the exact matching on url
        #[cfg(all(feature = "exact_match", feature = "url"))]
        const EXACT_URL       = 0b0000_0000_0001;
        /// Enable the exact matching on method
        #[cfg(all(feature = "exact_match", feature = "method"))]
        const EXACT_METHOD    = 0b0000_0000_0010;
        /// Enable the exact matching on all headers
        #[cfg(all(feature = "exact_match", feature = "headers"))]
        const EXACT_HEADERS   = 0b0000_0000_0100;
        /// Enable the exact matching on one header
        #[cfg(all(feature = "exact_match", feature = "header"))]
        const EXACT_HEADER    = 0b0000_0000_1000;
        /// Enable the pattern matching on url
        #[cfg(all(feature = "pattern_match", feature = "url"))]
        const PATTERN_URL     = 0b0000_0001_0000;
        /// Enable the pattern matching on one header
        #[cfg(all(feature = "pattern_match", feature = "header"))]
        const PATTERN_HEADER  = 0b0000_1000_0000;
        /// Enable the pattern matching on method
        #[cfg(all(feature = "pattern_match", feature = "method"))]
        const PATTERN_METHOD  = 0b0001_0000_0000;
        /// Enable the pattern matching on all headers
        #[cfg(all(feature = "pattern_match", feature = "headers"))]
        const PATTERN_HEADERS = 0b0010_0000_0000;
    }
}

impl Enabled {
    /// Enable all of the exact matching.
    pub fn exact() -> Self {
        Self::exact_url() | Self::exact_method() | Self::exact_header() | Self::exact_headers()
    }

    /// Enable all of the pattern matching.
    pub fn pattern() -> Self {
        Self::pattern_url()
            | Self::pattern_method()
            | Self::pattern_header()
            | Self::pattern_headers()
    }

    #[cfg(all(feature = "exact_match", feature = "url"))]
    fn exact_url() -> Self {
        Self::EXACT_URL
    }

    #[cfg(not(all(feature = "exact_match", feature = "url")))]
    fn exact_url() -> Self {
        Self::empty()
    }

    #[cfg(all(feature = "exact_match", feature = "method"))]
    fn exact_method() -> Self {
        Self::EXACT_METHOD
    }

    #[cfg(not(all(feature = "exact_match", feature = "method")))]
    fn exact_method() -> Self {
        Self::empty()
    }

    #[cfg(all(feature = "exact_match", feature = "header"))]
    fn exact_header() -> Self {
        Self::EXACT_HEADER
    }

    #[cfg(not(all(feature = "exact_match", feature = "header")))]
    fn exact_header() -> Self {
        Self::empty()
    }

    #[cfg(all(feature = "exact_match", feature = "headers"))]
    fn exact_headers() -> Self {
        Self::EXACT_HEADERS
    }

    #[cfg(not(all(feature = "exact_match", feature = "headers")))]
    fn exact_headers() -> Self {
        Self::empty()
    }

    #[cfg(all(feature = "pattern_match", feature = "url"))]
    fn pattern_url() -> Self {
        Self::PATTERN_URL
    }

    #[cfg(not(all(feature = "pattern_match", feature = "url")))]
    fn pattern_url() -> Self {
        Self::empty()
    }

    #[cfg(all(feature = "pattern_match", feature = "method"))]
    fn pattern_method() -> Self {
        Self::PATTERN_METHOD
    }

    #[cfg(not(all(feature = "pattern_match", feature = "method")))]
    fn pattern_method() -> Self {
        Self::empty()
    }

    #[cfg(all(feature = "pattern_match", feature = "header"))]
    fn pattern_header() -> Self {
        Self::PATTERN_HEADER
    }

    #[cfg(not(all(feature = "pattern_match", feature = "header")))]
    fn pattern_header() -> Self {
        Self::empty()
    }

    #[cfg(all(feature = "pattern_match", feature = "headers"))]
    fn pattern_headers() -> Self {
        Self::PATTERN_HEADERS
    }

    #[cfg(not(all(feature = "pattern_match", feature = "headers")))]
    fn pattern_headers() -> Self {
        Self::empty()
    }
}

impl fmt::Display for Enabled {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[cfg(feature = "headers")]
crate type HeaderTuple = (HeaderName, HeaderValue);
#[cfg(feature = "headers")]
crate type HeaderTupleRef<'a> = (&'a HeaderName, &'a HeaderValue);

#[cfg(feature = "headers")]
crate fn to_header_tuple(header: &Header) -> Result<HeaderTuple, failure::Error> {
    Ok((
        HeaderName::from_bytes(header.key().as_bytes())?,
        HeaderValue::from_bytes(header.value().as_bytes())?,
    ))
}

#[cfg(feature = "headers")]
crate fn equal_headers(actual: HeaderTupleRef<'_>, expected: HeaderTupleRef<'_>) -> bool {
    actual == expected
}

/// A struct that supports slog logging
pub trait Slogger {
    /// Add an optional stdout `slog` logger to the struct.
    fn set_stdout(self, stdout: Option<Logger>) -> Self;
    /// Add an optional stderr `slog` logger to the struct.
    fn set_stderr(self, stderr: Option<Logger>) -> Self;
}

/// A request matcher
pub trait RequestMatch: fmt::Debug + fmt::Display {
    /// Does the incoming request match the request configuration from a mapping.
    ///
    /// If the matcher has configuration, then `is_match` must return `Some(bool)`.
    /// Otherwise, `is_match` must return `None`
    fn is_match(
        &self,
        request: &Request<()>,
        request_config: &RequestConfig,
    ) -> Result<Option<bool>, Error>;
}

/// Try to match an incoming request to a mapping.
#[allow(box_pointers)]
pub struct Matcher {
    /// The matchers setup for request matching.
    matchers: Vec<Box<dyn RequestMatch>>,
    /// stdout slog logger
    stdout: Option<Logger>,
    /// stderr slog logger
    stderr: Option<Logger>,
}

#[allow(box_pointers)]
impl fmt::Debug for Matcher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.matchers
            .iter()
            .map(|matcher| write!(f, "{:?},", matcher))
            .collect()
    }
}

#[cfg(all(feature = "exact_match", feature = "url"))]
fn enable_exact_match_url(enabled: Enabled, matcher: &mut Matcher) {
    enable_matcher::<ExactMatchUrl>(enabled, Enabled::EXACT_URL, matcher);
}

#[cfg(not(all(feature = "exact_match", feature = "url")))]
fn enable_exact_match_url(_enabled: Enabled, _matcher: &mut Matcher) {}

#[cfg(all(feature = "pattern_match", feature = "url"))]
fn enable_pattern_match_url(enabled: Enabled, matcher: &mut Matcher) {
    enable_matcher::<PatternMatchUrl>(enabled, Enabled::PATTERN_URL, matcher);
}

#[cfg(not(all(feature = "pattern_match", feature = "url")))]
fn enable_pattern_match_url(_enabled: Enabled, _matcher: &mut Matcher) {}

#[cfg(all(feature = "exact_match", feature = "method"))]
fn enable_exact_match_method(enabled: Enabled, matcher: &mut Matcher) {
    enable_matcher::<ExactMatchMethod>(enabled, Enabled::EXACT_METHOD, matcher);
}

#[cfg(not(all(feature = "exact_match", feature = "method")))]
fn enable_exact_match_method(_enabled: Enabled, _matcher: &mut Matcher) {}

#[cfg(all(feature = "pattern_match", feature = "method"))]
fn enable_pattern_match_method(enabled: Enabled, matcher: &mut Matcher) {
    enable_matcher::<PatternMatchMethod>(enabled, Enabled::PATTERN_METHOD, matcher);
}

#[cfg(not(all(feature = "pattern_match", feature = "method")))]
fn enable_pattern_match_method(_enabled: Enabled, _matcher: &mut Matcher) {}

#[cfg(all(feature = "exact_match", feature = "header"))]
fn enable_exact_match_header(enabled: Enabled, matcher: &mut Matcher) {
    enable_matcher::<ExactMatchHeader>(enabled, Enabled::EXACT_HEADER, matcher);
}

#[cfg(not(all(feature = "exact_match", feature = "header")))]
fn enable_exact_match_header(_enabled: Enabled, _matcher: &mut Matcher) {}

#[cfg(all(feature = "pattern_match", feature = "header"))]
fn enable_pattern_match_header(enabled: Enabled, matcher: &mut Matcher) {
    enable_matcher::<PatternMatchHeader>(enabled, Enabled::PATTERN_HEADER, matcher);
}

#[cfg(not(all(feature = "pattern_match", feature = "header")))]
fn enable_pattern_match_header(_enabled: Enabled, _matcher: &mut Matcher) {}

#[cfg(all(feature = "exact_match", feature = "headers"))]
fn enable_exact_match_headers(enabled: Enabled, matcher: &mut Matcher) {
    enable_matcher::<ExactMatchHeaders>(enabled, Enabled::EXACT_HEADERS, matcher);
}

#[cfg(not(all(feature = "exact_match", feature = "headers")))]
fn enable_exact_match_headers(_enabled: Enabled, _matcher: &mut Matcher) {}

#[cfg(all(feature = "pattern_match", feature = "headers"))]
fn enable_pattern_match_headers(enabled: Enabled, matcher: &mut Matcher) {
    enable_matcher::<PatternMatchHeaders>(enabled, Enabled::PATTERN_HEADERS, matcher);
}

#[cfg(not(all(feature = "pattern_match", feature = "headers")))]
fn enable_pattern_match_headers(_enabled: Enabled, _matcher: &mut Matcher) {}

fn enable_matcher<T>(enabled: Enabled, contains: Enabled, matcher: &mut Matcher)
where
    T: 'static + RequestMatch + Default + Slogger,
{
    if enabled.contains(contains) {
        let _ = matcher.push(
            T::default()
                .set_stdout(matcher.stdout.clone())
                .set_stderr(matcher.stderr.clone()),
        );
    }
}

#[allow(box_pointers)]
impl Matcher {
    /// Create a new `Matcher`
    pub fn new(enabled: Enabled, stdout: Option<Logger>, stderr: Option<Logger>) -> Self {
        let mut matcher = Self {
            matchers: vec![],
            stdout,
            stderr,
        };

        enable_exact_match_url(enabled, &mut matcher);
        enable_pattern_match_url(enabled, &mut matcher);
        enable_exact_match_method(enabled, &mut matcher);
        enable_pattern_match_method(enabled, &mut matcher);
        enable_exact_match_header(enabled, &mut matcher);
        enable_pattern_match_header(enabled, &mut matcher);
        enable_exact_match_headers(enabled, &mut matcher);
        enable_pattern_match_headers(enabled, &mut matcher);

        matcher
    }

    /// Add a request matcher to the list.
    fn push<T: RequestMatch + 'static>(&mut self, request_match: T) -> &mut Self {
        self.matchers.push(Box::new(request_match));
        self
    }

    /// Get a mapping that matches the given request.
    pub fn get_match(&self, request: &Request<()>, mappings: &Mappings) -> Result<Mapping, Error> {
        mappings
            .inner()
            .iter()
            .inspect(|(_uuid, mapping)| {
                try_trace!(self.stdout, "");
                try_trace!(
                    self.stdout,
                    "{:#^1$}",
                    format!(" Checking '{}' ", mapping.name()),
                    80
                );
            })
            .filter_map(|(_uuid, mapping)| self.is_match(request, mapping))
            .min()
            .ok_or_else(|| MappingNotFound.into())
    }

    fn is_match(&self, request: &Request<()>, mapping: &Mapping) -> Option<Mapping> {
        let matches = self
            .matchers
            .iter()
            // Generate a list of matches
            // * If the matcher was configured and matches, returns `Some(true)`
            // * If the matcher was configured and doesn't match, returns `Some(false)`
            // * If the matcher was not configured, returns `None`
            .map(|matcher| matcher.is_match(request, mapping.request()))
            // Filter out any Errors
            .filter_map(|res| res.ok())
            // Filter out the `None` from matchers that weren't configured
            .filter_map(|x| x)
            .collect::<Vec<bool>>();

        let all_true = matches.iter().all(|x| *x);
        try_trace!(self.stdout, "Matches: {:?}, All: {}", matches, all_true);

        // Is the remaining list non-empty and all true?
        if !matches.is_empty() && all_true {
            Some(mapping.clone())
        } else {
            None
        }
    }
}

impl Slogger for Matcher {
    /// Add a stdout logger
    fn set_stdout(mut self, stdout: Option<Logger>) -> Self {
        self.stdout = stdout;
        self
    }

    /// Add a stderr logger
    fn set_stderr(mut self, stderr: Option<Logger>) -> Self {
        self.stderr = stderr;
        self
    }
}

#[cfg(test)]
mod test {
    use super::Matcher;
    use crate::config::files::test::test_files;
    use crate::config::mappings::test::test_mappings;
    use crate::matcher::Enabled;
    use http::request::Builder;
    use http::Request;
    // use slog::{o, Drain};
    // use slog_term;

    #[test]
    fn enable_pattern() {
        let all_pattern = Enabled::pattern();
        assert!(!all_pattern.is_empty());
        assert!(all_pattern.contains(
            Enabled::PATTERN_URL
                | Enabled::PATTERN_METHOD
                | Enabled::PATTERN_HEADER
                | Enabled::PATTERN_HEADERS
        ));
        assert!(!all_pattern.contains(Enabled::EXACT_URL));
        assert!(!all_pattern.contains(Enabled::EXACT_METHOD));
        assert!(!all_pattern.contains(Enabled::EXACT_HEADER));
        assert!(!all_pattern.contains(Enabled::EXACT_HEADERS));
    }

    #[test]
    fn enable_exact() {
        let all_exact = Enabled::exact();
        assert!(!all_exact.is_empty());
        assert!(all_exact.contains(
            Enabled::EXACT_URL
                | Enabled::EXACT_METHOD
                | Enabled::EXACT_HEADER
                | Enabled::EXACT_HEADERS
        ));
        assert!(!all_exact.contains(Enabled::PATTERN_URL));
        assert!(!all_exact.contains(Enabled::PATTERN_METHOD));
        assert!(!all_exact.contains(Enabled::PATTERN_HEADER));
        assert!(!all_exact.contains(Enabled::PATTERN_HEADERS));
    }

    #[allow(box_pointers)]
    fn check_request(enabled: Enabled, request_builder: &mut Builder, priority: u8, name: &str) {
        let mappings = test_mappings().expect("Unable to setup mappings!");
        // let decorator = slog_term::PlainDecorator::new(std::io::stderr());
        // let drain = slog_term::CompactFormat::new(decorator).build().fuse();
        // let drain = slog_async::Async::new(drain).build().fuse();
        // let log = slog::Logger::root(drain, o!("test" => "test"));

        let matcher = Matcher::new(enabled, None, None);
        assert!(!matcher.matchers.is_empty());

        if let Ok(request) = request_builder.body(()) {
            if let Ok(mapping) = matcher.get_match(&request, &mappings) {
                assert_eq!(mapping.name(), name);
                assert_eq!(*mapping.priority(), priority);
                assert!(mapping.response().body_file_name().is_some());
            } else {
                assert!(false, "Unable to find a matching mapping!");
            }
        } else {
            assert!(false, "Unable to build the request to test!");
        }
    }

    #[allow(box_pointers)]
    fn check_no_match(enabled: Enabled, request_builder: &mut Builder) {
        let mappings = test_mappings().expect("Unable to setup mappings!");
        let matcher = Matcher::new(enabled, None, None);
        assert!(!matcher.matchers.is_empty());

        if let Ok(request) = request_builder.body(()) {
            assert!(matcher.get_match(&request, &mappings).is_err());
        } else {
            assert!(false, "Unable to build the request to test!");
        }
    }

    #[test]
    #[allow(box_pointers)]
    fn load_test_files() {
        assert!(test_files().is_ok());
    }

    #[test]
    #[allow(box_pointers)]
    fn exact_match_header() {
        let mut request_builder = Request::builder();
        let _ = request_builder.header("X-Exact-Match", "header");

        check_request(
            Enabled::EXACT_HEADER,
            &mut request_builder,
            1,
            "Exact Match - Header",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn exact_match_headers() {
        let mut request_builder = Request::builder();
        let _ = request_builder.header("Content-Type", "application/json");
        let _ = request_builder.header("X-Exact-Headers", "true");

        check_request(
            Enabled::EXACT_HEADERS,
            &mut request_builder,
            1,
            "Exact Match - Headers",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn exact_match_method() {
        let mut request_builder = Request::builder();
        let _ = request_builder.method("PATCH");

        check_request(
            Enabled::EXACT_METHOD,
            &mut request_builder,
            1,
            "Exact Match - Method",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn exact_match_url() {
        let mut request_builder = Request::builder();
        let _ = request_builder.uri("/plaintext");

        check_request(
            Enabled::EXACT_URL,
            &mut request_builder,
            1,
            "Exact Match - URL",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn exact_match_method_and_url() {
        let mut request_builder = Request::builder();
        let _ = request_builder.uri("/json");
        let _ = request_builder.method("GET");

        check_request(
            Enabled::EXACT_URL | Enabled::EXACT_METHOD,
            &mut request_builder,
            2,
            "Exact Match - Method & URL",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn exact_match_method_url_and_header() {
        let mut request_builder = Request::builder();
        let _ = request_builder.uri("/header-method-url");
        let _ = request_builder.method("GET");
        let _ = request_builder.header("X-Exact-Match", "header-method-url");

        check_request(
            Enabled::EXACT_HEADER | Enabled::EXACT_METHOD | Enabled::EXACT_URL,
            &mut request_builder,
            3,
            "Exact Match - Header, Method, & URL",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn pattern_match_header() {
        let mut request_builder = Request::builder();
        let _ = request_builder.header("X-Pattern-Match", "yoda-darth");

        check_request(
            Enabled::PATTERN_HEADER,
            &mut request_builder,
            1,
            "Pattern Match - Header",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn pattern_match_headers() {
        let mut headers_request = Request::builder();
        let _ = headers_request.header("X-Correlation-Id", "12345");
        let _ = headers_request.header("X-Loyalty-Id", "abcd-1234");

        check_request(
            Enabled::PATTERN_HEADERS,
            &mut headers_request,
            2,
            "Pattern Match - Headers",
        );

        let mut long_corr_id = Request::builder();
        let _ = long_corr_id.header("X-Correlation-Id", "123456");
        let _ = long_corr_id.header("X-Loyalty-Id", "abcd-1234");

        check_no_match(Enabled::PATTERN_HEADERS, &mut long_corr_id);

        let mut short_corr_id = Request::builder();
        let _ = short_corr_id.header("X-Correlation-Id", "1234");
        let _ = short_corr_id.header("X-Loyalty-Id", "abcd-1234");

        check_no_match(Enabled::PATTERN_HEADERS, &mut short_corr_id);

        let mut invalid_loy_id = Request::builder();
        let _ = invalid_loy_id.header("X-Correlation-Id", "12345");
        let _ = invalid_loy_id.header("X-Loyalty-Id", "Abcd-1234");

        check_no_match(Enabled::PATTERN_HEADERS, &mut invalid_loy_id);
    }

    #[test]
    fn pattern_match_method() {
        let mut put_request = Request::builder();
        let _ = put_request.uri("/toodles");
        let _ = put_request.header("Content-Type", "application/json");
        let _ = put_request.method("PUT");

        check_request(
            Enabled::PATTERN_METHOD,
            &mut put_request,
            3,
            "Pattern Match - Method",
        );

        let mut post_request = Request::builder();
        let _ = post_request.uri("/poodles");
        let _ = post_request.header("Content-Type", "application/json");
        let _ = post_request.method("POST");

        check_request(
            Enabled::PATTERN_METHOD,
            &mut post_request,
            3,
            "Pattern Match - Method",
        );

        let mut patch_request = Request::builder();
        let _ = patch_request.uri("/noodles");
        let _ = patch_request.header("Content-Type", "application/json");
        let _ = patch_request.method("PATCH");

        check_request(
            Enabled::PATTERN_METHOD,
            &mut patch_request,
            3,
            "Pattern Match - Method",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn pattern_match_url() {
        let mut request_builder = Request::builder();
        let _ = request_builder.uri("/admin/list");

        check_request(
            Enabled::PATTERN_URL,
            &mut request_builder,
            4,
            "Pattern Match - URL",
        );
    }

    #[test]
    #[allow(box_pointers)]
    fn mixed_match_header() {
        let mut request_builder = Request::builder();
        let _ = request_builder.uri("/mixed-match");
        let _ = request_builder.header("X-Pattern-Match", "mixed-match");

        check_request(
            Enabled::EXACT_URL | Enabled::PATTERN_HEADER,
            &mut request_builder,
            2,
            "Mixed Match - Header & URL",
        );
    }
}