aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/util/no_proxy_pattern_test.rs
blob: 51017ea8fedbf92d4896e4fe37f341f497452391 (plain)
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
//! ref: composer/tests/Composer/Test/Util/NoProxyPatternTest.php

use shirabe::util::no_proxy_pattern::NoProxyPattern;

fn run_test(noproxy: &str, url: &str, expected: bool) {
    let mut matcher = NoProxyPattern::new(noproxy);
    let url = get_url(url);
    assert_eq!(expected, matcher.test(&url).unwrap());
}

/// Appends a scheme to the test url if it is missing.
fn get_url(url: &str) -> String {
    if url.contains("://") {
        return url.to_string();
    }

    let mut scheme = "http";

    if !url.starts_with('[') && url.rfind(':').is_some() {
        let port = url.split(':').nth(1).unwrap_or("");

        if port == "443" {
            scheme = "https";
        }
    }

    format!("{}://{}", scheme, url)
}

#[test]
fn test_host_name() {
    let noproxy = "foobar.com, .barbaz.net";

    run_test(noproxy, "foobar.com", true);
    run_test(noproxy, "www.foobar.com", true);
    run_test(noproxy, "foofoobar.com", false);
    run_test(noproxy, "barbaz.net", true);
    run_test(noproxy, "www.barbaz.net", true);
    run_test(noproxy, "barbarbaz.net", false);
    run_test(noproxy, "barbaz.com", false);
    run_test(noproxy, "foobar.com.", false);
}

#[test]
#[ignore]
fn test_ip_address() {
    let noproxy = "192.168.1.1, 2001:db8::52:0:1";

    run_test(noproxy, "192.168.1.1", true);
    run_test(noproxy, "192.168.1.4", false);
    run_test(noproxy, "[2001:db8:0:0:0:52:0:1]", true);
    run_test(noproxy, "[2001:db8:0:0:0:52:0:2]", false);
    run_test(noproxy, "[::FFFF:C0A8:0101]", true);
    run_test(noproxy, "[::FFFF:C0A8:0104]", false);
}

#[test]
#[ignore]
fn test_ip_range() {
    let noproxy = "10.0.0.0/30, 2002:db8:a::45/121";

    run_test(noproxy, "10.0.0.2", true);
    run_test(noproxy, "10.0.0.4", false);
    run_test(noproxy, "[2002:db8:a:0:0:0:0:7f]", true);
    run_test(noproxy, "[2002:db8:a:0:0:0:0:ff]", false);
    run_test(noproxy, "[::FFFF:0A00:0002]", true);
    run_test(noproxy, "[::FFFF:0A00:0004]", false);
}

#[test]
fn test_port() {
    let noproxy = "192.168.1.2:81, 192.168.1.3:80, [2001:db8::52:0:2]:443, [2001:db8::52:0:3]:80";

    run_test(noproxy, "192.168.1.3", true);
    run_test(noproxy, "192.168.1.2", false);
    run_test(noproxy, "[2001:db8::52:0:3]", true);
    run_test(noproxy, "[2001:db8::52:0:2]", false);
}