From e1828e884f7c5475eea92ec13191ebd9c20ede92 Mon Sep 17 00:00:00 2001 From: Nathan Gass Date: Tue, 16 Oct 2012 09:46:10 +0000 Subject: dry --- tests/it_url.t | 149 +++++++++++++++++++++--------------------------- tests/it_url_server.php | 33 +++++++++++ tests/it_url_slow.t | 53 ++++++----------- 3 files changed, 116 insertions(+), 119 deletions(-) create mode 100644 tests/it_url_server.php (limited to 'tests') diff --git a/tests/it_url.t b/tests/it_url.t index de1cd0b..45e882c 100755 --- a/tests/it_url.t +++ b/tests/it_url.t @@ -123,120 +123,99 @@ ok( '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); +require 'it_url_server.php'; -$res = 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/'), + "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/perm_redirect'), - "Testserver output after permanent redirect", - 'it_url::get() follows permanent redirect', +handle_server( + 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/relative_redirect'), - "Testserver output after relative redirect", - 'TODO: it_url::get() follows relative redirect correctly', +handle_server( + 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/nohost_redirect'), - "Testserver output after nohost redirect", - 'TODO: it_url::get() follows redirect without host correctly', +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 = 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 = handle_server( + 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( +if (!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 = handle_server( + 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( +if (!ok( count(preg_grep('/^Got Request:/', $output)) == 5, 'it_url::get() respects set retry count', -); -if (!$res || !$res2) +)) diag($output); -$res = is( - it_url::get(U('http://localhost:8000/get_server_value', 'key' => 'HTTP_HOST')), - 'localhost:8000', - 'TODO it_url::get() sets correct Host header', +handle_server( + is( + it_url::get(U('http://localhost:8000/get_server_value', 'key' => 'HTTP_HOST')), + 'localhost:8000', + 'TODO it_url::get() sets correct Host header', + ) ); -$output = server_output(); -if (!$res) - diag($output); -$res = 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( + 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', + ) ); -$output = server_output(); -if (!$res) - diag($output); -$res = 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', +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', + ) ); -$output = server_output(); -if (!$res) - diag($output); $pages = it_url::get_multi('urls' => array('a' => 'http://www.gna.ch/', 'b' => 'http://search.ch/')); ok(strpos($pages['a'], ''), 'it_url::get_multi got first url'); # UTF8SAFE ok(strpos($pages['b'], ''), 'it_url::get_multi got second url'); # UTF8SAFE is(count($pages), 2, 'it_url::get_multi no additional array elements'); -proc_terminate($server); - ?> diff --git a/tests/it_url_server.php b/tests/it_url_server.php new file mode 100644 index 0000000..a407ab5 --- /dev/null +++ b/tests/it_url_server.php @@ -0,0 +1,33 @@ + fopen('/dev/null', 'r'), 1 => fopen('/dev/null', 'w'), 2 => array('pipe', 'w')), + $pipes +); +register_shutdown_function( + function ($server) { + proc_terminate($server); + }, + $server +); + +usleep(100000); +stream_set_blocking($pipes[2], 0); + +function server_output() { + $result = array(); + while (($result[] = trim(fgets($GLOBALS['pipes'][2])))) {}; + return array_filter($result); +} + +function handle_server($args) { + $res = true; + foreach ((array)$args as $arg) + $res = $res && $arg; + $output = server_output(); + if (!$res) + diag($output); + else + return $output; +} + diff --git a/tests/it_url_slow.t b/tests/it_url_slow.t index 215528d..74440ce 100755 --- a/tests/it_url_slow.t +++ b/tests/it_url_slow.t @@ -5,27 +5,15 @@ # TESTFLAGS SLOW -$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); -} +require 'it_url_server.php'; -$res = is( - it_url::get('http://localhost:8000/short_sleep'), - "Testserver output after short sleep", - 'it_url::get() waits for 4 seconds', +handle_server( + is( + it_url::get('http://localhost:8000/short_sleep'), + "Testserver output after short sleep", + 'it_url::get() waits for 4 seconds', + ) ); -$output = server_output(); -if (!$res) - diag($output); $start = microtime(true); $res = ok( @@ -41,14 +29,13 @@ $output = server_output(); if (!$res || !$res2) diag($output); -$res = is( - it_url::get('http://localhost:8000/slow_response'), - join('', it::map('"Testserver slow output $v\n"', range(0, 5))), - 'it_url::get() waits for slow response with continuous output', +handle_server( + is( + it_url::get('http://localhost:8000/slow_response'), + join('', it::map('"Testserver slow output $v\n"', range(0, 5))), + 'it_url::get() waits for slow response with continuous output', + ) ); -$output = server_output(); -if (!$res) - diag($output); $start = microtime(true); $res = ok( @@ -59,17 +46,15 @@ $res2 = is( floor(microtime(true) - $start), 5, '... and fails as soon as totaltimeout is surpassed' ); +sleep(1); # wait for testserver $output = server_output(); if (!$res || !$res2) diag($output); -$res = ok( - it_url::get(U('http://localhost:8000/repeat', 'string' => "abc", 'num' => 1024 * 1024)) == str_repeat("abc", 1024 * 1024), - 'it_url::get() handles large response', +handle_server( + ok( + it_url::get(U('http://localhost:8000/repeat', 'string' => "abc", 'num' => 1024 * 1024)) == str_repeat("abc", 1024 * 1024), + 'it_url::get() handles large response', + ) ); -$output = server_output(); -if (!$res) - diag($output); - -proc_terminate($server); -- cgit v1.2.3