spider 2.48.21

A web crawler and scraper, building blocks for data curation workloads.
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
use crate::helpers::*;
use spider::hashbrown::HashMap;
use spider::reqwest::StatusCode;
use spider::website::Website;

// --- Double slash variants ---

#[tokio::test]
async fn double_slash_urls() {
    if !run_live_tests() {
        return;
    }

    let paths = [
        "/urls/double_slash//one",
        "/urls/double_slash////two",
        "/urls/double_slash//////three",
        "/urls/double_slash//////four//",
    ];

    for path in &paths {
        let page = fetch_page_http(path).await;
        assert!(
            page.status_code.is_success() || page.status_code == StatusCode::NOT_FOUND,
            "double slash URL {} should resolve, got {}",
            path,
            page.status_code
        );
    }
}

#[tokio::test]
async fn double_slash_disallowed_variants() {
    if !run_live_tests() {
        return;
    }

    let paths = [
        "/urls/double_slash//disallowed_middle",
        "/urls/double_slash/disallowed_end//",
    ];

    for path in &paths {
        let page = fetch_page_http(path).await;
        assert!(
            page.status_code.is_success() || page.status_code == StatusCode::NOT_FOUND,
            "disallowed double slash {} : got {}",
            path,
            page.status_code
        );
    }
}

// --- Parameter variants ---

#[tokio::test]
async fn url_with_parameters() {
    if !run_live_tests() {
        return;
    }

    let paths = [
        "/urls/parameter_1_1?parameter_1=foo",
        "/urls/parameter_1_2?parameter_x=x&parameter_1=foo",
        "/urls/parameter_1_3?parameter_x=x&parameter_1=foo&parameter_y=y",
        "/urls/parameter_2_1?parameter_1=foo",
        "/urls/parameter_2_2?parameter_x=x&parameter_1=foo",
        "/urls/parameter_2_3?parameter_x=x&parameter_1=foo&parameter_y=y",
    ];

    for path in &paths {
        let page = fetch_page_http(path).await;
        assert!(
            page.status_code.is_success(),
            "URL with parameters {} should resolve, got {}",
            path,
            page.status_code
        );
    }
}

#[tokio::test]
async fn duplicate_parameter_values() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/parameter?parameter_1=foo&parameter_1=bar").await;
    assert!(
        page.status_code.is_success(),
        "duplicate param different values: got {}",
        page.status_code
    );

    let page2 = fetch_page_http("/urls/parameter?parameter_1=foo&parameter_1=foo").await;
    assert!(
        page2.status_code.is_success(),
        "duplicate param same values: got {}",
        page2.status_code
    );
}

// --- Spaces and encoding ---

#[tokio::test]
async fn url_with_spaces() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/url_with_spaces/URL%20with%20spaces").await;
    assert!(
        page.status_code.is_success(),
        "URL with encoded spaces should resolve, got {}",
        page.status_code
    );
}

#[tokio::test]
async fn url_with_trailing_space() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/url_with_trailing_space/%20").await;
    assert!(
        page.status_code.is_success(),
        "trailing space URL: got {}",
        page.status_code
    );
}

#[tokio::test]
async fn url_with_encoded_trailing_space() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/url_with_encoded_trailing_space/").await;
    assert!(
        page.status_code.is_success(),
        "encoded trailing space URL: got {}",
        page.status_code
    );
}

// --- Duplication types ---

#[tokio::test]
async fn url_with_trailing_slash_vs_without() {
    if !run_live_tests() {
        return;
    }

    let with_slash = fetch_page_http("/urls/duplication_types/").await;
    let without_slash = fetch_page_http("/urls/duplication_types").await;

    assert!(with_slash.status_code.is_success(), "with trailing slash");
    assert!(
        without_slash.status_code.is_success(),
        "without trailing slash"
    );
}

#[tokio::test]
async fn duplication_types_tracking() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/duplication_types?tracking=yes").await;
    assert!(
        page.status_code.is_success(),
        "duplication with tracking param: got {}",
        page.status_code
    );
}

#[tokio::test]
async fn duplication_types_index_html() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/duplication_types/index.html").await;
    assert!(
        page.status_code.is_success(),
        "duplication_types/index.html: got {}",
        page.status_code
    );
}

#[tokio::test]
async fn duplication_types_nested() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/duplication_types/duplication_types/").await;
    assert!(
        page.status_code.is_success(),
        "nested duplication_types: got {}",
        page.status_code
    );
}

#[tokio::test]
async fn duplication_types_uppercase() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/DUPLICATION_TYPES").await;
    assert!(
        page.status_code.is_success(),
        "uppercase DUPLICATION_TYPES: got {}",
        page.status_code
    );
}

// --- Fragment and encoding ---

