stubr 0.6.2

Wiremock implemented in Rust
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
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
use crate::model::response::{
    template::{
        data::{HandlebarsData, RequestData},
        verify::Predictable,
        HandlebarTemplatable,
    },
    ResponseStub,
};

use super::super::{StdResponse, Verifier};

pub struct TextBodyTemplatingVerifier {
    pub actual: String,
    pub expected: String,
}

impl Verifier<'_> for TextBodyTemplatingVerifier {
    fn verify(self, stub: &'_ ResponseStub, name: &'_ str, req: &'_ RequestData, _: &'_ mut StdResponse) {
        let data = HandlebarsData {
            request: req,
            response: Some(self.actual.as_bytes()),
            is_verify: true,
            stub_name: Some(name),
        };
        stub.body.register(&self.expected, &self.expected);
        let expected = stub.body.render(&self.expected, &data).unwrap_or_default();
        if self.expected.is_predictable() {
            assert_eq!(
                self.actual, expected,
                "\nVerification failed for stub '{}'. Expected response body to be '{}' but was '{}'",
                name, expected, self.actual
            );
        }
    }
}

#[cfg(test)]
mod text_body_templating_verify_tests {
    use http_types::{Request, Response};

    use crate::model::response::body::BodyStub;

    use super::*;

    mod from_req {
        use super::*;

        #[test]
        fn should_verify_text_body() {
            let actual = "/one/two/three".to_string();
            let expected = "{{request.path}}".to_string();
            let stub = ResponseStub {
                body: BodyStub {
                    body: Some(expected.clone()),
                    ..Default::default()
                },
                transformers: vec![String::from("response-template")],
                ..Default::default()
            };
            let mut req = Request::get(format!("http://localhost{}", &actual).as_str());
            let mut resp = Response::new(200);
            resp.set_body(actual.as_str());
            TextBodyTemplatingVerifier { actual, expected }.verify(&stub, "text", &RequestData::from(&mut req), &mut StdResponse(resp));
        }

        #[test]
        fn should_verify_text_body_when_many_templates() {
            let actual = "one-two-three".to_string();
            let expected = "{{request.pathSegments.[0]}}-{{request.pathSegments.[1]}}-{{request.pathSegments.[2]}}".to_string();
            let stub = ResponseStub {
                body: BodyStub {
                    body: Some(expected.clone()),
                    ..Default::default()
                },
                transformers: vec![String::from("response-template")],
                ..Default::default()
            };
            let mut req = Request::get("http://localhost/one/two/three");
            let mut resp = Response::new(200);
            resp.set_body(actual.as_str());
            TextBodyTemplatingVerifier { actual, expected }.verify(&stub, "text", &RequestData::from(&mut req), &mut StdResponse(resp));
        }

