noir 0.2.0

rust based, DSL alike and request driven, black box testing library for HTTP APIs.
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
// Copyright (c) 2016 Ivo Wetzel

// 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. This file may not be copied, modified, or distributed
// except according to those terms.


// STD Dependencies -----------------------------------------------------------
use std::str;


// External Dependencies ------------------------------------------------------
use colored::*;


// Internal Dependencies ------------------------------------------------------
use util;
use resource::http::HttpFormDataField;


// Deep Form Compare ----------------------------------------------------------
pub fn compare(
    expected: &[HttpFormDataField],
    actual: &[HttpFormDataField],
    check_additional_keys: bool

) -> Result<(), Vec<(Vec<String>, String)>> {

    let errors = compare_form(
        expected,
        actual,
        check_additional_keys,
        vec![]
    );

    if errors.is_empty() {
        Ok(())

    } else {
        Err(errors)
    }

}

pub fn format(errors: Vec<(Vec<String>, String)>) -> String {
    errors.into_iter().map(|(path, message)| {
        format!("- {}{}: {}", "form".blue().bold(), path.join(""), message)

    }).collect::<Vec<String>>().join("\n\n        ")
}


// Recursive Compare Function -------------------------------------------------
fn compare_form(
    a: &[HttpFormDataField],
    b: &[HttpFormDataField],
    check_additional_keys: bool,
    path: Vec<String>

) -> Vec<(Vec<String>, String)> {

    if a.is_empty() && b.is_empty() {
        return vec![];
    }

    let mut a_fields = map_form_fields(a);
    let mut b_fields = map_form_fields(b);

    a_fields.sort_by(|a, b| a.0.cmp(b.0));
    b_fields.sort_by(|a, b| a.0.cmp(b.0));

    let mut missing_fields = Vec::new();
    let mut errors = vec![];

    for (a_name, a_type, a_field) in a_fields {

        let mut found = false;
        b_fields.retain(|&(ref b_name, b_type, ref b_field)| {
            if a_name == *b_name {

                let mut field_path = path.clone();
                field_path.push(format!(".{}", a_name.blue().bold()));
                errors.append(&mut compare_form_fields(
                    a_type,
                    b_type,
                    a_field,
                    b_field,
                    field_path,
                    "Field"
                ));

                found = true;
                false

            } else {
                true
            }
        });

        if !found {
            missing_fields.push((a_name, a_type, a_field));
        }

    }

    missing_field_errors(FormFieldType::Field, &mut errors, &missing_fields);
    missing_field_errors(FormFieldType::Array, &mut errors, &missing_fields);
    missing_field_errors(FormFieldType::File, &mut errors, &missing_fields);

    if check_additional_keys {
        additional_field_errors(FormFieldType::Field, &mut errors, &b_fields);
        additional_field_errors(FormFieldType::Array, &mut errors, &b_fields);
        additional_field_errors(FormFieldType::File, &mut errors, &b_fields);
    }

    errors

}