#[tokio::test]
async fn url_with_fragment() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/url_with_fragment").await;
    assert!(
        page.status_code.is_success(),
        "URL with fragment should resolve"
    );
}

#[tokio::test]
async fn url_with_encoded_characters() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/url/URL%2c_with_encoded_reserved_character").await;
    assert!(
        page.status_code.is_success(),
        "URL with encoded characters should resolve, got {}",
        page.status_code
    );
}

#[tokio::test]
async fn url_with_encoded_unreserved_character() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/url/URL_with_encoded_unreserved_%63haracter").await;
    assert!(
        page.status_code.is_success(),
        "URL with encoded unreserved char: got {}",
        page.status_code
    );
}

#[tokio::test]
async fn url_with_encoded_space() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/url/with_encoded_space").await;
    assert!(
        page.status_code.is_success(),
        "URL with encoded space: got {}",
        page.status_code
    );
}

#[tokio::test]
async fn url_with_encoded_o_character() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/url/with_encoded_%C3%B3_character").await;
    assert!(
        page.status_code.is_success(),
        "URL with encoded ó: got {}",
        page.status_code
    );
}

#[tokio::test]
async fn url_with_colon() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/url/url_with:colon").await;
    assert!(
        page.status_code.is_success(),
        "URL with colon in path should resolve, got {}",
        page.status_code
    );
}

// --- Session ID ---

#[tokio::test]
async fn url_with_session_id() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/with_session_id?sessionID=5vSrxOoHw90aGk81xRa6").await;
    assert!(
        page.status_code.is_success(),
        "URL with session ID should resolve"
    );
}

// --- Directory index ---

#[tokio::test]
async fn directory_index_variants() {
    if !run_live_tests() {
        return;
    }

    let paths = [
        "/urls/directory_index/",
        "/urls/directory_index/index.html",
        "/urls/directory_index/index.htm",
        "/urls/directory_index/default.htm",
        "/urls/directory_index/index",
    ];

    for path in &paths {
        let page = fetch_page_http(path).await;
        assert!(
            page.status_code.is_success(),
            "directory index {} should resolve, got {}",
            path,
            page.status_code
        );
    }
}

// --- Malformed URLs ---

#[tokio::test]
async fn links_to_malformed_urls() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/links_to_malformed_urls").await;
    assert_eq!(page.status_code, StatusCode::OK);
}

// --- Pagination ---

#[tokio::test]
async fn paginated_pages() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/paginated_page").await;
    assert_eq!(page.status_code, StatusCode::OK);
    let html = page.get_html().to_lowercase();
    assert!(
        html.contains("rel=\"next\"") || html.contains("rel=\"prev\"") || html.contains("page"),
        "paginated page should have pagination markers"
    );
}

#[tokio::test]
async fn unlinked_paginated_page() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/unlinked_paginated_page").await;
    assert_eq!(page.status_code, StatusCode::OK);
}

#[tokio::test]
async fn paginated_and_noindex_page() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/paginated_and_noindex_page").await;
    assert_eq!(page.status_code, StatusCode::OK);
}

// --- Non-HTML file types ---

#[tokio::test]
async fn links_to_non_html_filetypes() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/links_to_non_html_filetypes").await;
    assert_eq!(page.status_code, StatusCode::OK);
    let html = page.get_html().to_lowercase();
    assert!(
        html.contains(".pdf") || html.contains(".jpg") || html.contains(".png"),
        "should contain links to non-HTML file types"
    );
}

// --- Hreflang ---

#[tokio::test]
async fn pages_with_hreflang() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/pages_with_hreflang").await;
    assert_eq!(page.status_code, StatusCode::OK);
    let html = page.get_html().to_lowercase();
    assert!(
        html.contains("hreflang"),
        "should contain hreflang attributes"
    );
}

#[tokio::test]
async fn page_with_hreflang_header_ok() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/page_with_hreflang_header_ok").await;
    assert_eq!(page.status_code, StatusCode::OK);
}

#[tokio::test]
async fn page_with_hreflang_header_not_ok() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/page_with_hreflang_header_not_ok").await;
    assert_eq!(page.status_code, StatusCode::OK);
}

// --- Duplicate pages ---

#[tokio::test]
async fn duplicate_pages() {
    if !run_live_tests() {
        return;
    }

    let paths = [
        "/urls/duplicate_page",
        "/urls/duplicate_page/foo",
        "/urls/duplicate_page/bar",
        "/urls/duplicate_page/baz",
    ];

    for path in &paths {
        let page = fetch_page_http(path).await;
        assert_eq!(
            page.status_code,
            StatusCode::OK,
            "duplicate_page {} should resolve",
            path
        );
    }
}

// --- Page URL length ---

