#!/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;
$_SERVER['HTTP_HOST'] = "gna.ch:42";
is(
	it_url::absolute("/port"),
	'http://gna.ch:42/port',
	'it_url::absolute with non-standard port'
);

$_SERVER['HTTPS'] = true;
$_SERVER['HTTP_HOST'] = "gna.ch";
$_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'
);

require 'it_url_server.php';

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

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

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

handle_server(
	is(
		it_url::get('http://localhost:8000/relative_redirect'),
		"Testserver output after relative redirect",
		'TODO: it_url::get() follows relative redirect correctly',
	)
);

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

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

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

handle_server(
	is(
		it_url::get(U('http://localhost:8000/get_server_value', 'key' => 'HTTP_HOST')),
		'localhost:8000',
		'it_url::get() sets correct Host header',
	)
);

handle_server(
	is(
		it_url::get(U('http://localhost:8000/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://localhost:8000/get_server_value', 'key' => 'HTTP_USER_AGENT'))),
		'it_url::get() sets User-Agent containing Mozilla',
	)
);

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

?>