aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/util/http/proxy_item_test.rs
blob: a8c0438e446e31c46f99057732bf60aabb948766 (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
//! ref: composer/tests/Composer/Test/Util/Http/ProxyItemTest.php

use shirabe::util::http::proxy_item::ProxyItem;

#[test]
fn test_throws_on_malformed_url() {
    for url in data_malformed() {
        assert!(ProxyItem::new(url.to_string(), "http_proxy".to_string()).is_err());
    }
}

fn data_malformed() -> Vec<&'static str> {
    vec![
        // 'ws-r'
        "http://user\rname@localhost:80",
        // 'ws-n'
        "http://user\nname@localhost:80",
        // 'ws-t'
        "http://user\tname@localhost:80",
        // 'no-host'
        "localhost",
        // 'no-port'
        "scheme://localhost",
        // 'port-0'
        "http://localhost:0",
        // 'port-big'
        "http://localhost:65536",
    ]
}

#[test]
#[ignore = "URL parsing yields no host for these cases, so proxy formatting fails (parse_url/port discrepancy)"]
fn test_url_formatting() {
    for (url, expected) in data_formatting() {
        let proxy_item = ProxyItem::new(url.to_string(), "http_proxy".to_string()).unwrap();
        let proxy = proxy_item.to_request_proxy("http".to_string());

        assert_eq!(expected, proxy.get_status(None).unwrap());
    }
}

fn data_formatting() -> Vec<(&'static str, &'static str)> {
    // url, expected
    vec![
        // 'none'
        ("http://proxy.com:8888", "http://proxy.com:8888"),
        // 'lowercases-scheme'
        ("HTTP://proxy.com:8888", "http://proxy.com:8888"),
        // 'adds-http-scheme'
        ("proxy.com:80", "http://proxy.com:80"),
        // 'adds-http-port'
        ("http://proxy.com", "http://proxy.com:80"),
        // 'adds-https-port'
        ("https://proxy.com", "https://proxy.com:443"),
        // 'removes-user'
        ("http://user@proxy.com:6180", "http://***@proxy.com:6180"),
        // 'removes-user-pass'
        (
            "http://user:p%40ss@proxy.com:6180",
            "http://***:***@proxy.com:6180",
        ),
    ]
}