#[tokio::test]
async fn page_url_length_n() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/urls/page_url_length_n").await;
    assert_eq!(page.status_code, StatusCode::OK);
}

// --- Multiple slashes ---

#[tokio::test]
async fn multiple_slashes() {
    if !run_live_tests() {
        return;
    }

    let paths = [
        "/urls/multiple_slashes///200_404",
        "/urls/multiple_slashes///404_200",
    ];

    for path in &paths {
        let page = fetch_page_http(path).await;
        let status = page.status_code.as_u16();
        assert!(
            status == 200 || status == 404,
            "multiple slashes URL {}: got {}",
            path,
            status
        );
    }
}

// --- Deep paths ---

#[tokio::test]
async fn deep_paths() {
    if !run_live_tests() {
        return;
    }

    let paths = [
        "/one/two/three/four",
        "/one/two/three/four/five",
        "/one/two/three/four/five/six",
        "/one/two/three/four/five/six/seven",
    ];

    for path in &paths {
        let page = fetch_page_http(path).await;
        assert!(
            page.status_code.is_success(),
            "deep path {} : got {}",
            path,
            page.status_code
        );
    }
}

#[tokio::test]
async fn path_segments() {
    if !run_live_tests() {
        return;
    }

    let paths = ["/path/1/path/2", "/path/1/path/2/path/3"];

    for path in &paths {
        let page = fetch_page_http(path).await;
        assert!(
            page.status_code.is_success(),
            "path {} : got {}",
            path,
            page.status_code
        );
    }
}

// --- Relative base source ---

#[tokio::test]
async fn relative_base_source() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/relative_base_source").await;
    assert!(
        page.status_code.is_success(),
        "relative_base_source: got {}",
        page.status_code
    );
}

// --- Parameter on hostname root ---

#[tokio::test]
async fn parameter_on_hostname_root() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/?parameter-on-hostname-root=parameter-value").await;
    assert!(
        page.status_code.is_success(),
        "parameter on root: got {}",
        page.status_code
    );
}

#[tokio::test]
async fn removed_and_retained_parameter() {
    if !run_live_tests() {
        return;
    }

    let page = fetch_page_http("/?removed_parameter=1&retained_parameter=1").await;
    assert!(
        page.status_code.is_success(),
        "removed/retained params: got {}",
        page.status_code
    );
}

// --- Infinite URL budget ---

#[tokio::test]
async fn infinite_urls_budget_caps_crawl() {
    if !run_live_tests() {
        return;
    }

    let budget = 5u32;
    let url = format!("{}/infinite/", BASE);
    let mut website = Website::new(&url);
    website
        .with_budget(Some(HashMap::from([("*", budget)])))
        .with_depth(3)
        .with_request_timeout(Some(std::time::Duration::from_secs(30)))
        .with_crawl_timeout(Some(std::time::Duration::from_secs(60)));

    let mut w = website.clone();
    let mut rx = w.subscribe(16).expect("subscribe");
    let (done_tx, mut done_rx) = spider::tokio::sync::oneshot::channel::<()>();

    let crawl = async move {
        w.crawl_raw().await;
        w.unsubscribe();
        let _ = done_tx.send(());
    };

    let mut urls = Vec::new();
    let sub = async {
        loop {
            spider::tokio::select! {
                biased;
                _ = &mut done_rx => break,
                result = rx.recv() => {
                    if let Ok(page) = result {
                        urls.push(page.get_url().to_string());
                    } else {
                        break;
                    }
                }
            }
        }
    };

    let crawl_result = spider::tokio::time::timeout(std::time::Duration::from_secs(90), async {
        spider::tokio::join!(sub, crawl)
    })
    .await;

    assert!(crawl_result.is_ok(), "infinite crawl should not hang");

    assert!(
        urls.len() <= (budget as usize) + 2,
        "budget should cap infinite crawl; got {} pages",
        urls.len()
    );
}

// --- Depth limit ---

#[tokio::test]
async fn depth_limit_respected() {
    if !run_live_tests() {
        return;
    }

    let pages = crawl_collect_http("/one/two/three/four", 10, 2).await;
    assert!(
        pages.len() <= 12,
        "depth limit should constrain crawl; got {} pages",
        pages.len()
    );
}

// --- Chrome ---

#[cfg(feature = "chrome")]
#[tokio::test]
async fn url_with_spaces_chrome() {
    if !run_live_tests() {
        return;
    }

    if let Some(page) = fetch_page_chrome("/urls/url_with_spaces/URL%20with%20spaces").await {
        assert!(
            !page.get_html().is_empty(),
            "chrome should handle URL with spaces"
        );
    } else {
        eprintln!("SKIP: chrome not available");
    }
}