With the Fopen adapter, sniff the error message for an HTTP status code, and if
authorChuck Hagenbuch <chuck@horde.org>
Sun, 23 Aug 2009 16:30:59 +0000 (12:30 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Sun, 23 Aug 2009 16:30:59 +0000 (12:30 -0400)
there is one, return a minimally useful Response object instead of throwing an
error message.

framework/Http_Client/lib/Horde/Http/Client/Adapter/Fopen.php
framework/Http_Client/test/Horde/Http/Client/Adapter/FopenTest.php [new file with mode: 0644]
framework/Http_Client/test/Horde/Http/Client/AllTests.php [new file with mode: 0644]

index 4cdc70e..a735e93 100644 (file)
@@ -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 (file)
index 0000000..f9f5632
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+/**
+ * @category   Horde
+ * @package    Http_Client
+ * @subpackage UnitTests
+ * @copyright  2007-2009 The Horde Project (http://www.horde.org/)
+ * @license    http://opensource.org/licenses/bsd-license.php
+ */
+
+/**
+ * @group      support
+ * @category   Horde
+ * @package    Http_Client
+ * @subpackage UnitTests
+ * @copyright  2007-2009 The Horde Project (http://www.horde.org/)
+ * @license    http://opensource.org/licenses/bsd-license.php
+ */
+class Horde_Http_Client_Adapter_FopenTest extends PHPUnit_Framework_TestCase
+{
+    public function testThrowsOnBadUri()
+    {
+        $client = new Horde_Http_Client(array('adapter' => 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 (file)
index 0000000..f2bc7e3
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/**
+ * @category   Horde
+ * @package    Http_Client
+ * @subpackage UnitTests
+ * @copyright  2008-2009 The Horde Project (http://www.horde.org/)
+ * @license    http://opensource.org/licenses/bsd-license.php
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Horde_Http_Client_AllTests::main');
+}
+
+require_once 'PHPUnit/Framework/TestSuite.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+
+class Horde_Http_Client_AllTests {
+
+    public static function main()
+    {
+        PHPUnit_TextUI_TestRunner::run(self::suite());
+    }
+
+    public static function suite()
+    {
+        set_include_path(dirname(__FILE__) . '/../../../../lib' . PATH_SEPARATOR . get_include_path());
+        if (!spl_autoload_functions()) {
+            spl_autoload_register(create_function('$class', '$filename = str_replace(array(\'::\', \'_\'), \'/\', $class); include "$filename.php";'));
+        }
+
+        $suite = new PHPUnit_Framework_TestSuite('Horde Framework - Horde_Http_Client');
+
+        $basedir = dirname(__FILE__);
+        $baseregexp = preg_quote($basedir . DIRECTORY_SEPARATOR, '/');
+
+        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basedir)) as $file) {
+            if ($file->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();
+}