summaryrefslogtreecommitdiff
path: root/test/it_url.t
blob: b141670b8af1a09d2be1f5a9e9e69b31f8582c2a (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
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
#!/www/server/bin/php -qC
<?php

require 'it_url_server.php';

it::getopt("Usage: it_url.t [OPTIONS]");

# Create object and parse url
$url = new it_url('HTTP://falcon:joshua@www.Relog.CH:80/default.asp');

is(
	$url->url,
	'http://www.relog.ch/default.asp',
	'$url->url'
);

is(
	$url->protocol,
	'http',
	'$url->protocol'
);

is(
	$url->hostname,
	'relog.ch',
	'$url->hostname'
);

is(
	$url->realhostname,
	'www.relog.ch',
	'$url->realhostname'
);

is(
	$url->port,
	80,
	'$url->port'
);

is(
	$url->path,
	'default.asp',
	'$url->path'
);

is(
	$url->user,
	'falcon',
	'$url->user'
);

is(
	$url->pass,
	'joshua',
	'$url->pass'
);

# and now check for path
$url = new it_url('HTTP://falcon:joshua@www.Relog.CH:80/foo/bar.html');

is(
	$url->path,
	'foo/bar.html',
	'$url->path'
);

# punycode test
$url = new it_url('http://www.sörtsch.ch/?q=ültra#sörtsch');
is(
	$url->realhostname,
	'www.xn--srtsch-wxa.ch',
	'punycode $url->realhostname'
);

# it_url::absolute() tests
$php_self = $_SERVER['PHP_SELF'];
$_SERVER['PHP_SELF'] = '/foo/bar/self.php';
list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null];
is(
	it_url::absolute("/"),
	'http://gna.ch/',
	'it_url::absolute basic'
);

is(
	it_url::absolute("qux"),
	'http://gna.ch/foo/bar/qux',
	'it_url::absolute relative path'
);

is(
	it_url::absolute("qux", 'https', '/prefix'),
	'https://gna.ch/prefix/foo/bar/qux',
	'it_url::absolute relative path'
);

list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch:42", 42, null];
is(
	it_url::absolute("/port"),
	'http://gna.ch:42/port',
	'it_url::absolute with non-standard port'
);

list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", 443, true];
is(
	it_url::absolute("/https"),
	'https://gna.ch/https',
	'it_url::absolute for https'
);

list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null];
is(
	it_url::absolute("/https", "https"),
	'https://gna.ch/https',
	'it_url::absolute force https in http context'
);

list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", 443, true];
is(
	it_url::absolute("/foo", "http"),
	'http://gna.ch/foo',
	'it_url::absolute force http in https context'
);

list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null];
is(
	it_url::absolute("http://gna.ch/foo", "https"),
	'https://gna.ch/foo',
	'it_url::absolute force https overwriting existing url'
);
$_SERVER['PHP_SELF'] = $php_self;

is(it_url::is_reachable('http://www.gna.ch/'), true, "is_reachable('http://www.gna.ch/')");
is(it_url::is_reachable('http://www.search.ch/not_found'), false, "is_reachable('http://www.search.ch/not_found')");
is(it_url::is_reachable('http://bogus.url'), false, "is_reachable('http://bogus.url')");
is(it_url::is_reachable(U("http://$host/redirect_loop", ['num' => 40])), false, "redirect loop is not reachable");

$response = it_url::get(['url' => 'http://www.gna.ch/', 'assoc' => true]);
ok(
	strpos($response['data'], '</html>'), # UTF8SAFE
	'Get with url as named arg'
);
is(
	$response['status'],
	'200',
	'Reponse status = 200'
);
is(
	$response['headers']['Server'],
	'Apache',
	'Response headers correctly set'
);

$page = it_url::get('http://www.gna.ch/');
ok(
	strpos($page, '</html>'), # UTF8SAFE
	'it_url::get() static call'
);

handle_server(
	is(
		it_url::get("http://$host/"),
		"Testserver root output",
		'it_url::get() static call with port'
	)
);

handle_server(
	is(
		it_url::get("//$host/"),
		"Testserver root output",
		'it_url::get() static call with port but no protocol'
	)
);

handle_server(
	is(
		it_url::get(['url' => "http://$host/", 'maxlength' => 100]),
		"Testserver root output",
		'it_url::get() static call with port and maxlength'
	)
);

handle_server(
	is(
		it_url::get(['url' => "http://$host/", 'maxlength' => 5, 'it_error' => false]),
		false,
		'it_url::get() static call with port and too small maxlength'
	)
);

handle_server(
	is(
		it_url::get("http://$host/temp_redirect"),
		"Testserver output after temporary redirect",
		'it_url::get() follows temproary redirect'
	)
);

handle_server(
	is(
		it_url::get("http://$host/perm_redirect"),
		"Testserver output after permanent redirect",
		'it_url::get() follows permanent redirect'
	)
);

handle_server(
	is(
		it_url::get(['url' => "http://$host/relative_redirect", 'it_error' => false]),
		"Testserver output after relative redirect",
		'it_url::get() follows relative redirect correctly'
	)
);

