rust_scraper 1.0.0

Production-ready web scraper with Clean Architecture, TUI selector, and sitemap support
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
//! Crawler domain entities
//!
//! Core business entities for web crawling functionality.
//! Following Clean Architecture: pure domain logic, no framework dependencies.
//!
//! # Rules Applied
//!
//! - **err-thiserror-for-libraries**: `CrawlError` uses thiserror, NO reqwest/anyhow
//! - **api-builder**: `CrawlerConfig` with builder pattern
//! - **api-must-use**: `#[must_use]` on result structs
//! - **api-non-exhaustive**: `#[non_exhaustive]` for future evolution
//! - **clean-architecture-dependency-rule**: Domain NO depende de Infra (reqwest/anyhow)
//! - **security-ssrf-prevention**: URL parsing antes de comparación en `matches_pattern`

use thiserror::Error;
use url::Url;

/// Content type discovered during crawling
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ContentType {
    /// HTML page
    #[default]
    Html,
    /// XML document (including sitemaps)
    Xml,
    /// Plain text
    Text,
    /// Unknown or other content type
    Other,
}

/// A discovered URL during crawling
///
/// Note: Cannot derive `Copy` because `Url` is not `Copy`.
/// Following **own-borrow-over-clone**: We'll pass references where possible.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiscoveredUrl {
    /// The discovered URL
    pub url: Url,
    /// Depth in the crawl tree (0 = seed)
    pub depth: u8,
    /// Parent URL that led to this discovery
    pub parent_url: Url,
    /// Content type if known
    pub content_type: ContentType,
}

impl DiscoveredUrl {
    /// Create a new discovered URL
    #[must_use]
    pub fn new(url: Url, depth: u8, parent_url: Url, content_type: ContentType) -> Self {
        Self {
            url,
            depth,
            parent_url,
            content_type,
        }
    }

    /// Create a new discovered URL with default HTML content type
    #[must_use]
    pub fn html(url: Url, depth: u8, parent_url: Url) -> Self {
        Self {
            url,
            depth,
            parent_url,
            content_type: ContentType::Html,
        }
    }
}

/// Crawler configuration with builder pattern
///
/// Following **api-builder**: Provides fluent builder API.
/// Following **api-non-exhaustive**: Can evolve without breaking changes.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CrawlerConfig {
    /// Seed URL to start crawling from
    pub seed_url: Url,
    /// Maximum depth to crawl (0 = only seed)
    pub max_depth: u8,
    /// Maximum number of pages to crawl
    pub max_pages: usize,
    /// URL patterns to include (glob-style)
    pub include_patterns: Vec<String>,
    /// URL patterns to exclude (glob-style)
    pub exclude_patterns: Vec<String>,
    /// Concurrency level (number of parallel requests)
    pub concurrency: usize,
    /// Delay between requests in milliseconds (rate limiting)
    pub delay_ms: u64,
    /// User agent string
    pub user_agent: String,
    /// Timeout for each request in seconds
    pub timeout_secs: u64,
    /// Use sitemap for URL discovery (FASE 3)
    pub use_sitemap: bool,
    /// Explicit sitemap URL (auto-discovers if None)
    pub sitemap_url: Option<String>,
}

impl CrawlerConfig {
    /// Create a new config with seed URL
    ///
    /// Following **api-builder**: Returns builder for fluent configuration.
    pub fn builder(seed_url: Url) -> CrawlerConfigBuilder {
        CrawlerConfigBuilder::new(seed_url)
    }

    /// Create a new config with default values
    pub fn new(seed_url: Url) -> Self {
        Self {
            seed_url,
            max_depth: 3,
            max_pages: 100,
            include_patterns: Vec::new(),
            exclude_patterns: Vec::new(),
            concurrency: 3, // Hardware-aware: nproc - 1 for 4C CPU
            delay_ms: 500,  // Hardware-aware: 500ms for HDD
            user_agent: "rust-scraper/0.3.0 (Web Crawler)".to_string(),
            timeout_secs: 30,
            use_sitemap: false,
            sitemap_url: None,
        }
    }

    /// Check if a URL matches the include patterns
    #[inline]
    #[must_use]
    pub fn matches_include(&self, url: &str) -> bool {
        if self.include_patterns.is_empty() {
            return true;
        }
        self.include_patterns
            .iter()
            .any(|pattern| matches_pattern(url, pattern))
    }