// Form Field Comparision -----------------------------------------------------
fn compare_form_fields(
    type_a: FormFieldType,
    type_b: FormFieldType,
    a: &HttpFormDataField,
    b: &HttpFormDataField,
    path: Vec<String>,
    name: &'static str

) -> Vec<(Vec<String>, String)> {

    if type_a == type_b {

        let mut errors = vec![];
        match *a {

            HttpFormDataField::Field(_, ref value_a) => {
                if let HttpFormDataField::Field(_, ref value_b) = *b {
                    if value_a != value_b {
                        let (expected, actual, diff) = util::diff::text(value_b, value_a);
                        errors.push((
                            path.clone(),
                            format!(
                                "{} {}\n\n              \"{}\"\n\n          {}\n\n              \"{}\"\n\n          {}\n\n              \"{}\"",
                                name.green().bold(),
                                "value does not match, expected:".yellow(),
                                expected.green().bold(),
                                "but got:".yellow(),
                                actual.red().bold(),
                                "difference:".yellow(),
                                diff
                            )
                        ))
                    }
                }
            },

            HttpFormDataField::Array(_, ref value_a) => {
                if let HttpFormDataField::Array(_, ref value_b) = *b {

                    let len_a = value_a.len();
                    let len_b = value_b.len();

                    if len_a != len_b {
                        errors.push((
                            path.clone(),
                            format!(
                                "{} {} {} {} {}",
                                "Array".green().bold(),
                                "with".yellow(),
                                format!("{}", len_b).red().bold(),
                                "item(s) does not match expected length of".yellow(),
                                format!("{}", len_a).green().bold()
                            )
                        ));
                    }

                    for (index, (a_item, b_item)) in value_a.iter().zip(value_b).enumerate() {

                        let mut item_path = path.clone();
                        item_path.push(format!("[{}]", index).purple().bold().to_string());

                        let a = HttpFormDataField::Field("".to_string(), a_item.to_string());
                        let b = HttpFormDataField::Field("".to_string(), b_item.to_string());

                        errors.append(&mut compare_form_fields(
                            FormFieldType::Field,
                            FormFieldType::Field,
                            &a,
                            &b,
                            item_path,
                            "ArrayItem"
                        ));

                    }

                }
            },

            HttpFormDataField::FileVec(_, ref filename_a, ref mime_a, _) => {
                if let HttpFormDataField::FileVec(_, ref filename_b, ref mime_b, _) = *b {

                    if filename_a != filename_b {
                        errors.push((
                            path.clone(),
                            format!(
                                "{} (\"{}\") {} (\"{}\")",
                                "Filename".green().bold(),
                                filename_b.red().bold(),
                                "does not match expected value".yellow(),
                                filename_a.green().bold()
                            )
                        ))
                    }

                    if mime_a != mime_b {
                        errors.push((
                            path.clone(),
                            format!(
                                "{} ({}) {} ({})",
                                "Mimetype".green().bold(),
                                format!("{}", mime_b).red().bold(),
                                "does not match expected value".yellow(),
                                format!("{}", mime_a).green().bold()
                            )
                        ))
                    }

                    // TODO IW: Compare file content based on mime type

                }
            },
            _ => unreachable!()
        }

        errors

    } else {
        vec![(
            path,
            format!(
                "{} {} {} {}",
                "Expected a".yellow(),
                type_a.as_str().green().bold(),
                "but found a".yellow(),
                type_b.as_str().red().bold()
            )
        )]
    }

}


// Helpers --------------------------------------------------------------------
fn missing_field_errors(
    typ: FormFieldType,
    errors: &mut Vec<(Vec<String>, String)>,
    fields: &[(&String, FormFieldType, &HttpFormDataField)]
) {

    let fields = fields.iter().filter(|v| v.1 == typ).collect::<Vec<&(&String, FormFieldType, &HttpFormDataField)>>();
    if !fields.is_empty() {
        errors.push((
            vec![],
            format!(
                "{} {} {} ({})",
                "Is missing".yellow(),
                format!("{}", fields.len()).red().bold(),
                format!("{}(s)", typ.as_str()).yellow(),
                fields.iter().map(|e| {
                    e.0.as_str().red().bold().to_string()

                }).collect::<Vec<String>>().join(", ")
            )
        ));
    }

}

fn additional_field_errors(
    typ: FormFieldType,
    errors: &mut Vec<(Vec<String>, String)>,
    fields: &[(&String, FormFieldType, &HttpFormDataField)]
) {

    let fields = fields.iter().filter(|v| v.1 == typ).collect::<Vec<&(&String, FormFieldType, &HttpFormDataField)>>();
    if !fields.is_empty() {
        errors.push((
            vec![],
            format!(
                "{} {} {} ({})",
                "Has".yellow(),
                format!("{}", fields.len()).red().bold(),
                format!("additional unexpected {}(s)", typ.as_str()).yellow(),
                fields.iter().map(|e| {
                    e.0.as_str().red().bold().to_string()

                }).collect::<Vec<String>>().join(", ")
            )
        ));
    }

}

