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

# Tests for url.class, currently only constructor's parser

# 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/',
	'$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,
	'',
	'$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'
);

$_SERVER['HTTP_HOST'] = "gna.ch";
is(
	it_url::absolute("/"),
	'http://gna.ch:/',
	'it_url::absolute basic'
);

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

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

$url = new it_url('http://www.gna.ch/');
$page = $url->get();
ok(
	strpos($page, '</html>'), # UTF8SAFE
	'$url->get with url in constructor'
);

$url = new it_url('http://bogus.url');
$page = $url->get('http://www.gna.ch/');
ok(
	strpos($page, '</html>'), # UTF8SAFE
	'$url->get(url) with url as string arg'
);

$url = new it_url('http://bogus.url');
$page = $url->get(array('url' => 'http://www.gna.ch/'));
ok(
	strpos($page, '</html>'), # UTF8SAFE
	'$url->get(\'url\' => url) with url as named arg'
);
is(
	$url->result,
	200,
	'$url->result = 200'
);
is(
	$url->headers['Connection'],
	'close',
	'$url->headers correctly set'
);

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

$server = proc_open(
	'php -S localhost:8000 ' . dirname($_SERVER['PHP_SELF']) . '/it_url.testserver.php',
	array(0 => fopen('/dev/null', 'r'), 1 => fopen('/dev/null', 'w'), 2 => array('pipe', 'w')),
	$pipes
);
usleep(100000);
stream_set_blocking($pipes[2], 0);
function server_output() {
	$result = array();
	while (($result[] = trim(fgets($GLOBALS['pipes'][2])))) {};
	return array_filter($result);
}

$res = is(
	it_url::get('http://localhost:8000/'),
	"Testserver root output",
	'it_url::get() static call with port',
);
$output = server_output();
if (!$res)
	diag($output);

$res = is(
	it_url::get('http://localhost:8000/temp_redirect'),
	"Testserver output after temporary redirect",
	'it_url::get() follows temproary redirect',
);
$output = server_output();
if (!$res)
	diag($output);

$res = is(
	it_url::get('http://localhost:8000/perm_redirect'),
	"Testserver output after permanent redirect",
	'it_url::get() follows permanent redirect',
);
$output = server_output();
if (!$res)
	diag($output);

$res = is(
	it_url::get('http://localhost:8000/relative_redirect'),
	"Testserver output after relative redirect",
	'TODO: it_url::get() follows relative redirect correctly',
);
$output = server_output();
if (!$res)
	diag($output);

$res = is(
	it_url::get('http://localhost:8000/nohost_redirect'),
	"Testserver output after nohost redirect",
	'TODO: it_url::get() follows redirect without host correctly',
);
$output = server_output();
if (!$res)
	diag($output);

$res = ok(
	!it_url::get(U('http://localhost:8000/redirect_loop', 'num' => 10)),
	'it_url::get() handles redirect loop',
);
$output = server_output();
$last_num = it::match('num=(\d+)', end($output));
$res2 = ok(
	$last_num == 5,
	'it_url::get() aborts redirect loop after 5 redirects',
);
if (!$res || !$res2)
	diag($output);

$res = ok(
	!it_url::get('url' => 'http://localhost:8000/does_not_exist', 'retries' => 4),
	'it_url::get() retries on empty response',
);
$output = server_output();
$res2 = ok(
	count(preg_grep('/^Got Request:/', $output)) == 5,
	'it_url::get() respects set retry count',
);
if (!$res || !$res2)
	diag($output);

$pages = it_url::get_multi('urls' => array('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');

proc_terminate($server);

?>