    /// Check if a URL matches the exclude patterns
    #[inline]
    #[must_use]
    pub fn matches_exclude(&self, url: &str) -> bool {
        self.exclude_patterns
            .iter()
            .any(|pattern| matches_pattern(url, pattern))
    }
}

/// Builder for CrawlerConfig
///
/// Following **api-builder** and **api-must-use**.
#[derive(Debug)]
#[must_use]
pub struct CrawlerConfigBuilder {
    use_sitemap: bool,
    sitemap_url: Option<String>,
    seed_url: Url,
    max_depth: u8,
    max_pages: usize,
    include_patterns: Vec<String>,
    exclude_patterns: Vec<String>,
    concurrency: usize,
    delay_ms: u64,
    user_agent: String,
    timeout_secs: u64,
}

impl CrawlerConfigBuilder {
    /// Create a new builder with seed URL
    pub fn new(seed_url: Url) -> Self {
        Self {
            seed_url,
            max_depth: 3,
            max_pages: 100,
            include_patterns: Vec::new(),
            exclude_patterns: Vec::new(),
            concurrency: 3,
            delay_ms: 500,
            user_agent: "rust-scraper/0.3.0 (Web Crawler)".to_string(),
            timeout_secs: 30,
            use_sitemap: false,
            sitemap_url: None,
        }
    }

    /// Set maximum crawl depth
    pub fn max_depth(mut self, depth: u8) -> Self {
        self.max_depth = depth;
        self
    }

    /// Set maximum number of pages
    pub fn max_pages(mut self, pages: usize) -> Self {
        self.max_pages = pages;
        self
    }

    /// Add an include pattern
    pub fn include_pattern(mut self, pattern: impl Into<String>) -> Self {
        self.include_patterns.push(pattern.into());
        self
    }

    /// Add multiple include patterns
    pub fn include_patterns(mut self, patterns: Vec<String>) -> Self {
        self.include_patterns.extend(patterns);
        self
    }

    /// Add an exclude pattern
    pub fn exclude_pattern(mut self, pattern: impl Into<String>) -> Self {
        self.exclude_patterns.push(pattern.into());
        self
    }

    /// Add multiple exclude patterns
    pub fn exclude_patterns(mut self, patterns: Vec<String>) -> Self {
        self.exclude_patterns.extend(patterns);
        self
    }

    /// Set concurrency level
    pub fn concurrency(mut self, level: usize) -> Self {
        self.concurrency = level;
        self
    }

    /// Set delay between requests in milliseconds
    pub fn delay_ms(mut self, ms: u64) -> Self {
        self.delay_ms = ms;
        self
    }

    /// Set user agent string
    pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
        self.user_agent = ua.into();
        self
    }

    /// Set request timeout in seconds
    pub fn timeout_secs(mut self, secs: u64) -> Self {
        self.timeout_secs = secs;
        self
    }

    /// Set use_sitemap flag (FASE 3)
    pub fn use_sitemap(mut self, use_sitemap: bool) -> Self {
        self.use_sitemap = use_sitemap;
        self
    }

    /// Set explicit sitemap URL (FASE 3)
    pub fn sitemap_url(mut self, url: impl Into<String>) -> Self {
        self.sitemap_url = Some(url.into());
        self
    }

    #[must_use]
    pub fn build(self) -> CrawlerConfig {
        CrawlerConfig {
            seed_url: self.seed_url,
            max_depth: self.max_depth,
            max_pages: self.max_pages,
            include_patterns: self.include_patterns,
            exclude_patterns: self.exclude_patterns,
            concurrency: self.concurrency,
            delay_ms: self.delay_ms,
            user_agent: self.user_agent,
            timeout_secs: self.timeout_secs,
            use_sitemap: self.use_sitemap,
            sitemap_url: self.sitemap_url,
        }
    }
}

/// Crawl result containing discovered URLs
///
/// Following **api-must-use** and **api-non-exhaustive**.
#[derive(Debug, Clone, Default)]
#[must_use]
#[non_exhaustive]
pub struct CrawlResult {
    /// All discovered URLs
    pub urls: Vec<DiscoveredUrl>,
    /// Total number of pages crawled
    pub total_pages: usize,
    /// Number of errors encountered
    pub errors: usize,
}

impl CrawlResult {
    /// Create a new crawl result
    pub fn new(urls: Vec<DiscoveredUrl>, total_pages: usize, errors: usize) -> Self {
        Self {
            urls,
            total_pages,
            errors,
        }
    }

    /// Create an empty crawl result
    pub fn empty() -> Self {
        Self::default()
    }

    /// Check if the result is empty
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.urls.is_empty()
    }
}

