]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/HTTP/Request2/Adapter/Mock.php
Merge branch '0.9.x' into userflag
[quix0rs-gnu-social.git] / extlib / HTTP / Request2 / Adapter / Mock.php
1 <?php\r
2 /**\r
3  * Mock adapter intended for testing\r
4  *\r
5  * PHP version 5\r
6  *\r
7  * LICENSE:\r
8  *\r
9  * Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>\r
10  * All rights reserved.\r
11  *\r
12  * Redistribution and use in source and binary forms, with or without\r
13  * modification, are permitted provided that the following conditions\r
14  * are met:\r
15  *\r
16  *    * Redistributions of source code must retain the above copyright\r
17  *      notice, this list of conditions and the following disclaimer.\r
18  *    * Redistributions in binary form must reproduce the above copyright\r
19  *      notice, this list of conditions and the following disclaimer in the\r
20  *      documentation and/or other materials provided with the distribution.\r
21  *    * The names of the authors may not be used to endorse or promote products\r
22  *      derived from this software without specific prior written permission.\r
23  *\r
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
32  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
35  *\r
36  * @category   HTTP\r
37  * @package    HTTP_Request2\r
38  * @author     Alexey Borzov <avb@php.net>\r
39  * @license    http://opensource.org/licenses/bsd-license.php New BSD License\r
40  * @version    CVS: $Id: Mock.php 274406 2009-01-23 18:01:57Z avb $\r
41  * @link       http://pear.php.net/package/HTTP_Request2\r
42  */\r
43 \r
44 /**\r
45  * Base class for HTTP_Request2 adapters\r
46  */\r
47 require_once 'HTTP/Request2/Adapter.php';\r
48 \r
49 /**\r
50  * Mock adapter intended for testing\r
51  *\r
52  * Can be used to test applications depending on HTTP_Request2 package without\r
53  * actually performing any HTTP requests. This adapter will return responses\r
54  * previously added via addResponse()\r
55  * <code>\r
56  * $mock = new HTTP_Request2_Adapter_Mock();\r
57  * $mock->addResponse("HTTP/1.1 ... ");\r
58  * \r
59  * $request = new HTTP_Request2();\r
60  * $request->setAdapter($mock);\r
61  * \r
62  * // This will return the response set above\r
63  * $response = $req->send();\r
64  * </code> \r
65  *\r
66  * @category   HTTP\r
67  * @package    HTTP_Request2\r
68  * @author     Alexey Borzov <avb@php.net>\r
69  * @version    Release: 0.4.1\r
70  */\r
71 class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter\r
72 {\r
73    /**\r
74     * A queue of responses to be returned by sendRequest()\r
75     * @var  array \r
76     */\r
77     protected $responses = array();\r
78 \r
79    /**\r
80     * Returns the next response from the queue built by addResponse()\r
81     *\r
82     * If the queue is empty will return default empty response with status 400,\r
83     * if an Exception object was added to the queue it will be thrown.\r
84     *\r
85     * @param    HTTP_Request2\r
86     * @return   HTTP_Request2_Response\r
87     * @throws   Exception\r
88     */\r
89     public function sendRequest(HTTP_Request2 $request)\r
90     {\r
91         if (count($this->responses) > 0) {\r
92             $response = array_shift($this->responses);\r
93             if ($response instanceof HTTP_Request2_Response) {\r
94                 return $response;\r
95             } else {\r
96                 // rethrow the exception,\r
97                 $class   = get_class($response);\r
98                 $message = $response->getMessage();\r
99                 $code    = $response->getCode();\r
100                 throw new $class($message, $code);\r
101             }\r
102         } else {\r
103             return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");\r
104         }\r
105     }\r
106 \r
107    /**\r
108     * Adds response to the queue\r
109     *\r
110     * @param    mixed   either a string, a pointer to an open file,\r
111     *                   a HTTP_Request2_Response or Exception object\r
112     * @throws   HTTP_Request2_Exception\r
113     */\r
114     public function addResponse($response)\r
115     {\r
116         if (is_string($response)) {\r
117             $response = self::createResponseFromString($response);\r
118         } elseif (is_resource($response)) {\r
119             $response = self::createResponseFromFile($response);\r
120         } elseif (!$response instanceof HTTP_Request2_Response &&\r
121                   !$response instanceof Exception\r
122         ) {\r
123             throw new HTTP_Request2_Exception('Parameter is not a valid response');\r
124         }\r
125         $this->responses[] = $response;\r
126     }\r
127 \r
128    /**\r
129     * Creates a new HTTP_Request2_Response object from a string\r
130     *\r
131     * @param    string\r
132     * @return   HTTP_Request2_Response\r
133     * @throws   HTTP_Request2_Exception\r
134     */\r
135     public static function createResponseFromString($str)\r
136     {\r
137         $parts       = preg_split('!(\r?\n){2}!m', $str, 2);\r
138         $headerLines = explode("\n", $parts[0]); \r
139         $response    = new HTTP_Request2_Response(array_shift($headerLines));\r
140         foreach ($headerLines as $headerLine) {\r
141             $response->parseHeaderLine($headerLine);\r
142         }\r
143         $response->parseHeaderLine('');\r
144         if (isset($parts[1])) {\r
145             $response->appendBody($parts[1]);\r
146         }\r
147         return $response;\r
148     }\r
149 \r
150    /**\r
151     * Creates a new HTTP_Request2_Response object from a file\r
152     *\r
153     * @param    resource    file pointer returned by fopen()\r
154     * @return   HTTP_Request2_Response\r
155     * @throws   HTTP_Request2_Exception\r
156     */\r
157     public static function createResponseFromFile($fp)\r
158     {\r
159         $response = new HTTP_Request2_Response(fgets($fp));\r
160         do {\r
161             $headerLine = fgets($fp);\r
162             $response->parseHeaderLine($headerLine);\r
163         } while ('' != trim($headerLine));\r
164 \r
165         while (!feof($fp)) {\r
166             $response->appendBody(fread($fp, 8192));\r
167         }\r
168         return $response;\r
169     }\r
170 }\r
171 ?>