$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);
--- /dev/null
+<?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);
+ }
+}
--- /dev/null
+<?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();
+}