handle_server(
	is(
		it_url::get("http://$host/nohost_redirect"),
		"Testserver output after nohost redirect",
		'it_url::get() follows redirect without host correctly'
	)
);

handle_server(
	is(
		it_url::get("http://$host/created_redirect"),
		"Testserver output *before* created redirect",
		'it_url::get() does not follow Location of 201 (Created) result'
	)
);

$output = handle_server(
	ok(
		!it_url::get(['url' => U("http://$host/redirect_loop", ['num' => 40]), 'it_error' => false]),
		'it_url::get() handles redirect loop'
	)
);
$last_num = it::match('num=(\d+)', end($output));
if (!ok(
	$last_num == 20,
	'it_url::get() aborts redirect loop after 5 redirects'
))
	diag($output);

$output = handle_server(
	ok(
		!it_url::get(['url' => "http://$host/does_not_exist", 'retries' => 4]),
		'it_url::get() on 404'
	)
);
if (!ok(
	count(preg_grep('/^Got Request:/', $output)) == 1,
	'it_url::get() does not retry on 404'
))
	diag($output);

$output = handle_server(
	ok(
		it_url::get(['url' => "http://$host/not_found_with_body", 'empty_on_fail' => false, 'it_error' => false]),
		'it_url::get() on 404 with body'
	)
);

$output = handle_server(
	ok(
		!it_url::get(['url' => "http://$host/not_found_with_body", 'empty_on_fail' => true, 'it_error' => false]),
		'it_url::get() on 404 with body and empty_on_fail'
	)
);

$output = handle_server(
	is(
		it::filter_keys(it_url::get(['url' => "http://$host/not_found_with_body", 'empty_on_fail' => true, 'it_error' => false, 'assoc' => true]), 'status,data'),
		[],
		'it_url::get() on 404 with body, empty_on_fail and assoc'
	)
);

$output = handle_server(
	ok(
		!it_url::get(['url' => "http://$host/repeat?num=0", 'retries' => 4]),
		'it_url::get() on empty page'
	)
);
if (!ok(
	count(preg_grep('/^Got Request:/', $output)) == 5,
	'it_url::get() does retry on empty page'
))
	diag($output);

$output = handle_server(
	is(
		it_url::get(U("http://$host/empty")),
		'',
		'it_url::get() explicit empty result'
	)
);
if (!ok(
	count(preg_grep('/^Got Request:/', $output)) == 1,
	'it_url::get() does not retry on explicity empty page'
))
	diag($output);

handle_server(
	is(
		it_url::get(U("http://$host/get_server_value", ['key' => 'HTTP_HOST'])),
		$host,
		'it_url::get() sets correct Host header'
	)
);

handle_server(
	is(
		it_url::get(U("http://$host/get_server_value", ['key' => 'HTTP_ACCEPT_LANGUAGE'])),
		T_lang(),
		'it_url::get() sets correct Accept-Language header'
	)
);

handle_server(
	ok(
		it::match('Mozilla', it_url::get(U("http://$host/get_server_value", ['key' => 'HTTP_USER_AGENT']))),
		'it_url::get() sets User-Agent containing Mozilla'
	)
);

handle_server(
	is(
		it_url::get(U("http://user:password@$host/get_server_value", ['key' => 'PHP_AUTH_USER'])),
		'user',
		'it_url::get() basic authentication user'
	)
);

handle_server(
	is(
		it_url::get(U("http://user:password@$host/get_server_value", ['key' => 'PHP_AUTH_PW'])),
		'password',
		'it_url::get() basic authentication password'
	)
);

$pages = it_url::get_multi(['urls' => ['a' => 'http://www.gna.ch/', 'b' => 'http://search.ch/']]);
ok(strpos($pages['a'], '</html>'), 'it_url::get_multi got first url'); # UTF8SAFE
ok(strpos($pages['b'], '</html>'), 'it_url::get_multi got second url'); # UTF8SAFE
is(count($pages), 2, 'it_url::get_multi no additional array elements');

handle_server(
	is(
		it_url::get(['url' => "http://$host/maybe_error?chance=0"]),
		"success"
	),
	is(
		it_url::get(['url' => "http://$host/maybe_error?chance=100", 'empty_on_fail' => false]),
		"failure"
	),
	is(
		it_url::get(['url' => "http://$host/maybe_error?chance=10", 'empty_on_fail' => true, 'retries' => 10]),
		"success",
		"Retry on sporadically failing url in ::get"
	),
	is(
		it_url::get_multi(['urls' => ["http://$host/maybe_error?chance=10"], 'retries' => 10]),
		["success"],
		"Retry on sporadically failing url in ::get_multi"
	)
);

is(it_url::parse("/foo"), ["/foo"], "it_url::parse path only");
is(it_url::parse("/foo?"), ["/foo"], "it_url::parse empty parameter");
is(it_url::parse("/foo?bar=baz&qux=quux"), ["/foo", 'bar' => "baz", 'qux' => "quux"], "it_url::parse parameters");
is(it_url::parse("/foo?b%E4r=b%E4z"), ["/foo", 'bär' => "bäz"]);