]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - extlib/HTTP/Request2/Adapter/Mock.php
Updating external libraries for net access
[quix0rs-gnu-social.git] / extlib / HTTP / Request2 / Adapter / Mock.php
index c99defb899dc3a67feaec14803cfeaade3225c06..29d48dbb749af962938815a0ac94aecf7da7cc5d 100644 (file)
@@ -6,7 +6,7 @@
  *\r
  * LICENSE:\r
  *\r
- * Copyright (c) 2008-2011, 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    SVN: $Id: Mock.php 308322 2011-02-14 13:58:03Z 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
@@ -63,55 +63,71 @@ require_once 'HTTP/Request2/Adapter.php';
  * $response = $req->send();\r
  * </code>\r
  *\r
- * @category   HTTP\r
- * @package    HTTP_Request2\r
- * @author     Alexey Borzov <avb@php.net>\r
- * @version    Release: 2.0.0RC1\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 it 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
-    *                   an instance of HTTP_Request2_Response or Exception\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
@@ -122,16 +138,17 @@ class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
         ) {\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
@@ -147,13 +164,14 @@ class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
         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