fn map_form_fields(
    fields: &[HttpFormDataField]

) -> Vec<(&String, FormFieldType, &HttpFormDataField)> {
    fields.iter().map(|field| {
        match *field {
            HttpFormDataField::Field(ref name, _) => {
                (name, FormFieldType::Field, field)
            },
            HttpFormDataField::Array(ref name, _) => {
                (name, FormFieldType::Array, field)
            },
            HttpFormDataField::FileVec(ref name, _, _, _) |
            HttpFormDataField::FileFs(ref name, _, _, _) => {
                (name, FormFieldType::File, field)
            }
        }

    }).collect()
}

#[derive(Copy, Clone, PartialEq)]
enum FormFieldType {
    Field,
    Array,
    File
}

impl FormFieldType {
    fn as_str(&self) -> &'static str {
        match *self {
            FormFieldType::Field => "plain field",
            FormFieldType::Array => "array",
            FormFieldType::File => "file attachment"
        }
    }
}


// Tests ----------------------------------------------------------------------
#[cfg(test)]
mod tests {

    use hyper::mime::{Mime, TopLevel, SubLevel};
    use resource::http::HttpFormDataField;
    use super::compare;

    macro_rules! form {
        {} => (vec![]);

        { $( $key:expr => $value:expr ),* } => ({

            let mut fields = Vec::new();

            $(
                fields.push(($key, $value).into());
            )*

            fields
        })
    }

    fn uncolor(mut text: String) -> String {
        text = text.replace("\u{1b}", "");
        text = text.replace("[1;31m", "<br>");
        text = text.replace("[1;32m", "<bg>");
        text = text.replace("[33m", "<by>");
        text = text.replace("[1;34m", "<bb>");
        text = text.replace("[1;35m", "<bp>");
        text = text.replace("[36m", "<bn>");
        text = text.replace("[1;36m", "<bc>");
        text = text.replace("[1;42;37m", "<gbg>");
        text = text.replace("[1;41;37m", "<gbr>");
        text.replace("[0m", "")
    }

    fn cmp(
        expected: Vec<HttpFormDataField>,
        actual: Vec<HttpFormDataField>,
        errors: Vec<(Vec<&str>, &str)>
    ) {
        cmp_base(expected, actual, errors, false);
    }

    fn cmp_base(
        expected: Vec<HttpFormDataField>,
        actual: Vec<HttpFormDataField>,
        errors: Vec<(Vec<&str>, &str)>,
        add: bool
    ) {
        match compare(&expected, &actual, add) {
            Ok(()) => {
                assert!(errors.is_empty());
            },
            Err(e) => {
                let formatted = e.into_iter().map(|e| {
                    (
                        e.0.into_iter().map(|p| uncolor(p)).collect::<Vec<String>>(),
                        uncolor(e.1)
                    )

                }).collect::<Vec<(Vec<String>, String)>>();

                let err = formatted.iter().map(|e| {
                    (
                        e.0.iter().map(|p| p.as_str()).collect::<Vec<&str>>(),
                        e.1.as_str()
                    )

                }).collect::<Vec<(Vec<&str>, &str)>>();

                assert_eq!(err, errors);

            }
        }
    }

    #[test]
    fn test_compare_empty() {
        cmp(form!{}, form!{}, vec![]);
    }

    #[test]
    fn test_compare_fields() {

        cmp(form!{ "field" => "value" }, form!{ "field" => "value" }, vec![
        ]);

        cmp(form!{ "field" => "value" }, form!{ "field" => "other value" }, vec![
            (vec![".<bb>field"], "<bg>Field <by>value does not match, expected:\n\n              \"<bg>other value\"\n\n          <by>but got:\n\n              \"<br>value\"\n\n          <by>difference:\n\n              \"<gbr>other value\"")
        ]);

        cmp(form!{ "field" => "value" }, form!{ "field" => vec!["1"] }, vec![
            (vec![".<bb>field"], "<by>Expected a <bg>plain field <by>but found a <br>array")
        ]);

        cmp(form!{ "field" => "value" }, form!{ "field" => (
            "filename",
            Mime(TopLevel::Text, SubLevel::Plain, vec![]),
            "Data"

        )}, vec![
            (vec![".<bb>field"], "<by>Expected a <bg>plain field <by>but found a <br>file attachment")
        ]);

    }

