texting_robots 0.2.2

Texting Robots: A Rust native `robots.txt` parser with thorough unit testing.
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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
use super::{robots_txt_parse, Error, Robot};

use super::Line;
use super::Line::*;

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_parser_line_elements() {
        let txt = "User-Agent: SmerBot
Disallow: /path
Allow:    /path/exception   # ONLY THIS IS ALLOWED
Crawl-delay : 60 # Very slow delay

sitemap: https://example.com/sitemap.xml";

        let lines = robots_txt_parse(txt.as_bytes()).unwrap().1;

        let result: Vec<Line> = vec![
            UserAgent(b"SmerBot"),
            Disallow(b"/path"),
            Allow(b"/path/exception"),
            CrawlDelay(Some(60.0)),
            Raw(b""),
            Sitemap(b"https://example.com/sitemap.xml"),
        ];

        assert_eq!(lines, result);
    }

    #[test]
    fn test_parser_crawl_delay() {
        // Test correct retrieval
        let good_text = "    crawl-delay  : 60";
        match robots_txt_parse(good_text.as_bytes()) {
            Ok((_, lines)) => {
                assert_eq!(lines.len(), 1);
                assert_eq!(lines[0], CrawlDelay(Some(60.0)));
            }
            Err(_) => panic!("Crawl-Delay not correctly retrieved"),
        };
        // Test float (good)
        let good_text = "    crawl-delay  : 3.14";
        match robots_txt_parse(good_text.as_bytes()) {
            Ok((_, lines)) => {
                assert_eq!(lines.len(), 1);
                assert_eq!(lines[0], CrawlDelay(Some(3.14)));
            }
            Err(_) => panic!("Crawl-Delay not correctly retrieved"),
        };
        // Test float (good)
        let good_text = "    crawl-delay  : 0.0";
        match robots_txt_parse(good_text.as_bytes()) {
            Ok((_, lines)) => {
                assert_eq!(lines.len(), 1);
                assert_eq!(lines[0], CrawlDelay(Some(0.0)));
            }
            Err(_) => panic!("Crawl-Delay not correctly retrieved"),
        };
        // Test float (bad)
        let bad_text = "    crawl-delay  : -1.618";
        match robots_txt_parse(bad_text.as_bytes()) {
            Ok((_, lines)) => {
                assert_eq!(lines.len(), 1);
                assert!(!matches!(lines[0], CrawlDelay(_)));
            }
            Err(_) => panic!("Crawl-Delay not correctly retrieved"),
        };
        // Test invalid result
        let bad_text = "Crawl-delay: wait";
        let r = robots_txt_parse(bad_text.as_bytes());
        if let Ok((_, lines)) = &r {
            assert_eq!(lines.len(), 1);
            if let Raw(_) = lines[0] {
            } else {
                panic!("Invalid Crawl-Delay not correctly handled")
            }
        }
    }

    #[test]
    fn test_robot_all_user_agents() {
        let txt = "User-agent: *
        User-agent: BobBot
        User-AGENT: SmerBot";
        let r = Robot::new("SmerBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("/index.html"));
    }

    #[test]
    fn test_robot_retrieve_crawl_delay() {
        let txt = "User-Agent: A
        Crawl-Delay: 42
        # A B and the other Agent ...
        User-Agent: B
        User-Agent: C
        Crawl-Delay: 420
        User-Agent: D
        Crawl-Delay: -1.25
        User-Agent: E
        Crawl-Delay: 8
        User-Agent: *
        CRAWL-Delay : 3600
        User-Agent: Zero
        Crawl-Delay: 0";

        let r = Robot::new("A", txt.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(42.0));
        let r = Robot::new("B", txt.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(420.0));
        let r = Robot::new("C", txt.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(420.0));
        let r = Robot::new("D", txt.as_bytes()).unwrap();
        // Note: D ends up with 8 as it falls through to E's value
        // I'm not in love with this but it's in line with the spec ...
        // It's the same as what occurs with the comment between A/B/C above
        assert_eq!(r.delay, Some(8.0));
        let r = Robot::new("Zero", txt.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(0.0));
    }

    #[test]
    fn test_robot_crawl_delay_not_integer() {
        let txt = b"User-Agent: A
        Crawl-Delay:1.0
        User-Agent: B
        Crawl-Delay:4.2
        User-Agent: C
        Crawl-Delay: \x41\xc2\xc3\xb1\x42";

        let r = Robot::new("A", txt).unwrap();
        assert_eq!(r.delay, Some(1.0));
        // We assume the (not well specified) Crawl-Delay only allows integers
        // Converting floats is both complicated and not at all well defined
        let r = Robot::new("B", txt).unwrap();
        assert_eq!(r.delay, Some(4.2));
        let r = Robot::new("C", txt).unwrap();
        assert_eq!(r.delay, None);
    }

    #[test]
    fn test_robot_crawl_evil_utf8() {
        // Example of ill-formed UTF-8 code unit sequence from:
        // http://www.unicode.org/versions/Unicode6.2.0/ch03.pdf
        let txt = b"User-Agent: A
        Allow: \x41\xc2\xc3\xb1\x42
        Disallow: \x41\xc2\xc3\xb1\x42
        SiteMap: \x41\xc2\xc3\xb1\x42
        Crawl-Delay: \x41\xc2\xc3\xb1\x42
        Disallow: /bob/";

        let r = Robot::new("A", txt).unwrap();
        assert!(!r.allowed("/bob/"));
        assert_eq!(r.delay, None);
        assert!(r.sitemaps.is_empty());
    }

    #[test]
    fn test_robot_retrieve_sitemaps() {
        let txt = "user-agent: otherbot
        disallow: /kale

        sitemap: https://example.com/sitemap.xml
        Sitemap: https://cdn.example.org/other-sitemap.xml
        siteMAP: https://ja.example.org/ใƒ†ใ‚นใƒˆ-ใ‚ตใ‚คใƒˆใƒžใƒƒใƒ—.xml";
        let sitemaps = vec![
            "https://example.com/sitemap.xml",
            "https://cdn.example.org/other-sitemap.xml",
            "https://ja.example.org/ใƒ†ใ‚นใƒˆ-ใ‚ตใ‚คใƒˆใƒžใƒƒใƒ—.xml",
        ];

        let r = Robot::new("otherbot", txt.as_bytes()).unwrap();
        assert_eq!(r.sitemaps, sitemaps);
        let r = Robot::new("blah", txt.as_bytes()).unwrap();
        assert_eq!(r.sitemaps, sitemaps);
    }

    #[test]
    fn test_robot_excessive_crawl_delay() {
        let txt = "User-Agent: Y
        Crawl-Delay: 115792089237316195423570985008687907853269984665640564039457584007913129639936";
        let r = Robot::new("Y", txt.as_bytes()).unwrap();
        // In the past this was none as the crawl delay overflow integer
        // but since we've moved to floating point it's complicated ...
        assert!(r.delay.unwrap() > 3e38);
    }

    #[test]
    fn test_robot_starts_with_crawl_delay() {
        // Some domains, such as https://www.ipwatchdog.com/robots.txt, start with a
        // Crawl-Delay directive that applies to all User-Agents.
        // We assume if your Agent doesn't have a specific Crawl-Delay then this applies.
        let txt = "Crawl-Delay: 42
        User-Agent: *
        Disallow: /blah
        User-Agent: SpecialFriend
        Allow: /
        Crawl-Delay: 1";

        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(42.0));
        let r = Robot::new("SpecialFriend", txt.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(1.0));
    }

    #[test]
    fn test_robot_handles_random_nulls() {
        let txt = "User-Agent: *
        \x00\x00Allow: /family\x00\x00
        Disallow: /family/photos\x00\x00\x00
        Crawl-Delay: 42";

        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("/family"));
        assert!(!r.allowed("/family/photos"));
        assert_eq!(r.delay, Some(42.0));
    }

    #[test]
    fn test_robot_doesnt_do_full_regex() {
        // This is added purely as paranoia after seeing so many full regular
        // expressions written in robots.txt files!
        // This is also a good sanity test to ensure we always escape the rule
        let pat = "/(Cat|Dog).html";
        let target = "/Cat.html";
        assert!(regex::Regex::new(pat).unwrap().is_match(target));

        let txt = "User-Agent: *
        Disallow: /
        Allow: /(Cat|Dog).html";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(r.allowed(pat));
        assert!(!r.allowed(target));
    }

    #[test]
    fn test_robot_errors_on_crazy_long_line() {
        let mut txt = b"Disallow: /".to_vec();
        let ending = b"AAAAAAAAAA".to_vec();
        // 10 bytes * 100_000 = 1MB
        for _ in 0..100_000 {
            txt.extend(&ending);
        }
        // A Disallow followed by a megabyte of "A" was a real world adversarial example
        let result = Robot::new("BobBot", &txt);
        let _expected = anyhow::Error::new(Error::InvalidRobots);
        assert!(matches!(result, _expected));
    }

    #[test]
    fn test_robot_handles_end_properly() {
        let txt = "User-Agent: *
        Disallow: /
        Disallow: /*/about
        Allow: /about$";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("https://quora.com/about"));
        assert!(!r.allowed("/about/"));
    }

    #[test]
    fn test_robot_debug_format() {
        let txt = "User-Agent: A
        Allow: /allow/
        Disallow: /disallow/
        Crawl-Delay: 42
        SiteMap: https://example.com/sitemap.xml";

        let r = Robot::new("A", txt.as_bytes()).unwrap();
        let s = format!("{:?}", r);
        // This isn't particularly complex but a reasonable sanity test
        // The majority of the properties of the robots.txt file should be presented
        assert!(s.contains("/allow/"));
        assert!(s.contains("/disallow/"));
        assert!(s.contains("Some(42.0)"));
        assert!(s.contains("https://example.com/sitemap.xml"));
    }

    /// From Common Crawl burn test
    //

    #[test]
    fn test_robot_handle_double_return_then_newline() {
        let txt = b"\r
        User-agent: *\r\r
        Disallow: /en-AU/party\r\r\r\n\n\r\n
        User-Agent: BobBot
        Disallow: /fi-FI/party\r\r\n
        Disallow: /en-US/party\r\r\n
        \r\n\r\r\r\n\n
        Crawl-Delay: 4";

        let r = Robot::new("RandomBot", txt).unwrap();
        assert!(!r.allowed("/en-AU/party"));

        let r = Robot::new("BobBot", txt).unwrap();
        assert_eq!(r.delay, Some(4.0));
        assert!(r.allowed("/en-AU/party"));
        assert!(!r.allowed("/fi-FI/party"));
        assert!(!r.allowed("/en-US/party"));
    }

    #[test]
    fn test_robot_crazy_long_regex() {
        // Inspired by https://www.diecastlegends.com/robots.txt
        // The only sane reason that a million stars in a row make sense
        let txt = "User-agent: *
        Disallow: /basket*
        # Longest string takes priority. This is necessary due to conflicting Allow rules:
        Disallow: /*?************************************************************************************donotindex=1*";

        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/basket"));
        assert!(!r.allowed("/basket/ball"));
        assert!(r.allowed("/example/file?xyz=42"));
        assert!(!r.allowed("/example/file?xyz=42&donotindex=1"));
    }

    #[test]
    fn test_robot_many_star_rule_simplifier() {
        let txt = "Disallow: /x***y/";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/x/y/"));
        assert_eq!(r.rules.len(), 1);
        let (rule, _) = &r.rules[0];
        assert_eq!(rule.as_str(), "/x*y/");
    }

    #[test]
    fn test_robot_starts_with_wildcard() {
        let txt = "Disallow: *";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/"));
        assert!(!r.allowed("/a"));

        let txt = "Allow: *
        Disallow: *y
        Disallow: */a/*.html";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("/"));
        assert!(r.allowed("/b"));
        assert!(!r.allowed("bob/a/home.html"));
        assert!(!r.allowed("/gray"));
    }

    #[test]
    fn test_robot_handles_starting_position() {
        let txt = "User-agent: *
        Allow: /ocean
        Disallow: /tooth$
        Disallow: /fish*$";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("/ocean"));
        assert!(!r.allowed("/fish"));
        assert!(r.allowed("/shark/tooth"));
        assert!(!r.allowed("/tooth"));
        assert!(r.allowed("/toothy"));
        // Without proper starting position handling this will match the /fish rule
        assert!(r.allowed("/shark/fish"));
        assert!(!r.allowed("/fish/fins"));
        assert!(!r.allowed("/fish"));
        assert!(!r.allowed("/fishy"));
    }

    /// From fuzzer
    //

    #[test]
    fn test_fuzzed_long_regex_rule() {
        let statements: Vec<&str> = vec!["Allow:*", "Disallow:*"];
        // Note: We don't do this for Sitemap / User-Agent / Crawl-Delay
        // For the first two it'd be an allowed input and the latter is ignored
        for statement in statements {
            let mut crash: Vec<u8> =
                [statement.as_bytes(), &vec!['A' as u8; 4096]].concat();
            // Add wildcards (*) and an end match ($) to trigger full regex mode
            // Compilation doesn't fail when using the two shortcut modes
            crash.extend(b"*$");
            crash[10] = '*' as u8;
            crash[30] = '*' as u8;
            let r = Robot::new("BobBot", &crash);
            assert!(r.is_err());
        }
    }

    /// URL Tests
    ////////////////////////////////////////////////////////////////////////////////

    #[test]
    fn test_url_prepare_relative() {
        for (url, path) in vec![
            ("https://example.com/foo/bar/baz.html", "/foo/bar/baz.html"),
            ("https://example.com/", "/"),
            ("https://example.com/path", "/path"),
            ("https://example.com/path?q=Linux", "/path?q=Linux"),
        ] {
            assert_eq!(Robot::prepare_url(url), path);
            assert_eq!(Robot::prepare_url(path), path);
        }
    }

    /// REPPY TESTS
    ////////////////////////////////////////////////////////////////////////////////

    // From https://github.com/seomoz/rep-cpp/issues/34
    #[test]
    fn test_reppy_handles_leading_wildcard() {
        let txt = "User-agent: *
        Disallow: */test";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/test"));
        assert!(!r.allowed("/test/"));
        assert!(!r.allowed("/foo/test"));
        assert!(r.allowed("/foo"));
    }

    #[test]
    fn test_reppy_no_leading_user_agent() {
        let txt = "Disallow: /path
        Allow: /path/exception
        Crawl-delay: 7";

        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert!(r.allowed("/path/exception"));
        assert!(!r.allowed("/path"));
        assert!(r.allowed("/"));
        assert_eq!(r.delay, Some(7.0));
    }

    #[test]
    fn test_reppy_honours_default() {
        let txt = "User-agent: *
        Disallow: /tmp

        User-agent: other-agent
        Allow: /tmp";
        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/tmp"));
        assert!(r.allowed("/path"));
    }

    #[test]
    fn test_reppy_honours_specific_user_agent() {
        let txt = "User-agent: *
        Disallow: /tmp

        User-agent: agent
        Allow: /tmp";
        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert!(r.allowed("/tmp"));
        assert!(r.allowed("/path"));
    }

    #[test]
    fn test_reppy_grouping() {
        let txt = "User-agent: one
        User-agent: two
        Disallow: /tmp";
        let r = Robot::new("one", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/tmp"));
        let r = Robot::new("two", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/tmp"));
    }

    /*
    // Disabled as it conflicts with a Google unit test
    // There's also a legitimate interpretation where disallow takes precedence
    #[test]
    fn test_reppy_grouping_unknown_keys() {
        let txt = "User-agent: *
        Disallow: /content/2/
        User-agent: *
        Noindex: /gb.html
        Noindex: /content/2/
        User-agent: ia_archiver
        Disallow: /";
        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert!(r.allowed("/foo"));
        let r = Robot::new("ia_archiver", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/bar"));
    }
    */

    #[test]
    fn test_reppy_separates_agents() {
        let txt = "User-agent: one
        Crawl-delay: 1

        User-agent: two
        Crawl-delay: 2";
        let r = Robot::new("one", txt.as_bytes()).unwrap();
        let u = Robot::new("two", txt.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(1.0));
        assert_eq!(u.delay, Some(2.0));
    }

    #[test]
    fn test_reppy_finds_and_exposes_sitemaps() {
        let txt = "            Sitemap: http://a.com/sitemap.xml
        Sitemap: http://b.com/sitemap.xml";
        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert_eq!(
            r.sitemaps,
            vec!["http://a.com/sitemap.xml", "http://b.com/sitemap.xml"]
        );
    }

    #[test]
    fn test_reppy_case_insensitivity() {
        let txt = "User-agent: Agent
        Disallow: /path";
        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/path"));
        let r = Robot::new("AGeNT", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/path"));
    }

    #[test]
    fn test_reppy_empty_allows_all() {
        let r = Robot::new("agent", b"").unwrap();
        assert!(r.sitemaps.is_empty());
        assert_eq!(r.delay, None);
        assert!(r.allowed("/"));
        assert!(r.allowed("/foo"));
        assert!(r.allowed("/foo/bar"));
    }

    #[test]
    fn test_reppy_comments() {
        let txt = "User-Agent: *  # comment saying it's the default agent
        Allow: /
        Disallow: /foo";
        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert!(r.allowed("/"));
        assert!(!r.allowed("/foo"));
        assert!(!r.allowed("/foo/bar"));
    }

    #[test]
    fn test_reppy_accepts_full_url() {
        let txt = "User-Agent: *  # comment saying it's the default agent
        Allow: /
        Disallow: /foo";
        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert!(r.allowed("https://example.com/"));
        assert!(!r.allowed("https://example.com/foo"));
        assert!(!r.allowed("https://example.com/foo/bar"));
        assert!(r.allowed("https://example.com/found"));
    }

    /* #[test]
    fn test_reppy_skips_malformed_line() {
        // Note: This conflicts with Google as they allow "Disallow /path"
        let txt = "User-Agent: agent
        Disallow /no/colon/in/this/line";
        let r = Robot::new("agent", txt.as_bytes()).unwrap();
        assert!(r.allowed("/no/colon/in/this/line"));
    } */

    // TODO: Allow for HTTP status code consideration
    // See: Robots.fetch examples

    // Ignored reppy tests:
    // - test_utf8_bom: Google considers any line with bom as malformed

    #[test]
    fn test_robot_rfc_example() {
        let txt = "# /robots.txt for http://www.fict.org/
        # comments to webmaster@fict.org

        User-agent: unhipbot
        Disallow: /

        User-agent: webcrawler
        User-agent: excite
        Disallow:

        User-agent: *
        Disallow: /org/plans.html
        Allow: /org/
        Allow: /serv
        Allow: /~mak
        Disallow: /";

        let targets = vec![
            "/",
            "/index.html",
            "/server.html",
            "/services/fast.html",
            "/services/slow.html",
            "/orgo.gif",
            "/org/about.html",
            "/org/plans.html",
            "/%7Ejim/jim.html",
            "/~mak/mak.html",
        ];

        let r = Robot::new("unhipbot", txt.as_bytes()).unwrap();
        assert!(r.allowed("/robots.txt"));
        for t in &targets {
            assert!(!r.allowed(t));
        }

        let r = Robot::new("webcrawler", txt.as_bytes()).unwrap();
        assert!(r.allowed("/robots.txt"));
        for t in &targets {
            assert!(r.allowed(t), "Allowed failed on {}", t);
        }

        let r = Robot::new("excite", txt.as_bytes()).unwrap();
        assert!(r.allowed("/robots.txt"));
        for t in &targets {
            assert!(r.allowed(t), "Allowed failed on {}", t);
        }

        let r = Robot::new("anything", txt.as_bytes()).unwrap();
        assert!(r.allowed("/robots.txt"));
        assert!(!r.allowed("/"));
        assert!(!r.allowed("/index.html"));
        assert!(r.allowed("/server.html"));
        assert!(r.allowed("/services/fast.html"));
        assert!(r.allowed("/services/slow.html"));
        assert!(!r.allowed("/orgo.gif"));
        assert!(r.allowed("/org/about.html"));
        assert!(!r.allowed("/org/plans.html"));
        assert!(!r.allowed("/%7Ejim/jim.html"));
        assert!(r.allowed("/~mak/mak.html"));
    }

    /// TEST FORGIVENESS
    /// Inspired by Google allowing a million variations of "disallow"
    ////////////////////////////////////////////////////////////////////////////////

    #[test]
    fn test_forgiveness_disallow_all_but_no_colon() {
        let text = "user-agent: FooBot
        disallow /\n";
        let r = Robot::new("FooBot", text.as_bytes()).unwrap();
        assert!(!r.allowed("/"));
        assert!(!r.allowed("/foo"));
    }

    #[test]
    fn test_forgiveness_disallow_variations() {
        let text = "user-agent: FooBot
        disallow: /a
        dissallow: /b
        dissalow: /c
        disalow: /d
        diasllow: /e
        disallaw: /f\n";
        let r = Robot::new("FooBot", text.as_bytes()).unwrap();
        for path in vec!["/a", "/b", "/c", "/d", "/e", "/f"] {
            assert!(!r.allowed(path));
        }
    }

    #[test]
    fn test_forgiveness_ensure_not_too_forgiving() {
        let text = "user-agent: FooBot
        disallow:/a
        dissallow/b
        disallow    /c\n";
        let r = Robot::new("FooBot", text.as_bytes()).unwrap();
        assert!(!r.allowed("/a"));
        assert!(r.allowed("/b"));
        assert!(!r.allowed("/c"));
    }

    #[test]
    fn test_forgiveness_sitemap_variations() {
        let text = "user-agent: FooBot
        site-map: /a
        sitemap: /b
        site map: /c\n";
        let r = Robot::new("FooBot", text.as_bytes()).unwrap();
        assert_eq!(r.sitemaps, vec!["/a", "/b", "/c"]);
    }

    #[test]
    fn test_forgiveness_crawl_delay_variations() {
        let text = "user-agent: FooBot
        crawl-delay: 42
        user-agent: BobBot
        crawl delay: 420
        user-agent: EveBot
        crawldelay: 360\n";
        let r = Robot::new("FooBot", text.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(42.0));
        let r = Robot::new("BobBot", text.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(420.0));
        let r = Robot::new("EveBot", text.as_bytes()).unwrap();
        assert_eq!(r.delay, Some(360.0));
    }

    #[test]
    fn test_forgiveness_user_agent_variations() {
        let text = "user-agent: FooBot
        disallow: /a
        user agent: BobBot
        disallow: /b
        useragent: EveBot
        disallow: /e\n";
        let r = Robot::new("FooBot", text.as_bytes()).unwrap();
        assert!(!r.allowed("/a"));
        let r = Robot::new("BobBot", text.as_bytes()).unwrap();
        assert!(!r.allowed("/b"));
        let r = Robot::new("EveBot", text.as_bytes()).unwrap();
        assert!(!r.allowed("/e"));
    }

    /// GOOGLE TESTS
    ////////////////////////////////////////////////////////////////////////////////

    #[test]
    fn test_google_foo_bar() {
        let text = "foo: FooBot
        bar: /\n";
        let r = Robot::new("FooBot", text.as_bytes()).unwrap();
        assert!(r.allowed("/"));
        assert!(r.allowed("/foo"));
    }

    #[test]
    fn test_google_allows_disallow_with_no_colon() {
        // This stands in conflict to reppy's "test_reppy_skips_malformed_line"
        let txt = "user-agent FooBot
        disallow /\n";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/"));
    }

    #[test]
    fn test_google_grouping() {
        let txt = "allow: /foo/bar/

        user-agent: FooBot
        disallow: /
        allow: /x/
        user-agent: BarBot
        disallow: /
        allow: /y/


        allow: /w/
        user-agent: BazBot

        user-agent: FooBot
        allow: /z/
        disallow: /";

        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/b"));
        assert!(r.allowed("http://foo.bar/z/d"));
        assert!(!r.allowed("http://foo.bar/y/c"));
        // Line outside of groupings ignored
        assert!(!r.allowed("http://foo.bar/foo/bar/"));

        let r = Robot::new("BarBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/y/c"));
        assert!(r.allowed("http://foo.bar/w/a"));
        assert!(!r.allowed("http://foo.bar/z/d"));
        // Line outside of groupings ignored
        assert!(!r.allowed("http://foo.bar/foo/bar/"));

        let r = Robot::new("BazBot", txt.as_bytes()).unwrap();
        println!("{:?}", r);
        assert!(r.allowed("http://foo.bar/z/d"));
        // Line outside of groupings ignored
        assert!(!r.allowed("http://foo.bar/foo/bar/"));
    }

    #[test]
    fn test_google_grouping_other_rules() {
        // This test stands in conflict with reppy's "test_robot_grouping_unknown_keys"
        let txt = "User-agent: BarBot
        Sitemap: https://foo.bar/sitemap
        User-agent: *
        Disallow: /";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/"));
        let r = Robot::new("BarBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/"));

        let txt = "User-agent: FooBot
        Invalid-Unknown-Line: unknown
        User-agent: *
        Disallow: /\n";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/"));
        let r = Robot::new("BarBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/"));
    }

    #[test]
    fn test_google_lines_and_bots_are_case_insensitive() {
        let txt = "USER-AGENT: FooBot
        ALLOW: /x/
        DISALLOW: /

        user-agent: BarBot
        allow: /x/
        disallow: /

        uSeR-aGeNt: BAZBOT
        AlLoW: /x/
        dIsAlLoW: /";

        for bot in vec!["FooBot", "BarBot", "BazBot"] {
            let r = Robot::new(bot, txt.as_bytes()).unwrap();
            assert!(r.allowed("http://foo.bar/x/y"));
            assert!(!r.allowed("http://foo.bar/a/b"));
        }
    }

    #[test]
    fn test_google_global_groups_secondary() {
        // Empty robots.txt is handled in a separate test

        let global = "user-agent: *
        allow: /
        user-agent: FooBot
        disallow: /";
        let r = Robot::new("FooBot", global.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/x/y"));
        let r = Robot::new("BarBot", global.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/y"));

        // If not specified you may assume full permission
        let specific = "user-agent: FooBot
        allow: /
        user-agent: BarBot
        disallow: /
        user-agent: BazBot
        disallow: /";
        let r = Robot::new("QuxBot", specific.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/y"));
    }

    #[test]
    fn test_google_allow_disallow_value_case_sensitive() {
        let txt = "user-agent: FooBot
        disallow: /x/";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/x/y"));

        let txt = "user-agent: FooBot
        disallow: /X/";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/y"));
    }

    #[test]
    fn test_google_longest_match() {
        let txt = "user-agent: FooBot
        disallow: /x/page.html
        allow: /x/";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/x/page.html"));

        let txt = "user-agent: FooBot
        allow: /x/page.html
        disallow: /x/";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/page.html"));
        assert!(!r.allowed("http://foo.bar/x/"));

        let txt = "user-agent: FooBot
        disallow: 
        allow: ";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/page.html"));

        let txt = "user-agent: FooBot
        disallow: /
        allow: /";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/page.html"));

        let txt = "user-agent: FooBot
        disallow: /x
        allow: /x/";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/x"));
        assert!(r.allowed("http://foo.bar/x/"));

        let txt = "user-agent: FooBot
        disallow: /x/page.html
        allow: /x/page.html";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/page.html"));

        let txt = "user-agent: FooBot
        allow: /page
        disallow: /*.html";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/page.html"));
        assert!(r.allowed("http://foo.bar/page"));

        // "Longest match wins"
        let txt = "user-agent: FooBot
        allow: /x/page.
        disallow: /*.html";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/page.html"));
        assert!(!r.allowed("http://foo.bar/x/y.html"));

        let txt = "User-agent: *
        Disallow: /x/
        User-agent: FooBot
        Disallow: /y/";
        // Most specific group for FooBot allows implicitly /x/page
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/x/page"));
        assert!(!r.allowed("http://foo.bar/y/page"));
    }

    #[test]
    fn test_google_encoding() {
        let txt = "User-agent: FooBot
        Disallow: /
        Allow: /foo/bar?qux=taz&baz=http://foo.bar?tar&par";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed(
            "http://foo.bar/foo/bar?qux=taz&baz=http://foo.bar?tar&par"
        ));

        let txt = "User-agent: FooBot
        Disallow: /
        Allow: /foo/bar/ใƒ„";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/foo/bar/ใƒ„"));
        assert!(r.allowed("http://foo.bar/foo/bar/%E3%83%84"));
        assert!(r.allowed("/foo/bar/ใƒ„"));
        assert!(r.allowed("/foo/bar/%E3%83%84"));

        let txt = "User-agent: FooBot
        Disallow: /
        Allow: /foo/bar/%E3%83%84";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/foo/bar/%E3%83%84"));
        // Google's test says this should fail but I think that's as they don't have percent encoding in pipeline
        assert!(r.allowed("http://foo.bar/foo/bar/ใƒ„"));

        let txt = "User-agent: FooBot
        Disallow: /
        Allow: /foo/bar/%62%61%7A";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/foo/bar/baz"));
        assert!(r.allowed("http://foo.bar/foo/bar/%62%61%7A"));
    }

    #[test]
    fn test_google_special_characters() {
        let txt = "User-agent: FooBot
        Disallow: /foo/bar/quz
        Allow: /foo/*/qux";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/foo/bar/quz"));
        assert!(r.allowed("http://foo.bar/foo/quz"));
        assert!(r.allowed("http://foo.bar/foo//quz"));
        assert!(r.allowed("http://foo.bar/foo/bax/quz"));

        let txt = "User-agent: FooBot
        Disallow: /foo/bar$
        Allow: /foo/bar/qux";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/foo/bar"));
        assert!(r.allowed("http://foo.bar/foo/bar/qux"));
        assert!(r.allowed("http://foo.bar/foo/bar/"));
        assert!(r.allowed("http://foo.bar/foo/bar/baz"));

        let txt = "User-agent: FooBot
        # Disallow: /
        Disallow: /foo/quz#qux
        Allow: /";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/foo/bar"));
        assert!(!r.allowed("http://foo.bar/foo/quz"));
    }

    #[test]
    fn test_google_documentation_checks() {
        for r in vec!["/fish", "/fish*"] {
            let txt = format!(
                "user-agent: FooBot
            disallow: /
            allow: {}",
                r
            );
            let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
            assert!(!r.allowed("http://foo.bar/bar"));
            assert!(r.allowed("http://foo.bar/fish"));
            assert!(r.allowed("http://foo.bar/fish/salmon"));
            assert!(r.allowed("http://foo.bar/fishheads"));
            assert!(r.allowed("http://foo.bar/fishheads/yummy.html"));
            assert!(r.allowed("http://foo.bar/fish.html?id=anything"));
            assert!(!r.allowed("http://foo.bar/Fish.asp"));
            assert!(!r.allowed("http://foo.bar/catfish"));
            assert!(!r.allowed("http://foo.bar/?id=fish"));
        }

        let txt = "user-agent: FooBot
        disallow: /
        allow: /fish/";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://foo.bar/fish/"));
        assert!(r.allowed("http://foo.bar/fish/salmon"));
        assert!(r.allowed("http://foo.bar/fish/?salmon"));
        assert!(r.allowed("http://foo.bar/fish/salmon.html"));
        assert!(r.allowed("http://foo.bar/fish/?id=anything"));

        assert!(!r.allowed("http://foo.bar/fish"));
        assert!(!r.allowed("http://foo.bar/fish.html"));
        assert!(!r.allowed("http://foo.bar/Fish/Salmon.html"));

        let txt = "user-agent: FooBot
        disallow: /
        allow: /*.php";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/bar"));
        assert!(r.allowed("http://foo.bar/filename.php"));
        assert!(r.allowed("http://foo.bar/folder/filename.php"));
        assert!(r.allowed("http://foo.bar//folder/any.php.file.html"));
        assert!(r.allowed("http://foo.bar/filename.php/"));
        assert!(r.allowed("http://foo.bar/index?f=filename.php/"));
        assert!(!r.allowed("http://foo.bar/php/"));
        assert!(!r.allowed("http://foo.bar/index?php"));
        assert!(!r.allowed("http://foo.bar/windows.PHP"));

        let txt = "user-agent: FooBot
        disallow: /
        allow: /*.php$";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/bar"));
        assert!(r.allowed("http://foo.bar/filename.php"));
        assert!(r.allowed("http://foo.bar/folder/filename.php"));
        //
        assert!(!r.allowed("http://foo.bar/filename.php?parameters"));
        assert!(!r.allowed("http://foo.bar/filename.php/"));
        assert!(!r.allowed("http://foo.bar/filename.php5"));
        assert!(!r.allowed("http://foo.bar/php/"));
        assert!(!r.allowed("http://foo.bar/filename?php"));
        assert!(!r.allowed("http://foo.bar/aaaphpaaa"));
        assert!(!r.allowed("http://foo.bar//windows.PHP"));

        let txt = "user-agent: FooBot
        disallow: /
        allow: /fish*.php";
        let r = Robot::new("FooBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("http://foo.bar/bar"));
        assert!(r.allowed("http://foo.bar/fish.php"));
        assert!(r.allowed("http://foo.bar/fishheads/catfish.php?parameters"));
        assert!(!r.allowed("http://foo.bar/Fish.PHP"));
    }

    #[test]
    fn test_google_order_of_precedence() {
        // From https://developers.google.com/search/docs/advanced/robots/robots_txt : "Order of precedence for rules"
        let txt = "allow: /p
        disallow: /";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("/page"));
        assert!(r.allowed("http://example.com/page"));

        let txt = "allow: /folder
        disallow: /folder";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("/folder"));
        assert!(r.allowed("http://example.com/folder/page"));

        let txt = "allow: /page
        disallow: /*.htm";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(!r.allowed("/page.htm"));
        assert!(!r.allowed("http://example.com/page.htm"));

        // Skipping the "page.php5" example as I don't understand / agree

        let txt = "allow: /$
        disallow: /";
        let r = Robot::new("BobBot", txt.as_bytes()).unwrap();
        assert!(r.allowed("http://example.com/"));
        assert!(!r.allowed("http://example.com/page.htm"));
    }

    #[test]
    fn test_google_lines_correctly_counted() {
        // Skipping "\r" only line ending - assuming "\r\n" or "\n"
        for line_ending in &["\n", "\r\n"] {
            let txt = "User-Agent: foo
            Allow: /some/path
            User-Agent: bar
            
            
            Disallow: /";
            let txt = txt.replace("\n", line_ending);
            let (buffer, lines) = robots_txt_parse(txt.as_bytes()).unwrap();
            assert!(buffer.is_empty());
            assert_eq!(lines.len(), 6);
            assert_eq!(
                lines
                    .iter()
                    .filter(|x| matches!(
                        x,
                        UserAgent(_) | Allow(_) | Disallow(_)
                    ))
                    .count(),
                4
            );
        }

        // Add an extra newline at the very end
        let txt = "User-Agent: foo
        Allow: /some/path
        User-Agent: bar


        Disallow: /\n";
        let (buffer, lines) = robots_txt_parse(txt.as_bytes()).unwrap();
        assert!(buffer.is_empty());
        assert_eq!(lines.len(), 6);
        assert_eq!(
            lines
                .iter()
                .filter(|x| matches!(x, UserAgent(_) | Allow(_) | Disallow(_)))
                .count(),
            4
        );

        // Mixed \n and \r\n
        let txt = "User-Agent: foo\nAllow: /some/path\r\nUser-Agent: bar\n\r\n\nDisallow: /\n";
        let (buffer, lines) = robots_txt_parse(txt.as_bytes()).unwrap();
        assert!(buffer.is_empty());
        assert_eq!(lines.len(), 6);
        assert_eq!(
            lines
                .iter()
                .filter(|x| matches!(x, UserAgent(_) | Allow(_) | Disallow(_)))
                .count(),
            4
        );
    }

    #[test]
    fn test_google_utf8_bom_is_skipped() {
        for bom in vec![
            b"\xef\xbb\xbf".to_vec(),
            b"\xef\xbb".to_vec(),
            b"\xef".to_vec(),
        ] {
            let txt = b"User-Agent: foo\nAllow: /AnyValue\n".to_vec();
            let txt = [&bom[..], &txt[..]].concat();
            let (buffer, lines) = robots_txt_parse(&txt).unwrap();
            assert!(buffer.is_empty());
            assert_eq!(lines.len(), 2);
            assert_eq!(
                lines
                    .iter()
                    .filter(|x| matches!(
                        x,
                        UserAgent(_) | Allow(_) | Disallow(_)
                    ))
                    .count(),
                2
            );
        }

        // Broken BOM: Expect one broken line (i.e. "\x11\xbfUser-Agent")
        let txt = b"\xef\x11\xbfUser-Agent: foo\nAllow: /AnyValue\n";
        let (buffer, lines) = robots_txt_parse(txt).unwrap();
        assert!(buffer.is_empty());
        assert_eq!(
            lines,
            vec![Raw(b"\x11\xbfUser-Agent: foo"), Allow(b"/AnyValue")]
        );
        assert_eq!(lines.len(), 2);
        assert_eq!(
            lines
                .iter()
                .filter(|x| matches!(x, UserAgent(_) | Allow(_) | Disallow(_)))
                .count(),
            1
        );

        // BOM in middle of the file
        let txt = b"User-Agent: foo\n\xef\xbb\xbfAllow: /AnyValue\n";
        let (buffer, lines) = robots_txt_parse(txt).unwrap();
        assert!(buffer.is_empty());
        assert_eq!(
            lines,
            vec![UserAgent(b"foo"), Raw(b"\xef\xbb\xbfAllow: /AnyValue")]
        );
        assert_eq!(lines.len(), 2);
        assert_eq!(
            lines
                .iter()
                .filter(|x| matches!(x, UserAgent(_) | Allow(_) | Disallow(_)))
                .count(),
            1
        );
    }

    #[test]
    fn test_google_url_prepare_get_path_params_query() {
        // Note: We skip part of the test as we assume the user passed in a URL with valid http/s, not "example.com"
        for (url, path) in vec![
            ("", "/"),
            ("https://example.com", "/"),
            ("https://example.com/", "/"),
            ("http://www.example.com/a", "/a"),
            ("http://www.example.com/a/", "/a/"),
            ("http://www.example.com/a/b?c=http://d.e/", "/a/b?c=http://d.e/"),
            (
                "http://www.example.com/a/b?c=d&e=f#fragment",
                "/a/b?c=d&e=f#fragment",
            ),
        ] {
            assert_eq!(Robot::prepare_url(url), path);
            assert_eq!(Robot::prepare_url(path), path);
        }
    }

    #[test]
    fn test_google_url_prepare_escape_pattern() {
        // For the complexity of whether to normalize percent encoding (i.e. "%AA" = "%aa") see:
        // https://github.com/servo/rust-url/issues/149
        // "the algorithm specified at https://url.spec.whatwg.org/#path-state ..."
        // "leaves existing percent-encoded sequences unchanged"
        for (start, end) in vec![
            ("http://www.example.com", "/"),
            ("/a/b/c", "/a/b/c"),
            ("/รก", "/%C3%A1"),
            // According the above, percent encoded remain encoded the same as before
            ("/%aa", "/%aa"),
        ] {
            assert_eq!(Robot::prepare_url(start), end);
        }
    }

    // Ignored Google test:
    // - ID_VerifyValidUserAgentsToObey ensures agents are [A-Za-z_-]
    // - Skip "GoogleOnly_AcceptUserAgentUpToFirstSpace"
    //   -(i.e. "Googlebot-Images" being "Googlebot Images" and screwing "Googlebot")
    // - Skip "GoogleOnly_IndexHTMLisDirectory" (i.e. allow "/index.html" if "/" is allowed)
    // - Skip "GoogleOnly_LineTooLong" (though something equivalent makes sense)
    // - TODO: Test the path + params conversion
}