*\r
* LICENSE:\r
*\r
- * Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>\r
+ * Copyright (c) 2008-2012, Alexey Borzov <avb@php.net>\r
* All rights reserved.\r
*\r
* Redistribution and use in source and binary forms, with or without\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
+ * @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 SVN: $Id: Mock.php 324937 2012-04-07 10:05:57Z avb $\r
+ * @link http://pear.php.net/package/HTTP_Request2\r
*/\r
\r
/**\r
* <code>\r
* $mock = new HTTP_Request2_Adapter_Mock();\r
* $mock->addResponse("HTTP/1.1 ... ");\r
- * \r
+ *\r
* $request = new HTTP_Request2();\r
* $request->setAdapter($mock);\r
- * \r
+ *\r
* // This will return the response set above\r
* $response = $req->send();\r
- * </code> \r
+ * </code>\r
*\r
- * @category HTTP\r
- * @package HTTP_Request2\r
- * @author Alexey Borzov <avb@php.net>\r
- * @version Release: 0.4.1\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 Release: 2.1.1\r
+ * @link http://pear.php.net/package/HTTP_Request2\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
+ /**\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
+ /**\r
+ * Returns the next response from the queue built by addResponse()\r
+ *\r
+ * Only responses without explicit URLs or with URLs equal to request URL\r
+ * will be considered. If matching response is not found or the queue is\r
+ * empty then default empty response with status 400 will be returned,\r
+ * if an Exception object was added to the queue it will be thrown.\r
+ *\r
+ * @param HTTP_Request2 $request HTTP request message\r
+ *\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
+ $requestUrl = (string)$request->getUrl();\r
+ $response = null;\r
+ foreach ($this->responses as $k => $v) {\r
+ if (!$v[1] || $requestUrl == $v[1]) {\r
+ $response = $v[0];\r
+ array_splice($this->responses, $k, 1);\r
+ break;\r
}\r
- } else {\r
+ }\r
+ if (!$response) {\r
return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");\r
+\r
+ } elseif ($response instanceof HTTP_Request2_Response) {\r
+ return $response;\r
+\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
}\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
+ * Adds response to the queue\r
+ *\r
+ * @param mixed $response either a string, a pointer to an open file,\r
+ * an instance of HTTP_Request2_Response or Exception\r
+ * @param string $url A request URL this response should be valid for\r
+ * (see {@link http://pear.php.net/bugs/bug.php?id=19276})\r
+ *\r
+ * @throws HTTP_Request2_Exception\r
+ */\r
+ public function addResponse($response, $url = null)\r
{\r
if (is_string($response)) {\r
$response = self::createResponseFromString($response);\r
) {\r
throw new HTTP_Request2_Exception('Parameter is not a valid response');\r
}\r
- $this->responses[] = $response;\r
+ $this->responses[] = array($response, $url);\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
+ /**\r
+ * Creates a new HTTP_Request2_Response object from a string\r
+ *\r
+ * @param string $str string containing HTTP response message\r
+ *\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
+ $headerLines = explode("\n", $parts[0]);\r
$response = new HTTP_Request2_Response(array_shift($headerLines));\r
foreach ($headerLines as $headerLine) {\r
$response->parseHeaderLine($headerLine);\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
+ /**\r
+ * Creates a new HTTP_Request2_Response object from a file\r
+ *\r
+ * @param resource $fp file pointer returned by fopen()\r
+ *\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