    #[test]
    fn test_compare_arrays() {

        cmp(form!{ "array[]" => vec!["item"] }, form!{ "array[]" => vec!["item"] }, vec![
        ]);

        cmp(form!{ "array[]" => vec!["item"] }, form!{ "array[]" => "value" }, vec![
            (vec![".<bb>array[]"], "<by>Expected a <bg>array <by>but found a <br>plain field")
        ]);

        cmp(form!{ "array[]" => vec!["item"] }, form!{ "array[]" => (
            "filename",
            Mime(TopLevel::Text, SubLevel::Plain, vec![]),
            "Data"

        )}, vec![
            (vec![".<bb>array[]"], "<by>Expected a <bg>array <by>but found a <br>file attachment")
        ]);

        cmp(form!{ "array[]" => vec!["item"] }, form!{ "array[]" => vec!["item", "item"] }, vec![
            (vec![".<bb>array[]"], "<bg>Array <by>with <br>2 <by>item(s) does not match expected length of <bg>1")
        ]);

        cmp(form!{ "array[]" => vec!["item", "item"] }, form!{ "array[]" => vec!["item"] }, vec![
            (vec![".<bb>array[]"], "<bg>Array <by>with <br>1 <by>item(s) does not match expected length of <bg>2")
        ]);

        cmp(form!{ "array[]" => vec!["item"] }, form!{ "array[]" => vec!["other item"] }, vec![
            (vec![".<bb>array[]", "<bp>[0]"], "<bg>ArrayItem <by>value does not match, expected:\n\n              \"<bg>other item\"\n\n          <by>but got:\n\n              \"<br>item\"\n\n          <by>difference:\n\n              \"<gbr>other item\"")
        ]);

    }

    #[test]
    fn test_compare_missing() {
        cmp(form!{ "field" => "value", "field2" => "value" }, form!{  }, vec![
            (vec![], "<by>Is missing <br>2 <by>plain field(s) (<br>field, <br>field2)")
        ]);
    }

    #[test]
    fn test_compare_missing_ignore_additional() {
        cmp(form!{}, form!{ "otherField" => "value", "otherField2" => "value"}, vec![
        ]);
    }

    #[test]
    fn test_compare_additional_check_additional() {
        cmp_base(form!{  }, form!{ "otherField" => "value", "otherField2" => "value"}, vec![
            (vec![], "<by>Has <br>2 <by>additional unexpected plain field(s) (<br>otherField, <br>otherField2)")

        ], true);
    }

    #[test]
    fn test_compare_files() {

        cmp(form!{
            "file" => (
                "filename",
                Mime(TopLevel::Text, SubLevel::Plain, vec![]),
                "Data"
            )

        }, form!{
            "file" => (
                "filename",
                Mime(TopLevel::Text, SubLevel::Plain, vec![]),
                "Data"
            )

        }, vec![]);

        cmp(form!{
            "file" => (
                "filename",
                Mime(TopLevel::Text, SubLevel::Plain, vec![]),
                "Data"
            )

        }, form!{
            "file" => (
                "otherFilename",
                Mime(TopLevel::Text, SubLevel::Plain, vec![]),
                "Data"
            )

        }, vec![
            (vec![".<bb>file"], "<bg>Filename (\"<br>otherFilename\") <by>does not match expected value (\"<bg>filename\")")
        ]);

        cmp(form!{
            "file" => (
                "filename",
                Mime(TopLevel::Text, SubLevel::Plain, vec![]),
                "Data"
            )

        }, form!{
            "file" => (
                "filename",
                Mime(TopLevel::Text, SubLevel::Html, vec![]),
                "Data"
            )

        }, vec![
            (vec![".<bb>file"], "<bg>Mimetype (<br>text/html) <by>does not match expected value (<bg>text/plain)")
        ]);

    }

}