--- /dev/null
+<?php\r
+/**\r
+ * Mock adapter intended for testing\r
+ *\r
+ * PHP version 5\r
+ *\r
+ * LICENSE:\r
+ *\r
+ * Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *\r
+ * * Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * * Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ * * The names of the authors may not be used to endorse or promote products\r
+ * derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * @category HTTP\r
+ * @package HTTP_Request2\r
+ * @author Alexey Borzov <avb@php.net>\r
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
+ * @version CVS: $Id: Mock.php 274406 2009-01-23 18:01:57Z avb $\r
+ * @link http://pear.php.net/package/HTTP_Request2\r
+ */\r
+\r
+/**\r
+ * Base class for HTTP_Request2 adapters\r
+ */\r
+require_once 'HTTP/Request2/Adapter.php';\r
+\r
+/**\r
+ * Mock adapter intended for testing\r
+ *\r
+ * Can be used to test applications depending on HTTP_Request2 package without\r
+ * actually performing any HTTP requests. This adapter will return responses\r
+ * previously added via addResponse()\r
+ * <code>\r
+ * $mock = new HTTP_Request2_Adapter_Mock();\r
+ * $mock->addResponse("HTTP/1.1 ... ");\r
+ * \r
+ * $request = new HTTP_Request2();\r
+ * $request->setAdapter($mock);\r
+ * \r
+ * // This will return the response set above\r
+ * $response = $req->send();\r
+ * </code> \r
+ *\r
+ * @category HTTP\r
+ * @package HTTP_Request2\r
+ * @author Alexey Borzov <avb@php.net>\r
+ * @version Release: 0.4.1\r
+ */\r
+class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter\r
+{\r
+ /**\r
+ * A queue of responses to be returned by sendRequest()\r
+ * @var array \r
+ */\r
+ protected $responses = array();\r
+\r
+ /**\r
+ * Returns the next response from the queue built by addResponse()\r
+ *\r
+ * If the queue is empty will return default empty response with status 400,\r
+ * if an Exception object was added to the queue it will be thrown.\r
+ *\r
+ * @param HTTP_Request2\r
+ * @return HTTP_Request2_Response\r
+ * @throws Exception\r
+ */\r
+ public function sendRequest(HTTP_Request2 $request)\r
+ {\r
+ if (count($this->responses) > 0) {\r
+ $response = array_shift($this->responses);\r
+ if ($response instanceof HTTP_Request2_Response) {\r
+ return $response;\r
+ } else {\r
+ // rethrow the exception,\r
+ $class = get_class($response);\r
+ $message = $response->getMessage();\r
+ $code = $response->getCode();\r
+ throw new $class($message, $code);\r
+ }\r
+ } else {\r
+ return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Adds response to the queue\r
+ *\r
+ * @param mixed either a string, a pointer to an open file,\r
+ * a HTTP_Request2_Response or Exception object\r
+ * @throws HTTP_Request2_Exception\r
+ */\r
+ public function addResponse($response)\r
+ {\r
+ if (is_string($response)) {\r
+ $response = self::createResponseFromString($response);\r
+ } elseif (is_resource($response)) {\r
+ $response = self::createResponseFromFile($response);\r
+ } elseif (!$response instanceof HTTP_Request2_Response &&\r
+ !$response instanceof Exception\r
+ ) {\r
+ throw new HTTP_Request2_Exception('Parameter is not a valid response');\r
+ }\r
+ $this->responses[] = $response;\r
+ }\r
+\r
+ /**\r
+ * Creates a new HTTP_Request2_Response object from a string\r
+ *\r
+ * @param string\r
+ * @return HTTP_Request2_Response\r
+ * @throws HTTP_Request2_Exception\r
+ */\r
+ public static function createResponseFromString($str)\r
+ {\r
+ $parts = preg_split('!(\r?\n){2}!m', $str, 2);\r
+ $headerLines = explode("\n", $parts[0]); \r
+ $response = new HTTP_Request2_Response(array_shift($headerLines));\r
+ foreach ($headerLines as $headerLine) {\r
+ $response->parseHeaderLine($headerLine);\r
+ }\r
+ $response->parseHeaderLine('');\r
+ if (isset($parts[1])) {\r
+ $response->appendBody($parts[1]);\r
+ }\r
+ return $response;\r
+ }\r
+\r
+ /**\r
+ * Creates a new HTTP_Request2_Response object from a file\r
+ *\r
+ * @param resource file pointer returned by fopen()\r
+ * @return HTTP_Request2_Response\r
+ * @throws HTTP_Request2_Exception\r
+ */\r
+ public static function createResponseFromFile($fp)\r
+ {\r
+ $response = new HTTP_Request2_Response(fgets($fp));\r
+ do {\r
+ $headerLine = fgets($fp);\r
+ $response->parseHeaderLine($headerLine);\r
+ } while ('' != trim($headerLine));\r
+\r
+ while (!feof($fp)) {\r
+ $response->appendBody(fread($fp, 8192));\r
+ }\r
+ return $response;\r
+ }\r
+}\r
+?>
\ No newline at end of file