        #[should_panic(
            expected = "Verification failed for stub 'text'. Expected response body to be 'three-two-one' but was 'one-two-three'"
        )]
        #[test]
        fn verify_text_body_should_fail_when_not_eq() {
            let actual = "one-two-three".to_string();
            let expected = "{{request.pathSegments.[2]}}-{{request.pathSegments.[1]}}-{{request.pathSegments.[0]}}".to_string();
            let stub = ResponseStub {
                body: BodyStub {
                    body: Some(expected.clone()),
                    ..Default::default()
                },
                transformers: vec![String::from("response-template")],
                ..Default::default()
            };
            let mut req = Request::get("http://localhost/one/two/three");
            let mut resp = Response::new(200);
            resp.set_body(actual.as_str());
            TextBodyTemplatingVerifier { actual, expected }.verify(&stub, "text", &RequestData::from(&mut req), &mut StdResponse(resp));
        }
    }

    mod any {
        use super::*;

        #[should_panic(
            expected = "Cannot verify stub 'text' because response body '{{anyNonBlankString}}-{{anyNonEmptyString}}' is not verifiable"
        )]
        #[test]
        fn verify_text_body_when_many_rnd_template_should_not_verify_because_not_predictable() {
            verify("text", "anything", "{{anyNonBlankString}}-{{anyNonEmptyString}}")
        }
    }

    mod any_of {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("enum", "A", "{{anyOf 'A' 'B' 'C'}}");
            verify("enum", "B", "{{anyOf 'A' 'B' 'C'}}");
            verify("enum", "C", "{{anyOf 'A' 'B' 'C'}}");
        }

        #[should_panic(
            expected = "Verification failed for stub 'enum'. Expected response body to be one of [\"A\", \"B\", \"C\"] but was 'D'"
        )]
        #[test]
        fn verify_body_should_fail_when_regex_does_not_match() {
            verify("enum", "D", "{{anyOf 'A' 'B' 'C'}}");
        }

        #[should_panic(
            expected = "Verification failed for stub 'enum'. Expected response body to be one of [\"A\", \"B\", \"C\"] but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("enum", "", "{{anyOf 'A' 'B' 'C'}}")
        }
    }

    mod any_regex {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("regex", "FR", "{{anyRegex '[A-Z]{2}'}}")
        }

        #[should_panic(expected = "Verification failed for stub 'regex'. Expected response body to match '[A-Z]{2}' but was 'fr'")]
        #[test]
        fn verify_body_should_fail_when_regex_does_not_match() {
            verify("regex", "fr", "{{anyRegex '[A-Z]{2}'}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'regex'. Expected response body to match '[A-Z]{2}' but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("regex", "", "{{anyRegex '[A-Z]{2}'}}")
        }
    }

    mod any_uuid {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("uuid", "6a2f41a3-c54c-fce8-32d2-0324e1c32e22", "{{anyUuid}}")
        }

        #[should_panic(expected = "Verification failed for stub 'uuid'. Expected response body to be a valid uuid but was 'abcd'")]
        #[test]
        fn verify_body_should_fail_when_uuid_does_not_match() {
            verify("uuid", "abcd", "{{anyUuid}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'uuid'. Expected response body to be a valid uuid but was '6a2f41a3-c54c-fce8-32d2-0324e1c32e22-43a1'"
        )]
        #[test]
        fn verify_body_should_fail_when_uuid_longer() {
            verify("uuid", "6a2f41a3-c54c-fce8-32d2-0324e1c32e22-43a1", "{{anyUuid}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'uuid'. Expected response body to be a valid uuid but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("uuid", "", "{{anyUuid}}")
        }
    }

    mod any_email {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("email", "john.doe@gmail.com", "{{anyEmail}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'email'. Expected response body to be a valid email address but was 'john'"
        )]
        #[test]
        fn verify_body_should_fail_when_email_does_not_match() {
            verify("email", "john", "{{anyEmail}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'email'. Expected response body to be a valid email address but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("email", "", "{{anyEmail}}")
        }
    }

    mod any_hostname {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("host", "https://github.com", "{{anyHostname}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'host'. Expected response body to be a valid hostname but was 'github.com'"
        )]
        #[test]
        fn verify_body_should_fail_when_host_does_not_match() {
            verify("host", "github.com", "{{anyHostname}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'host'. Expected response body to be a valid hostname but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("host", "", "{{anyHostname}}")
        }
    }

    mod any_ip {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("ip", "127.0.0.1", "{{anyIpAddress}}")
        }

        #[should_panic(expected = "Verification failed for stub 'ip'. Expected response body to be a valid ip address but was '127.0.0'")]
        #[test]
        fn verify_body_should_fail_when_ip_does_not_match() {
            verify("ip", "127.0.0", "{{anyIpAddress}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'ip'. Expected response body to be a valid ip address but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("ip", "", "{{anyIpAddress}}")
        }
    }

    mod any_bool {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("bool", "true", "{{anyBoolean}}");
            verify("bool", "false", "{{anyBoolean}}");
        }

        #[should_panic(expected = "Verification failed for stub 'bool'. Expected response body to be a boolean but was 'either'")]
        #[test]
        fn verify_body_should_fail_when_bool_does_not_match() {
            verify("bool", "either", "{{anyBoolean}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'bool'. Expected response body to be a boolean but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("bool", "", "{{anyBoolean}}")
        }
    }

    mod any_date {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("date", "2022-04-13", "{{anyDate}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'date'. Expected response body to be a valid date (yyyy-mm-dd) but was '2022/04/13'"
        )]
        #[test]
        fn verify_body_should_fail_when_date_does_not_match() {
            verify("date", "2022/04/13", "{{anyDate}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'date'. Expected response body to be a valid date (yyyy-mm-dd) but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("date", "", "{{anyDate}}")
        }
    }

    mod any_time {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("time", "23:59:59", "{{anyTime}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'time'. Expected response body to be a valid time (hh:mm:ss) but was '24:59:59'"
        )]
        #[test]
        fn verify_body_should_fail_when_time_does_not_match() {
            verify("time", "24:59:59", "{{anyTime}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'time'. Expected response body to be a valid time (hh:mm:ss) but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("time", "", "{{anyTime}}")
        }
    }

    mod any_datetime {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("datetime", "2022-04-13T23:59:59", "{{anyDatetime}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'datetime'. Expected response body to be a valid datetime (yyyy-mm-ddThh:mm:ss) but was '2022/04/13T24:59:59'"
        )]
        #[test]
        fn verify_body_should_fail_when_datetime_does_not_match() {
            verify("datetime", "2022/04/13T24:59:59", "{{anyDatetime}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'datetime'. Expected response body to be a valid datetime (yyyy-mm-ddThh:mm:ss) but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("datetime", "", "{{anyDatetime}}")
        }
    }

    mod any_iso_8601_datetime {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("iso-8601-datetime", "2022-04-13T23:59:59Z", "{{anyIso8601}}");
            verify("iso-8601-datetime", "2022-04-13T23:59:59+01:00", "{{anyIso8601}}");
        }

        #[should_panic(
            expected = "Verification failed for stub 'iso-8601-datetime'. Expected response body to be a valid iso 8601 datetime (yyyy-mm-ddThh:mm:ss) but was '2022/04/13T24:59:59'"
        )]
        #[test]
        fn verify_body_should_fail_when_iso_8601_datetime_does_not_match() {
            verify("iso-8601-datetime", "2022/04/13T24:59:59", "{{anyIso8601}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'iso-8601-datetime'. Expected response body to be a valid iso 8601 datetime (yyyy-mm-ddThh:mm:ss) but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("iso-8601-datetime", "", "{{anyIso8601}}")
        }
    }

    mod any_non_blank_string {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("blank", "azerty", "{{anyNonBlankString}}")
        }

        #[should_panic(expected = "Verification failed for stub 'blank'. Expected response body to be a non blank string but was ' '")]
        #[test]
        fn verify_body_should_fail_when_body_contains_space() {
            verify("blank", " ", "{{anyNonBlankString}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'blank'. Expected response body to be a non blank string but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("blank", "", "{{anyNonBlankString}}")
        }
    }

    mod any_non_empty_string {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("empty", "azerty", "{{anyNonEmptyString}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'empty'. Expected response body to be a non empty string but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_empty() {
            verify("empty", "", "{{anyNonEmptyString}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'empty'. Expected response body to be a non empty string but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("empty", "", "{{anyNonEmptyString}}")
        }
    }

    mod any_alpha_numeric {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("alpha-num", "abcd1234ABCD", "{{anyAlphaNumeric}}")
        }

        #[should_panic(expected = "Verification failed for stub 'alpha-num'. Expected response body to be an alphanumeric but was '!?'")]
        #[test]
        fn verify_body_should_fail_when_not_alpha_numeric() {
            verify("alpha-num", "!?", "{{anyAlphaNumeric}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'alpha-num'. Expected response body to be an alphanumeric but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("alpha-num", "", "{{anyAlphaNumeric}}")
        }
    }

    mod any_number {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("number", "42", "{{anyNumber}}");
            verify("number", "42.3", "{{anyNumber}}");
        }

        #[should_panic(expected = "Verification failed for stub 'number'. Expected response body to be a number but was 'abcd'")]
        #[test]
        fn verify_body_should_fail_when_not_number() {
            verify("number", "abcd", "{{anyNumber}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'number'. Expected response body to be a number but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("number", "", "{{anyNumber}}")
        }
    }

    mod any_integer {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("integer", "42", "{{anyI64}}");
        }

        #[should_panic(expected = "Verification failed for stub 'integer'. Expected response body to be an i64 but was 'abcd'")]
        #[test]
        fn verify_body_should_fail_when_not_integer() {
            verify("integer", "abcd", "{{anyI64}}")
        }

        #[should_panic(expected = "Verification failed for stub 'integer'. Expected response body to be an i64 but was '42.3'")]
        #[test]
        fn verify_body_should_fail_when_float() {
            verify("integer", "42.3", "{{anyI64}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'integer'. Expected response body to be an i64 but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("integer", "", "{{anyI64}}")
        }
    }

    mod any_float {
        use super::*;

        #[test]
        fn should_verify_body() {
            verify("float", "42.3", "{{anyFloat}}");
        }

        #[should_panic(expected = "Verification failed for stub 'float'. Expected response body to be a float but was 'abcd'")]
        #[test]
        fn verify_body_should_fail_when_not_float() {
            verify("float", "abcd", "{{anyFloat}}")
        }

        #[should_panic(expected = "Verification failed for stub 'float'. Expected response body to be a float but was '42'")]
        #[test]
        fn verify_body_should_fail_when_integer() {
            verify("float", "42", "{{anyFloat}}")
        }

        #[should_panic(
            expected = "Verification failed for stub 'float'. Expected response body to be a float but no response body was present"
        )]
        #[test]
        fn verify_body_should_fail_when_body_absent() {
            verify("float", "", "{{anyFloat}}")
        }
    }

    fn verify(name: &str, actual: &str, expected: &str) {
        let stub = ResponseStub {
            body: BodyStub {
                body: Some(expected.to_string()),
                ..Default::default()
            },
            transformers: vec![String::from("response-template")],
            ..Default::default()
        };
        let mut resp = Response::new(200);
        resp.set_body(actual);
        TextBodyTemplatingVerifier {
            actual: actual.to_string(),
            expected: expected.to_string(),
        }
        .verify(
            &stub,
            name,
            &RequestData::from(&mut Request::get("http://localhost")),
            &mut StdResponse(resp),
        );
    }
}