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
|
//! ref: composer/tests/Composer/Test/Util/Http/RequestProxyTest.php
use indexmap::IndexMap;
use shirabe::util::http::request_proxy::RequestProxy;
use shirabe_php_shim::{
CURLAUTH_BASIC, CURLOPT_NOPROXY, CURLOPT_PROXY, CURLOPT_PROXY_CAINFO, CURLOPT_PROXY_CAPATH,
CURLOPT_PROXYAUTH, CURLOPT_PROXYUSERPWD, PhpMixed,
};
fn curl_options(pairs: &[(i64, PhpMixed)]) -> IndexMap<i64, PhpMixed> {
pairs.iter().cloned().collect()
}
#[test]
fn test_factory_none() {
let proxy = RequestProxy::none();
// extension_loaded('curl') is always true in the php-shim.
let options = curl_options(&[(CURLOPT_PROXY, PhpMixed::String(String::new()))]);
assert_eq!(options, proxy.get_curl_options(&IndexMap::new()).unwrap());
assert!(proxy.get_context_options().is_none());
assert_eq!("", proxy.get_status(None).unwrap());
}
#[test]
fn test_factory_no_proxy() {
let proxy = RequestProxy::no_proxy();
let options = curl_options(&[(CURLOPT_PROXY, PhpMixed::String(String::new()))]);
assert_eq!(options, proxy.get_curl_options(&IndexMap::new()).unwrap());
assert!(proxy.get_context_options().is_none());
assert_eq!("excluded by no_proxy", proxy.get_status(None).unwrap());
}
#[test]
fn test_is_secure() {
let cases: Vec<(Option<&str>, bool)> = vec![
(Some("http://proxy.com:80"), false),
(Some("https://proxy.com:443"), true),
(None, false),
];
for (url, expected) in cases {
let proxy = RequestProxy::new(url.map(String::from), None, None, None);
assert_eq!(expected, proxy.is_secure());
}
}
#[test]
fn test_get_status_throws_on_bad_format_specifier() {
let proxy = RequestProxy::new(
Some("http://proxy.com:80".to_string()),
None,
None,
Some("http://proxy.com:80".to_string()),
);
assert!(proxy.get_status(Some("using proxy")).is_err());
}
#[test]
fn test_get_status() {
let format = "proxy (%s)";
let cases: Vec<(Option<&str>, Option<&str>, &str)> = vec![
(None, Some(format), ""),
(Some("http://proxy.com:80"), None, "http://proxy.com:80"),
(
Some("http://proxy.com:80"),
Some(format),
"proxy (http://proxy.com:80)",
),
];
for (url, format, expected) in cases {
let proxy = RequestProxy::new(url.map(String::from), None, None, url.map(String::from));
if format.is_none() {
// try with and without optional param
assert_eq!(expected, proxy.get_status(None).unwrap());
assert_eq!(expected, proxy.get_status(format).unwrap());
} else {
assert_eq!(expected, proxy.get_status(format).unwrap());
}
}
}
#[test]
fn test_get_curl_options() {
let cases: Vec<(Option<&str>, Option<&str>, IndexMap<i64, PhpMixed>)> = vec![
(
None,
None,
curl_options(&[(CURLOPT_PROXY, PhpMixed::String(String::new()))]),
),
(
Some("http://proxy.com:80"),
None,
curl_options(&[
(
CURLOPT_PROXY,
PhpMixed::String("http://proxy.com:80".to_string()),
),
(CURLOPT_NOPROXY, PhpMixed::String(String::new())),
]),
),
(
Some("http://proxy.com:80"),
Some("user:p%40ss"),
curl_options(&[
(
CURLOPT_PROXY,
PhpMixed::String("http://proxy.com:80".to_string()),
),
(CURLOPT_NOPROXY, PhpMixed::String(String::new())),
(CURLOPT_PROXYAUTH, PhpMixed::Int(CURLAUTH_BASIC)),
(
CURLOPT_PROXYUSERPWD,
PhpMixed::String("user:p%40ss".to_string()),
),
]),
),
];
for (url, auth, expected) in cases {
let proxy = RequestProxy::new(url.map(String::from), auth.map(String::from), None, None);
assert_eq!(expected, proxy.get_curl_options(&IndexMap::new()).unwrap());
}
}
#[test]
#[ignore]
fn test_get_curl_options_with_ssl() {
let mut cafile_opts: IndexMap<String, PhpMixed> = IndexMap::new();
cafile_opts.insert(
"cafile".to_string(),
PhpMixed::String("/certs/bundle.pem".to_string()),
);
let mut capath_opts: IndexMap<String, PhpMixed> = IndexMap::new();
capath_opts.insert("capath".to_string(), PhpMixed::String("/certs".to_string()));
let cases: Vec<(
&str,
Option<&str>,
IndexMap<String, PhpMixed>,
IndexMap<i64, PhpMixed>,
)> = vec![
(
"https://proxy.com:443",
None,
cafile_opts,
curl_options(&[
(
CURLOPT_PROXY,
PhpMixed::String("https://proxy.com:443".to_string()),
),
(CURLOPT_NOPROXY, PhpMixed::String(String::new())),
(
CURLOPT_PROXY_CAINFO,
PhpMixed::String("/certs/bundle.pem".to_string()),
),
]),
),
(
"https://proxy.com:443",
Some("user:p%40ss"),
capath_opts,
curl_options(&[
(
CURLOPT_PROXY,
PhpMixed::String("https://proxy.com:443".to_string()),
),
(CURLOPT_NOPROXY, PhpMixed::String(String::new())),
(CURLOPT_PROXYAUTH, PhpMixed::Int(CURLAUTH_BASIC)),
(
CURLOPT_PROXYUSERPWD,
PhpMixed::String("user:p%40ss".to_string()),
),
(CURLOPT_PROXY_CAPATH, PhpMixed::String("/certs".to_string())),
]),
),
];
for (url, auth, ssl_options, expected) in cases {
let proxy = RequestProxy::new(Some(url.to_string()), auth.map(String::from), None, None);
assert_eq!(expected, proxy.get_curl_options(&ssl_options).unwrap());
}
}
|