/// Crawl errors
///
/// Following **err-thiserror-for-libraries**: Uses thiserror for library error types.
/// Following **api-non-exhaustive**: Can add variants without breaking changes.
/// Following **clean-architecture**: NO dependencies on reqwest/anyhow (Infra layer)
///
/// # Architecture Note
///
/// This error type does NOT contain `reqwest::Error` or `anyhow::Error`.
/// Those are infrastructure details. The Infrastructure layer converts
/// `reqwest::Error` → `CrawlError::Network` and `anyhow::Error` → specific variants.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CrawlError {
    /// Network error during HTTP request
    ///
    /// Note: Does NOT contain reqwest::Error (that's Infra detail).
    /// Infrastructure layer converts reqwest::Error → this variant.
    #[error("network error: {message} (status: {status_code:?})")]
    Network {
        message: String,
        status_code: Option<u16>,
    },

    /// HTTP error (status code or request failure)
    #[error("HTTP error: {0}")]
    Http(String),

    /// URL parsing error
    #[error("invalid URL: {0}")]
    InvalidUrl(String),

    /// HTML parsing error
    #[error("parse error: {0}")]
    Parse(String),

    /// Rate limit exceeded
    #[error("rate limit exceeded")]
    RateLimit,

    /// Maximum depth exceeded
    #[error("maximum depth {max} exceeded at depth {current}")]
    MaxDepthExceeded { current: u8, max: u8 },

    /// Maximum pages exceeded
    #[error("maximum pages {max} exceeded")]
    MaxPagesExceeded { max: usize },

    /// URL excluded by pattern
    #[error("URL excluded: {0}")]
    UrlExcluded(String),

    /// Invalid content type
    #[error("invalid content type: {0}")]
    InvalidContentType(String),

    /// I/O error
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// Semaphore error (concurrency control)
    #[error("semaphore error: {0}")]
    Semaphore(String),

    /// Internal error (unspecified)
    #[error("internal error: {0}")]
    Internal(String),

    /// Sitemap parsing error (FASE 3)
    /// Note: Does NOT contain sitemap_parser::SitemapError (that's Infra detail).
    /// Infrastructure layer converts SitemapError → this variant.
    #[error("sitemap error: {0}")]
    Sitemap(String),
}

