From 46a1e3392bb9ec95c73cfb832e30642c855840de Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 17 Feb 2014 22:31:51 -0500 Subject: [PATCH] Introduces support for the baked in PHP HTTP Server in 5.4 Pulled in from @zbuc s work in his proposed logger pull request Requires PHP 5.4 but this can now be useful for integration testing --- tests/Httpful/HttpfulTest.php | 35 +++++++++++++++++++++++++++------ tests/bootstrap-server.php | 37 +++++++++++++++++++++++++++++++++++ tests/phpunit.xml | 15 ++++++++++---- tests/static/test.json | 1 + 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 tests/bootstrap-server.php create mode 100644 tests/static/test.json diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 1659098..bb74912 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -19,9 +19,11 @@ use Httpful\Response; use Httpful\Handlers\JsonHandler; +define('TEST_SERVER', WEB_SERVER_HOST . ':' . WEB_SERVER_PORT); + class HttpfulTest extends \PHPUnit_Framework_TestCase { - const TEST_SERVER = '127.0.0.1:8008'; + const TEST_SERVER = TEST_SERVER; const TEST_URL = 'http://127.0.0.1:8008'; const TEST_URL_400 = 'http://127.0.0.1:8008/400'; @@ -492,18 +494,21 @@ public function testOverrideXmlHandler() $this->assertNotEquals($prev, $new); } - public function testHasProxyWithoutProxy() { + public function testHasProxyWithoutProxy() + { $r = Request::get('someUrl'); $this->assertFalse($r->hasProxy()); } - public function testHasProxyWithProxy() { + public function testHasProxyWithProxy() + { $r = Request::get('some_other_url'); $r->useProxy('proxy.com'); $this->assertTrue($r->hasProxy()); } - public function testParseJSON() { + public function testParseJSON() + { $handler = new JsonHandler(); $bodies = array( @@ -524,10 +529,28 @@ public function testParseJSON() { } $this->fail('Expected an exception to be thrown due to invalid json'); } + + // /** + // * Skeleton for testing against the 5.4 baked in server + // */ + // public function testLocalServer() + // { + // if (!defined('WITHOUT_SERVER') || (defined('WITHOUT_SERVER') && !WITHOUT_SERVER)) { + // // PHP test server seems to always set content type to application/octet-stream + // // so force parsing as JSON here + // Httpful::register('application/octet-stream', new \Httpful\Handlers\JsonHandler()); + // $response = Request::get(TEST_SERVER . '/test.json') + // ->sendsAndExpects(MIME::JSON); + // $response->send(); + // $this->assertTrue(...); + // } + // } } -class DemoMimeHandler extends \Httpful\Handlers\MimeHandlerAdapter { - public function parse($body) { +class DemoMimeHandler extends \Httpful\Handlers\MimeHandlerAdapter +{ + public function parse($body) + { return 'custom parse'; } } diff --git a/tests/bootstrap-server.php b/tests/bootstrap-server.php new file mode 100644 index 0000000..51a609e --- /dev/null +++ b/tests/bootstrap-server.php @@ -0,0 +1,37 @@ +./server.log 2>&1 & echo $!', WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT); + + // Execute the command and store the process ID + $output = array(); + exec($command, $output, $exit_code); + + // sleep for a second to let server come up + sleep(1); + $pid = (int) $output[0]; + + // check server.log to see if it failed to start + $server_logs = file_get_contents("./server.log"); + if (strpos($server_logs, "Fail") !== false) { + // server failed to start for some reason + print "Failed to start server! Logs:" . PHP_EOL . PHP_EOL; + print_r($server_logs); + exit(1); + } + + echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid) . PHP_EOL; + + register_shutdown_function(function() { + // cleanup after ourselves -- remove log file, shut down server + global $pid; + unlink("./server.log"); + posix_kill($pid, SIGKILL); + }); +} diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 8f62e80..18ab15a 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,7 +1,14 @@ - - - . - + + + + . + + + + + + + diff --git a/tests/static/test.json b/tests/static/test.json new file mode 100644 index 0000000..a628852 --- /dev/null +++ b/tests/static/test.json @@ -0,0 +1 @@ +{"foo": "bar", "baz": false}