forked from PuerkitoBio/gocrawl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tbldef_test.go
1141 lines (1080 loc) · 27.6 KB
/
tbldef_test.go
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
package gocrawl
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/PuerkitoBio/purell"
)
// Type a is a simple syntax helper to create test cases' asserts.
type a map[extensionMethodKey]int
// Type f is a simple syntax helper to create test cases' extension functions.
type f map[extensionMethodKey]interface{}
// Test case structure.
type testCase struct {
name string
opts *Options
http bool
seeds interface{}
funcs f
asserts a
logAsserts []string
customAssert func(*spyExtender, *testing.T)
panics bool
external func(*testing.T, *testCase, bool)
}
var (
// Actual definition of test cases.
// Prefix name with "*" to run this single starred test.
// Prefix name with "!" to ignore this test.
cases = [...]*testCase{
&testCase{
name: "AllSameHost",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://hosta/page1.html",
"http://hosta/page4.html",
},
asserts: a{
eMKVisit: 5,
eMKFilter: 13,
},
},
&testCase{
name: "AllNotSameHost",
opts: &Options{
SameHostOnly: false,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://hosta/page1.html",
"http://hosta/page4.html",
},
asserts: a{
eMKVisit: 10,
eMKFilter: 24,
},
},
&testCase{
name: "SelectOnlyPage1s",
opts: &Options{
SameHostOnly: false,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://hosta/page1.html",
"http://hosta/page4.html",
"http://hostb/pageunlinked.html",
},
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
if ctx.normalizedURL.Path == "/page1.html" {
return !isVisited
}
return false
},
},
asserts: a{
eMKVisit: 3, // hostd not visited (not even fetched) because linked only from hostc/page3, which is not a page1
eMKFilter: 11,
},
},
&testCase{
name: "IdleTimeOut",
opts: &Options{
SameHostOnly: false,
WorkerIdleTTL: 50 * time.Millisecond,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogInfo,
},
seeds: []string{
"http://hosta/page1.html",
"http://hosta/page4.html",
"http://hostb/pageunlinked.html",
},
logAsserts: []string{
"worker for host hostd cleared on idle policy\n",
"worker for host hostunknown cleared on idle policy\n",
},
},
&testCase{
name: "EnqueuedCount",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://robota/page1.html",
},
asserts: a{
eMKEnqueued: 2, // page1 and robots.txt (did not visit page1, so page2 never found)
eMKVisit: 0, // No visit per robots policy
},
},
&testCase{
name: "VisitedCount",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://hosta/page1.html",
},
asserts: a{
eMKVisited: 3,
},
},
&testCase{
name: "StartExtender",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://hostc/page1.html",
},
funcs: f{
eMKStart: func(seeds interface{}) interface{} {
ar := seeds.([]string)
return append(ar, "http://hostb/page1.html")
},
},
asserts: a{
eMKStart: 1,
eMKVisit: 4,
eMKEnqueued: 7, // Page1-2 for both, robots a-b, page unknown
},
},
&testCase{
name: "ComputeDelay",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogError | LogInfo,
},
seeds: []string{
"http://hosta/page1.html",
},
funcs: f{
eMKComputeDelay: func(host string, di *DelayInfo, lf *FetchInfo) time.Duration {
return 17 * time.Millisecond
},
},
asserts: a{
eMKComputeDelay: 4,
},
logAsserts: []string{
"using crawl-delay: 17ms\n",
},
},
&testCase{
name: "Filter",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogError | LogIgnored,
},
seeds: []string{
"http://hostc/page1.html",
},
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
return strings.HasSuffix(ctx.url.Path, "page1.html")
},
},
asserts: a{
eMKFilter: 3,
eMKEnqueued: 2, // robots.txt triggers Enqueued too
},
logAsserts: []string{
"ignore on filter policy: http://hostc/page2.html\n",
},
},
&testCase{
name: "HeadResponse",
http: true,
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
HeadBeforeGet: true,
MaxVisits: 1,
LogFlags: LogAll,
},
seeds: []string{
"http://httpstat.us/200",
},
funcs: f{
eMKRequestGet: func(ctx *URLContext, headRes *http.Response) bool {
assertTrue(len(headRes.Header) > 0, "expected headers to be present")
b, e := ioutil.ReadAll(headRes.Body)
if assertTrue(e == nil, "%s", e) {
assertTrue(len(b) == 0, "expected body to be empty")
}
return false
},
},
},
&testCase{
name: "RedirectRelative-i10",
http: true,
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://golang.org/pkg",
},
funcs: f{
eMKVisit: func(ctx *URLContext, res *http.Response, doc *goquery.Document) (harvested interface{}, findLinks bool) {
return nil, false
},
},
logAsserts: []string{
"!ignore on absolute policy: /pkg",
},
},
&testCase{
name: "CircularRedirect-i10",
http: true,
opts: &Options{
SameHostOnly: true,
MaxVisits: 1,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://golang.org/pkg",
},
funcs: f{
eMKVisit: func(ctx *URLContext, res *http.Response, doc *goquery.Document) (harvested interface{}, findLinks bool) {
return nil, false
},
},
asserts: a{
eMKEnqueued: 4, // Expect 4 Enqueued calls: robots, /pkg (redirects 302, 301), /pkg/
eMKFilter: 3, // Two for /pkg and one for /pkg/
eMKVisit: 1, // Expect 1 visit : /pkg/ (robots don't trigger visited)
},
},
&testCase{
name: "SameHostPolicyWithNormalizedSourceUrl-i13",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
URLNormalizationFlags: DefaultNormalizationFlags,
LogFlags: LogAll,
},
seeds: []string{
"http://www.hosta/page1.html",
},
funcs: f{
eMKVisit: func(ctx *URLContext, res *http.Response, doc *goquery.Document) (interface{}, bool) {
if res.Request.URL.Path == "/page1.html" {
u, err := res.Request.URL.Parse("page2.html")
if err != nil {
panic(err)
}
return []*url.URL{u}, false
}
return nil, false
},
},
asserts: a{
eMKDisallowed: 0,
eMKFilter: 2, // page1, page2
eMKVisit: 2, // page1, page2
},
},
&testCase{
name: "SameHostPolicyRejectWithNormalizedSourceUrl-i13",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
URLNormalizationFlags: DefaultNormalizationFlags,
},
seeds: []string{
"http://www.hosta/page1.html",
},
funcs: f{
eMKVisit: func(ctx *URLContext, res *http.Response, doc *goquery.Document) (interface{}, bool) {
if res.Request.URL.Host == "www.hosta" {
u, err := url.Parse("http://www.hostb/page1.html")
if err != nil {
panic(err)
}
return u, false
}
return nil, false
},
},
asserts: a{
eMKFilter: 2, // hosta/page1, hostb/page1
eMKVisit: 1, // hosta/page1
},
logAsserts: []string{
"ignore on same host policy: http://hostb/page1.html",
},
},
&testCase{
name: "ReadBodyInVisitor",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
MaxVisits: 1,
LogFlags: LogAll,
URLNormalizationFlags: DefaultNormalizationFlags,
},
seeds: []string{
"http://hostc/page3.html",
},
funcs: f{
eMKVisit: func(ctx *URLContext, res *http.Response, doc *goquery.Document) (interface{}, bool) {
b, err := ioutil.ReadAll(res.Body)
if assertTrue(err == nil, "%s", err) {
assertTrue(len(b) > 0, "expected some content in the body")
}
return nil, false
},
},
},
&testCase{
name: "EndReasonMaxVisits",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
MaxVisits: 1,
LogFlags: LogAll,
},
seeds: []string{
"http://hosta/page1.html",
},
funcs: f{
eMKEnd: func(err error) {
assertTrue(err == ErrMaxVisits, "expected error to be ErrMaxVisits")
},
},
asserts: a{
eMKEnd: 1,
},
},
&testCase{
name: "EndReasonDone",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://hosta/page5.html",
},
funcs: f{
eMKEnd: func(err error) {
assertTrue(err == nil, "expected error to be nil")
},
},
asserts: a{
eMKEnd: 1,
},
},
&testCase{
name: "ErrorFetch",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://hostb/page2.html", // Will try to load pageunknown.html
},
funcs: f{
eMKError: func(err *CrawlError) {
assertTrue(err.Kind == CekFetch, "expected error to be of kind %s, got %s", CekFetch, err.Kind)
},
},
asserts: a{
eMKError: 1,
},
},
&testCase{
name: "NonHtmlRequest",
http: true,
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"https://lh4.googleusercontent.com/-v0soe-ievYE/AAAAAAAAAAI/AAAAAAAAs7Y/_UbxpxC-VG0/photo.jpg",
},
asserts: a{
eMKError: 0,
eMKVisit: 1,
},
},
&testCase{
name: "InvalidSeed",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"#toto",
},
customAssert: func(spy *spyExtender, t *testing.T) {
v := runtime.Version()
if strings.HasPrefix(v, "go1.0") {
assertCallCount(spy, "InvalidSeed", eMKError, 1, t)
assertIsInLog("InvalidSeed", spy.b, "ERROR parsing URL #toto\n", t)
} else {
assertIsInLog("InvalidSeed", spy.b, "ignore on absolute policy: #toto\n", t)
}
assertCallCount(spy, "InvalidSeed", eMKVisit, 0, t)
},
},
&testCase{
name: "HostCount",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"ftp://roota/a", // Use FTP scheme so that it doesn't actually attempt a fetch
"ftp://roota/b",
"ftp://rootb/c",
},
asserts: a{
eMKVisit: 0,
},
logAsserts: []string{
"init() - host count: 2\n",
"init() - seeds length: 3\n",
},
},
&testCase{
name: "CustomFilterNoURL",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: []string{
"http://test1",
"http://test2",
},
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
return false
},
},
asserts: a{
eMKVisit: 0,
eMKFilter: 2,
},
logAsserts: []string{
"ignore on filter policy: http://test1\n",
"ignore on filter policy: http://test2\n",
},
},
&testCase{
name: "NoSeed",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: nil,
asserts: a{
eMKVisit: 0,
eMKFilter: 0,
eMKError: 0,
},
},
&testCase{
name: "NoVisitorFunc",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: "http://hosta/page1.html",
asserts: a{
eMKVisit: 3, // With default Filter and Visit, will visit all same-host links
eMKFilter: 10,
eMKError: 0,
},
},
&testCase{
name: "EnqueueChanDefault",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: "",
customAssert: func(spy *spyExtender, t *testing.T) {
assertTrue(spy.EnqueueChan != nil, "expected EnqueueChan to be non-nil")
},
},
&testCase{
name: "RobotDenyAll",
opts: &Options{
SameHostOnly: false,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
RobotUserAgent: DefaultRobotUserAgent,
},
seeds: "http://robota/page1.html",
asserts: a{
eMKVisit: 0,
eMKFilter: 1,
},
},
&testCase{
name: "RobotPartialDenyGooglebot",
opts: &Options{
SameHostOnly: false,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
RobotUserAgent: DefaultRobotUserAgent,
},
seeds: "http://robotb/page1.html",
asserts: a{
eMKVisit: 2,
eMKFilter: 4,
},
},
&testCase{
name: "RobotDenyOtherBot",
opts: &Options{
SameHostOnly: false,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
RobotUserAgent: "NotGoogleBot",
},
seeds: "http://robotb/page1.html",
asserts: a{
eMKVisit: 4,
eMKFilter: 5,
},
},
&testCase{
name: "RobotExplicitAllowPattern",
opts: &Options{
SameHostOnly: false,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
RobotUserAgent: DefaultRobotUserAgent,
},
seeds: "http://robotc/page1.html",
asserts: a{
eMKVisit: 4,
eMKFilter: 5,
},
},
&testCase{
name: "RobotCrawlDelay",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
RobotUserAgent: DefaultRobotUserAgent,
},
seeds: "http://robotc/page1.html",
asserts: a{
eMKVisit: 4,
eMKFilter: 5,
},
logAsserts: []string{
"using crawl-delay: 200ms\n",
},
},
&testCase{
name: "CachedRobot",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
RobotUserAgent: DefaultRobotUserAgent,
},
seeds: "http://robota/page1.html",
funcs: f{
eMKRequestRobots: func(ctx *URLContext, agent string) ([]byte, bool) {
return []byte("User-agent: *\nDisallow:/page2.html"), false
},
},
asserts: a{
eMKVisit: 1,
eMKEnqueued: 3,
eMKRequestRobots: 1,
eMKDisallowed: 1,
},
},
&testCase{
name: "FetchedRobot",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
RobotUserAgent: DefaultRobotUserAgent,
},
seeds: "http://robotc/page4.html",
funcs: f{
eMKFetchedRobots: func(ctx *URLContext, res *http.Response) {
b, err := ioutil.ReadAll(res.Body)
if assertTrue(err == nil, "%s", err) {
assertTrue(len(b) > 0, "expected fetched robots.txt body not to be empty")
}
},
},
asserts: a{
eMKRequestRobots: 1,
eMKEnqueued: 2,
eMKFetchedRobots: 1,
},
},
&testCase{
name: "RequestGetFalse",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
HeadBeforeGet: true,
LogFlags: LogAll,
},
seeds: []string{
"http://hosta/page1.html",
},
funcs: f{
eMKRequestGet: func(ctx *URLContext, headRes *http.Response) bool {
if strings.ToLower(headRes.Request.URL.Path) == "/page2.html" {
return false
}
return true
},
},
asserts: a{
eMKFetch: 6, // Once for robots.txt and page2, twice each for page1 and page3
eMKRequestGet: 3,
eMKEnqueued: 4,
eMKVisit: 2,
},
customAssert: func(s *spyExtender, t *testing.T) {
head := s.getCalledWithCount(eMKFetch, ignore, ignore, true)
nohead := s.getCalledWithCount(eMKFetch, ignore, ignore, false)
// 3 GET: robots, page1, page3; 3 HEAD: page1, page2, page3
assertTrue(head == 3, "expected 3 HEAD requests, got %d", head)
assertTrue(nohead == 3, "expected 3 GET requests, got %d", nohead)
},
},
&testCase{
name: "NoHead",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
HeadBeforeGet: false,
LogFlags: LogAll,
},
seeds: []string{
"http://hostb/page1.html",
},
asserts: a{
eMKFetch: 4, // robots.txt and unknown.html triggers Fetch
eMKRequestGet: 0,
},
customAssert: func(s *spyExtender, t *testing.T) {
head := s.getCalledWithCount(eMKFetch, ignore, ignore, true)
assertTrue(head == 0, "expected no HEAD request, got %d", head)
},
},
&testCase{
name: "AllHead",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
HeadBeforeGet: true,
LogFlags: LogAll,
},
seeds: "http://hosta/page1.html",
asserts: a{
eMKFetch: 7, // Once for robots.txt, twice each for page1-3
eMKRequestGet: 3,
eMKEnqueued: 4,
},
customAssert: func(s *spyExtender, t *testing.T) {
head := s.getCalledWithCount(eMKFetch, ignore, ignore, true)
nohead := s.getCalledWithCount(eMKFetch, ignore, ignore, false)
assertTrue(head == nohead-1, "expected HEAD requests to be equal to GET requests minus one (robots.txt)")
},
},
&testCase{
name: "AllHeadWithFetchError",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
HeadBeforeGet: true,
LogFlags: LogAll,
},
seeds: "http://hostb/page1.html",
asserts: a{
eMKFetch: 6, // Once for robots.txt and unkwown.html, twice each for page1,2
eMKRequestGet: 2,
eMKEnqueued: 4,
eMKError: 1, // unknown.html HEAD request
},
customAssert: func(s *spyExtender, t *testing.T) {
head := s.getCalledWithCount(eMKFetch, ignore, ignore, true)
nohead := s.getCalledWithCount(eMKFetch, ignore, ignore, false)
// Head should be = 3 (page1, 2, unknown), Get should be = 3 (robots, page1, 2)
assertTrue(head == 3, "expected 3 HEAD requests, got %d", head)
assertTrue(nohead == 3, "expected 3 GET requests, got %d", nohead)
},
},
&testCase{
name: "HeadTrueOverride",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
HeadBeforeGet: true,
LogFlags: LogAll,
},
seeds: "http://hosta/page1.html",
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
// Page2: No HEAD, Page3: No enqueue
if strings.ToLower(ctx.url.Path) == "/page2.html" {
ctx.HeadBeforeGet = false
return !isVisited
} else if strings.ToLower(ctx.url.Path) == "/page3.html" {
return false
}
return !isVisited
},
},
asserts: a{
eMKFetch: 4, // Once for robots.txt and page2, twice for page1
eMKRequestGet: 1, // Page1 only, page2 ignored HEAD
eMKEnqueued: 3, // Page1-2 and robots
},
customAssert: func(s *spyExtender, t *testing.T) {
head := s.getCalledWithCount(eMKFetch, ignore, ignore, true)
nohead := s.getCalledWithCount(eMKFetch, ignore, ignore, false)
// 3 GET: robots, page1, page2; 1 HEAD: page1
assertTrue(head == 1, "expected 1 HEAD request, got %d", head)
assertTrue(nohead == 3, "expected 3 GET requests, got %d", nohead)
},
},
&testCase{
name: "HeadFalseOverride",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
HeadBeforeGet: false,
LogFlags: LogAll,
},
seeds: "http://hosta/page1.html",
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
// Page1: default, Page2: Head before get, Page3: No enqueue
if strings.ToLower(ctx.url.Path) == "/page2.html" {
ctx.HeadBeforeGet = true
return !isVisited
} else if strings.ToLower(ctx.url.Path) == "/page3.html" {
return false
}
return !isVisited
},
},
asserts: a{
eMKFetch: 4, // Once for robots.txt and page1, twice for page2
eMKRequestGet: 1, // Page2 only, page1 ignored HEAD
eMKEnqueued: 3, // Page1-2 and robots
},
customAssert: func(s *spyExtender, t *testing.T) {
head := s.getCalledWithCount(eMKFetch, ignore, ignore, true)
nohead := s.getCalledWithCount(eMKFetch, ignore, ignore, false)
// 3 GET: robots, page1, page2; 1 HEAD: page2
assertTrue(head == 1, "expected 1 HEAD request, got %d", head)
assertTrue(nohead == 3, "expected 3 GET requests, got %d", nohead)
},
},
&testCase{
name: "RedirectFilterOut",
http: true,
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: "http://src.ca",
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
// Accept only src.ca
return !isVisited && ctx.url.Host == "src.ca"
},
},
asserts: a{
eMKFilter: 2, // src.ca and radio-canada.ca
eMKEnqueued: 2, // src.ca and robots.txt
eMKFetch: 2, // src.ca and robots.txt
eMKVisit: 0, // src.ca redirects, so no visit
},
},
// ignore this test, now src.ca redirects to radio-canada.ca, then to ici.radio-canada.ca
// too fragile.
&testCase{
name: "!RedirectFollow",
http: true,
opts: &Options{
SameHostOnly: false,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: "http://src.ca",
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
return !isVisited && ctx.sourceURL == nil
},
},
asserts: a{
eMKEnqueued: 4, // src.ca, radio-canada.ca and both robots.txt
eMKFetch: 4, // src.ca, radio-canada.ca and both robots.txt
eMKVisit: 1, // src.ca redirects, radio-canada.ca visited
},
},
// Like RedirectFollow, src.ca has changed and redirects more times.
// Brittle test case.
&testCase{
name: "!RedirectFollowHeadFirst",
http: true,
opts: &Options{
SameHostOnly: false,
CrawlDelay: DefaultTestCrawlDelay,
HeadBeforeGet: true,
LogFlags: LogAll,
},
seeds: "http://src.ca",
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
return !isVisited && ctx.sourceURL == nil
},
},
asserts: a{
eMKEnqueued: 4, // src.ca, radio-canada.ca and both robots.txt
eMKRequestGet: 1, // radio-canada.ca only (no HEAD for robots, and src.ca gets redirected)
eMKFetch: 5, // src.ca, 2*radio-canada.ca and both robots.txt
eMKVisit: 1, // src.ca redirects, radio-canada.ca visited
},
},
&testCase{
name: "PanicInFilter",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: "http://hosta/page1.html",
funcs: f{
eMKFilter: func(ctx *URLContext, isVisited bool) bool {
panic("error")
},
},
panics: true,
},
&testCase{
name: "VisitReturnsURLsWithStateUsingS",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: "http://hosta/page1.html",
funcs: f{
eMKVisit: func(ctx *URLContext, res *http.Response, doc *goquery.Document) (interface{}, bool) {
if ctx.sourceURL == nil {
// Only when called for seed
return S{
"http://hosta/page2.html": 2,
"http://hosta/page3.html": 3,
"http://hosta/page4.html": 4,
"http://hosta/page5.html": 5,
}, false
}
rx := regexp.MustCompile(`/page(\d)\.html`)
mtch := rx.FindStringSubmatch(ctx.normalizedURL.Path)
i, ok := ctx.State.(int)
if assertTrue(ok, "expected state data to be an int for %s", ctx.normalizedURL) {
if page, err := strconv.Atoi(mtch[1]); err != nil {
panic(err)
} else {
assertTrue(page == i, "expected state for page%d.html to be %d, got %d", page, page, i)
}
}
return nil, false
},
},
asserts: a{
eMKFilter: 5,
eMKVisit: 5,
eMKEnqueued: 6, // 5 pages + robots
},
},
&testCase{
name: "VisitReturnsURLsWithStateUsingU",
opts: &Options{
SameHostOnly: true,
CrawlDelay: DefaultTestCrawlDelay,
LogFlags: LogAll,
},
seeds: "http://hosta/page1.html",
funcs: f{
eMKVisit: func(ctx *URLContext, res *http.Response, doc *goquery.Document) (interface{}, bool) {
if ctx.sourceURL == nil {
// Only when called for seed