From: Chuck Hagenbuch Date: Sun, 23 Aug 2009 16:30:59 +0000 (-0400) Subject: With the Fopen adapter, sniff the error message for an HTTP status code, and if X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=1d9a5911bc6abf5c9a535a6c4b858a19f5c62f60;p=horde.git With the Fopen adapter, sniff the error message for an HTTP status code, and if there is one, return a minimally useful Response object instead of throwing an error message. --- diff --git a/framework/Http_Client/lib/Horde/Http/Client/Adapter/Fopen.php b/framework/Http_Client/lib/Horde/Http/Client/Adapter/Fopen.php index 4cdc70eb4..a735e9373 100644 --- a/framework/Http_Client/lib/Horde/Http/Client/Adapter/Fopen.php +++ b/framework/Http_Client/lib/Horde/Http/Client/Adapter/Fopen.php @@ -66,7 +66,13 @@ class Horde_Http_Client_Adapter_Fopen extends Horde_Http_Client_Adapter_Base $context = stream_context_create($opts); $stream = @fopen($uri, 'rb', false, $context); if (!$stream) { - throw new Horde_Http_Client_Exception('Problem with ' . $uri . ': ', error_get_last()); + $error = error_get_last(); + if (preg_match('/HTTP\/(\d+\.\d+) (\d{3}) (.*)$/', $error['message'], $matches)) { + // Create a Response for the HTTP error code + return new Horde_Http_Client_Response($uri, null, $matches[0]); + } else { + throw new Horde_Http_Client_Exception('Problem with ' . $uri . ': ', $error); + } } $meta = stream_get_meta_data($stream); diff --git a/framework/Http_Client/test/Horde/Http/Client/Adapter/FopenTest.php b/framework/Http_Client/test/Horde/Http/Client/Adapter/FopenTest.php new file mode 100644 index 000000000..f9f5632fa --- /dev/null +++ b/framework/Http_Client/test/Horde/Http/Client/Adapter/FopenTest.php @@ -0,0 +1,36 @@ + new Horde_Http_Client_Adapter_Fopen())); + try { + $response = $client->get('http://doesntexist.example.com/'); + $this->fail(); + } catch (Horde_Http_Client_Exception $e) { + } + } + + public function testReturnsResponseInsteadOfExceptionOn404() + { + $client = new Horde_Http_Client(array('adapter' => new Horde_Http_Client_Adapter_Fopen())); + $response = $client->get('http://example.com/doesntexist'); + $this->assertEquals(404, $response->code); + } +} diff --git a/framework/Http_Client/test/Horde/Http/Client/AllTests.php b/framework/Http_Client/test/Horde/Http/Client/AllTests.php new file mode 100644 index 000000000..f2bc7e3bd --- /dev/null +++ b/framework/Http_Client/test/Horde/Http/Client/AllTests.php @@ -0,0 +1,54 @@ +isFile() && preg_match('/Test.php$/', $file->getFilename())) { + $pathname = $file->getPathname(); + require $pathname; + + $class = str_replace(DIRECTORY_SEPARATOR, '_', + preg_replace("/^$baseregexp(.*)\.php/", '\\1', $pathname)); + $suite->addTestSuite('Horde_Http_Client_' . $class); + } + } + + return $suite; + } + +} + +if (PHPUnit_MAIN_METHOD == 'Horde_Http_Client_AllTests::main') { + Horde_Http_Client_AllTests::main(); +}