aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/util/url_test.rs
blob: 3691d267be0bc1c3fd06e03870dd2af1147bd233 (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
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
//! ref: composer/tests/Composer/Test/Util/UrlTest.php

use indexmap::IndexMap;
use shirabe::config::Config;
use shirabe::util::url::Url;
use shirabe_php_shim::PhpMixed;

fn conf(entries: &[(&str, &[&str])]) -> IndexMap<String, PhpMixed> {
    entries
        .iter()
        .map(|(k, vals)| {
            (
                k.to_string(),
                PhpMixed::List(
                    vals.iter()
                        .map(|v| PhpMixed::String(v.to_string()))
                        .collect(),
                ),
            )
        })
        .collect()
}

#[test]
#[ignore]
fn test_update_dist_reference() {
    for (url, expected_url, c, r#ref) in dist_refs_provider() {
        let mut config = Config::new(true, None);
        let mut merged: IndexMap<String, PhpMixed> = IndexMap::new();
        merged.insert("config".to_string(), PhpMixed::Array(c));
        config.merge(&merged, Config::SOURCE_UNKNOWN);

        assert_eq!(
            expected_url,
            Url::update_dist_reference(&config, url.to_string(), r#ref)
        );
    }
}

fn dist_refs_provider() -> Vec<(
    &'static str,
    &'static str,
    IndexMap<String, PhpMixed>,
    &'static str,
)> {
    vec![
        // github
        (
            "https://github.com/foo/bar/zipball/abcd",
            "https://api.github.com/repos/foo/bar/zipball/newref",
            conf(&[]),
            "newref",
        ),
        (
            "https://www.github.com/foo/bar/zipball/abcd",
            "https://api.github.com/repos/foo/bar/zipball/newref",
            conf(&[]),
            "newref",
        ),
        (
            "https://github.com/foo/bar/archive/abcd.zip",
            "https://api.github.com/repos/foo/bar/zipball/newref",
            conf(&[]),
            "newref",
        ),
        (
            "https://github.com/foo/bar/archive/abcd.tar.gz",
            "https://api.github.com/repos/foo/bar/tarball/newref",
            conf(&[]),
            "newref",
        ),
        (
            "https://api.github.com/repos/foo/bar/tarball",
            "https://api.github.com/repos/foo/bar/tarball/newref",
            conf(&[]),
            "newref",
        ),
        (
            "https://api.github.com/repos/foo/bar/tarball/abcd",
            "https://api.github.com/repos/foo/bar/tarball/newref",
            conf(&[]),
            "newref",
        ),
        // github enterprise
        (
            "https://mygithub.com/api/v3/repos/foo/bar/tarball/abcd",
            "https://mygithub.com/api/v3/repos/foo/bar/tarball/newref",
            conf(&[("github-domains", &["mygithub.com"])]),
            "newref",
        ),
        // bitbucket
        (
            "https://bitbucket.org/foo/bar/get/abcd.zip",
            "https://bitbucket.org/foo/bar/get/newref.zip",
            conf(&[]),
            "newref",
        ),
        (
            "https://www.bitbucket.org/foo/bar/get/abcd.tar.bz2",
            "https://bitbucket.org/foo/bar/get/newref.tar.bz2",
            conf(&[]),
            "newref",
        ),
        // gitlab
        (
            "https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=abcd",
            "https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=newref",
            conf(&[]),
            "newref",
        ),
        (
            "https://www.gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=abcd",
            "https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=newref",
            conf(&[]),
            "newref",
        ),
        (
            "https://gitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.gz?sha=abcd",
            "https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.tar.gz?sha=newref",
            conf(&[]),
            "newref",
        ),
        // gitlab enterprise
        (
            "https://mygitlab.com/api/v4/projects/foo%2Fbar/repository/archive.tar.gz?sha=abcd",
            "https://mygitlab.com/api/v4/projects/foo%2Fbar/repository/archive.tar.gz?sha=newref",
            conf(&[("gitlab-domains", &["mygitlab.com"])]),
            "newref",
        ),
        (
            "https://mygitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.bz2?sha=abcd",
            "https://mygitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.bz2?sha=newref",
            conf(&[("gitlab-domains", &["mygitlab.com"])]),
            "newref",
        ),
        (
            "https://mygitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.bz2?sha=abcd",
            "https://mygitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.bz2?sha=65",
            conf(&[("gitlab-domains", &["mygitlab.com"])]),
            "65",
        ),
    ]
}

#[test]
fn test_sanitize() {
    for (expected, url) in sanitize_provider() {
        assert_eq!(expected, Url::sanitize(url.to_string()));
    }
}

fn sanitize_provider() -> Vec<(&'static str, &'static str)> {
    vec![
        // with scheme
        (
            "https://foo:***@example.org/",
            "https://foo:bar@example.org/",
        ),
        ("https://foo@example.org/", "https://foo@example.org/"),
        ("https://example.org/", "https://example.org/"),
        (
            "http://***:***@example.org",
            "http://10a8f08e8d7b7b9:foo@example.org",
        ),
        (
            "https://foo:***@example.org:123/",
            "https://foo:bar@example.org:123/",
        ),
        (
            "https://example.org/foo/bar?access_token=***",
            "https://example.org/foo/bar?access_token=abcdef",
        ),
        (
            "https://example.org/foo/bar?foo=bar&access_token=***",
            "https://example.org/foo/bar?foo=bar&access_token=abcdef",
        ),
        (
            "https://***:***@github.com/acme/repo",
            "https://ghp_1234567890abcdefghijklmnopqrstuvwxyzAB:x-oauth-basic@github.com/acme/repo",
        ),
        (
            "https://***:***@github.com/acme/repo",
            "https://github_pat_1234567890abcdefghijkl_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW:x-oauth-basic@github.com/acme/repo",
        ),
        // without scheme
        ("foo:***@example.org/", "foo:bar@example.org/"),
        ("foo@example.org/", "foo@example.org/"),
        ("example.org/", "example.org/"),
        ("***:***@example.org", "10a8f08e8d7b7b9:foo@example.org"),
        ("foo:***@example.org:123/", "foo:bar@example.org:123/"),
        (
            "example.org/foo/bar?access_token=***",
            "example.org/foo/bar?access_token=abcdef",
        ),
        (
            "example.org/foo/bar?foo=bar&access_token=***",
            "example.org/foo/bar?foo=bar&access_token=abcdef",
        ),
    ]
}