/// Pattern matching helper function
///
/// Following **own-borrow-over-clone**: Accepts &str not &String.
/// Following **opt-inline**: Inlined for hot path performance.
/// Following **security-ssrf-prevention**: Parses URL before comparison (no .contains() on raw string)
///
/// # Security
///
/// This function parses the URL using `url::Url` and compares HOSTS only,
/// NOT raw string substrings. This prevents SSRF attacks where malicious
/// URLs like `https://evil.com/?q=example.com/path` could bypass filters.
///
/// # Examples
///
/// ```
/// use rust_scraper::domain::crawler_entities::matches_pattern;
///
/// // Valid subdomain match
/// assert!(matches_pattern("https://blog.example.com/post", "*.example.com/*"));
///
/// // SSRF bypass attempt (should NOT match)
/// assert!(!matches_pattern("https://evil.com/?q=example.com/path", "*.example.com/*"));
/// ```
#[inline]
#[must_use]
pub fn matches_pattern(url_str: &str, pattern: &str) -> bool {
    // Parse URL FIRST (extract real host)
    let url = match Url::parse(url_str) {
        Ok(u) => u,
        Err(_) => return false, // Invalid URL → no match
    };

    let host = match url.host_str() {
        Some(h) => h,
        None => return false, // No host → no match
    };

    // Handle empty pattern
    if pattern.is_empty() {
        return true;
    }

    // Handle wildcard
    if pattern == "*" {
        return true;
    }

    // Compare HOSTS only (NOT raw URL strings)
    match pattern {
        // *.example.com/* → match subdomain ONLY (not root domain)
        p if p.starts_with("*.") && p.ends_with("*") => {
            let domain = &p[2..p.len() - 1]; // "example.com/"
            let domain = domain.trim_end_matches('/');
            // Must be a subdomain, NOT the root domain itself
            host.ends_with(&format!(".{}", domain))
        }
        // *.example.com → match subdomain ONLY (not root domain)
        p if p.starts_with("*.") => {
            let domain = &p[2..];
            // Must be a subdomain, NOT the root domain itself
            host.ends_with(&format!(".{}", domain))
        }
        // Exact host match (no wildcard)
        p => host == p,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_discovered_url_new() {
        let url = Url::parse("https://example.com/page").unwrap();
        let parent = Url::parse("https://example.com/").unwrap();
        let discovered = DiscoveredUrl::new(url, 1, parent, ContentType::Html);

        assert_eq!(discovered.depth, 1);
        assert_eq!(discovered.content_type, ContentType::Html);
    }

    #[test]
    fn test_discovered_url_html() {
        let url = Url::parse("https://example.com/page").unwrap();
        let parent = Url::parse("https://example.com/").unwrap();
        let discovered = DiscoveredUrl::html(url, 0, parent);

        assert_eq!(discovered.depth, 0);
        assert_eq!(discovered.content_type, ContentType::Html);
    }

    #[test]
    fn test_crawler_config_builder() {
        let seed = Url::parse("https://example.com").unwrap();
        let config = CrawlerConfig::builder(seed)
            .max_depth(5)
            .max_pages(500)
            .concurrency(5)
            .delay_ms(1000)
            .include_pattern("*.example.com/*".to_string())
            .exclude_pattern("*/admin/*".to_string())
            .build();

        assert_eq!(config.max_depth, 5);
        assert_eq!(config.max_pages, 500);
        assert_eq!(config.concurrency, 5);
        assert_eq!(config.delay_ms, 1000);
        assert_eq!(config.include_patterns.len(), 1);
        assert_eq!(config.exclude_patterns.len(), 1);
    }

    #[test]
    fn test_crawler_config_default() {
        let seed = Url::parse("https://example.com").unwrap();
        let config = CrawlerConfig::new(seed);

        assert_eq!(config.max_depth, 3);
        assert_eq!(config.max_pages, 100);
        assert_eq!(config.concurrency, 3);
        assert_eq!(config.delay_ms, 500);
    }

    #[test]
    fn test_crawl_result_empty() {
        let result = CrawlResult::empty();
        assert!(result.is_empty());
        assert_eq!(result.total_pages, 0);
        assert_eq!(result.errors, 0);
    }

    #[test]
    fn test_crawl_result_new() {
        let url = Url::parse("https://example.com").unwrap();
        let parent = Url::parse("https://example.com/").unwrap();
        let discovered = DiscoveredUrl::html(url, 0, parent);
        let result = CrawlResult::new(vec![discovered], 1, 0);

        assert!(!result.is_empty());
        assert_eq!(result.total_pages, 1);
        assert_eq!(result.errors, 0);
        assert_eq!(result.urls.len(), 1);
    }

    // ========== CRAWL ERROR TESTS (NO reqwest/anyhow) ==========

    #[test]
    fn test_crawl_error_network_no_reqwest() {
        // Network error NO depende de reqwest
        let error = CrawlError::Network {
            message: "timeout".to_string(),
            status_code: Some(408),
        };
        assert!(error.to_string().contains("timeout"));
        assert!(error.to_string().contains("408"));
    }

    #[test]
    fn test_crawl_error_network_no_status() {
        let error = CrawlError::Network {
            message: "connection refused".to_string(),
            status_code: None,
        };
        assert!(error.to_string().contains("connection refused"));
        assert!(error.to_string().contains("None"));
    }

    #[test]
    fn test_crawl_error_io() {
        // Io error con std::io::Error
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let error = CrawlError::from(io_error);
        assert!(matches!(error, CrawlError::Io(_)));
        assert!(error.to_string().contains("file not found"));
    }

    #[test]
    fn test_crawl_error_semaphore() {
        let error = CrawlError::Semaphore("permit lost".to_string());
        assert!(error.to_string().contains("permit lost"));
    }

    #[test]
    fn test_crawl_error_internal() {
        let error = CrawlError::Internal("something went wrong".to_string());
        assert!(error.to_string().contains("something went wrong"));
    }

    #[test]
    fn test_crawl_error_display_all_variants() {
        // Test all error variants display correctly
        let error = CrawlError::InvalidUrl("bad-url".to_string());
        assert!(error.to_string().contains("bad-url"));

        let error = CrawlError::Parse("html parse failed".to_string());
        assert!(error.to_string().contains("html parse failed"));

        let error = CrawlError::RateLimit;
        assert_eq!(error.to_string(), "rate limit exceeded");

        let error = CrawlError::MaxDepthExceeded { current: 5, max: 3 };
        assert_eq!(error.to_string(), "maximum depth 3 exceeded at depth 5");

        let error = CrawlError::MaxPagesExceeded { max: 100 };
        assert_eq!(error.to_string(), "maximum pages 100 exceeded");

        let error = CrawlError::UrlExcluded("https://evil.com".to_string());
        assert!(error.to_string().contains("evil.com"));

        let error = CrawlError::InvalidContentType("image/png".to_string());
        assert!(error.to_string().contains("image/png"));
    }

    // ========== SSRF PREVENTION TESTS ==========

    #[test]
    fn test_matches_pattern_ssrf_bypass_attempt() {
        // ATAQUE: Evil URL con query params que contienen el dominio
        // Esto NO debe matchear
        assert!(!matches_pattern(
            "https://evil.com/?q=example.com/path",
            "*.example.com/*"
        ));

        assert!(!matches_pattern(
            "https://attacker.com/?redirect=example.com/admin",
            "*.example.com/*"
        ));

        // Another SSRF bypass attempt
        assert!(!matches_pattern(
            "https://malicious.com/redirect?url=example.com/secret",
            "*.example.com/*"
        ));
    }

    #[test]
    fn test_matches_pattern_real_subdomain() {
        // Subdominio real DEBE matchear
        assert!(matches_pattern(
            "https://blog.example.com/post",
            "*.example.com/*"
        ));

        assert!(matches_pattern(
            "https://sub.example.com/page",
            "*.example.com"
        ));

        // Multiple subdomain levels
        assert!(matches_pattern(
            "https://deep.sub.example.com/page",
            "*.example.com/*"
        ));
    }

    #[test]
    fn test_matches_pattern_with_port() {
        // URLs with ports should work
        // Note: example.com:8080 does NOT match *.example.com/* (needs subdomain)
        assert!(matches_pattern(
            "https://blog.example.com:8080/path",
            "*.example.com/*"
        ));

        assert!(matches_pattern(
            "https://blog.example.com:443/post",
            "*.example.com/*"
        ));
    }

    #[test]
    fn test_matches_pattern_ipv4() {
        // IPv4 debe funcionar
        assert!(matches_pattern(
            "http://192.168.1.1:8080/path",
            "192.168.1.1"
        ));
    }

    #[test]
    fn test_matches_pattern_ipv6() {
        // IPv6 debe funcionar
        assert!(matches_pattern("http://[::1]:8080/path", "[::1]"));
    }

    #[test]
    fn test_matches_pattern_invalid_url() {
        // URLs inválidas NO deben matchear
        assert!(!matches_pattern("not-a-url", "*.example.com/*"));
        assert!(!matches_pattern("://missing-scheme.com", "*"));
        assert!(!matches_pattern("", "*"));
    }

    #[test]
    fn test_matches_pattern_wildcard() {
        assert!(matches_pattern("https://example.com/page", "*"));
        assert!(matches_pattern("https://any.domain.com/page", "*"));
    }

    #[test]
    fn test_matches_pattern_empty() {
        assert!(matches_pattern("https://example.com", ""));
    }

    #[test]
    fn test_matches_pattern_no_match() {
        assert!(!matches_pattern("https://other.com/page", "example.com"));
        assert!(!matches_pattern("https://evil.com/page", "*.example.com/*"));
    }

    #[test]
    fn test_matches_pattern_exact_host() {
        assert!(matches_pattern("https://example.com/page", "example.com"));
        assert!(!matches_pattern(
            "https://sub.example.com/page",
            "example.com"
        ));
    }

    #[test]
    fn test_matches_pattern_prefix_wildcard() {
        // After SSRF fix, *.example.com/* matches SUBDOMAINS only, not root domain
        // Use "example.com/*" pattern to match the root domain itself
        assert!(matches_pattern(
            "https://blog.example.com/admin/users",
            "*.example.com/*"
        ));
        assert!(matches_pattern(
            "https://admin.example.com/users",
            "*.example.com/*"
        ));
        // Root domain does NOT match *.example.com/*
        assert!(!matches_pattern(
            "https://example.com/admin/users",
            "*.example.com/*"
        ));
    }

    #[test]
    fn test_matches_pattern_slash_wildcard() {
        // *.example.com/* matches any subdomain of example.com
        // Note: Compares HOSTS only, path is not considered
        assert!(matches_pattern(
            "https://blog.example.com/admin/users",
            "*.example.com/*"
        ));
        assert!(matches_pattern(
            "https://admin.example.com/users",
            "*.example.com/*"
        ));
        // Root domain does NOT match *.example.com/*
        assert!(!matches_pattern(
            "https://example.com/admin/users",
            "*.example.com/*"
        ));
    }
}