]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/HTTP/Request2/Adapter/Mock.php
29d48dbb749af962938815a0ac94aecf7da7cc5d
[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-2012, 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  SVN: $Id: Mock.php 324937 2012-04-07 10:05: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  * @license  http://opensource.org/licenses/bsd-license.php New BSD License\r
70  * @version  Release: 2.1.1\r
71  * @link     http://pear.php.net/package/HTTP_Request2\r
72  */\r
73 class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter\r
74 {\r
75     /**\r
76      * A queue of responses to be returned by sendRequest()\r
77      * @var  array\r
78      */\r
79     protected $responses = array();\r
80 \r
81     /**\r
82      * Returns the next response from the queue built by addResponse()\r
83      *\r
84      * Only responses without explicit URLs or with URLs equal to request URL\r
85      * will be considered. If matching response is not found or the queue is\r
86      * empty then default empty response with status 400 will be returned,\r
87      * if an Exception object was added to the queue it will be thrown.\r
88      *\r
89      * @param HTTP_Request2 $request HTTP request message\r
90      *\r
91      * @return   HTTP_Request2_Response\r
92      * @throws   Exception\r
93      */\r
94     public function sendRequest(HTTP_Request2 $request)\r
95     {\r
96         $requestUrl = (string)$request->getUrl();\r
97         $response   = null;\r
98         foreach ($this->responses as $k => $v) {\r
99             if (!$v[1] || $requestUrl == $v[1]) {\r
100                 $response = $v[0];\r
101                 array_splice($this->responses, $k, 1);\r
102                 break;\r
103             }\r
104         }\r
105         if (!$response) {\r
106             return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");\r
107 \r
108         } elseif ($response instanceof HTTP_Request2_Response) {\r
109             return $response;\r
110 \r
111         } else {\r
112             // rethrow the exception\r
113             $class   = get_class($response);\r
114             $message = $response->getMessage();\r
115             $code    = $response->getCode();\r
116             throw new $class($message, $code);\r
117         }\r
118     }\r
119 \r
120     /**\r
121      * Adds response to the queue\r
122      *\r
123      * @param mixed  $response either a string, a pointer to an open file,\r
124      *                         an instance of HTTP_Request2_Response or Exception\r
125      * @param string $url      A request URL this response should be valid for\r
126      *                         (see {@link http://pear.php.net/bugs/bug.php?id=19276})\r
127      *\r
128      * @throws   HTTP_Request2_Exception\r
129      */\r
130     public function addResponse($response, $url = null)\r
131     {\r
132         if (is_string($response)) {\r
133             $response = self::createResponseFromString($response);\r
134         } elseif (is_resource($response)) {\r
135             $response = self::createResponseFromFile($response);\r
136         } elseif (!$response instanceof HTTP_Request2_Response &&\r
137                   !$response instanceof Exception\r
138         ) {\r
139             throw new HTTP_Request2_Exception('Parameter is not a valid response');\r
140         }\r
141         $this->responses[] = array($response, $url);\r
142     }\r
143 \r
144     /**\r
145      * Creates a new HTTP_Request2_Response object from a string\r
146      *\r
147      * @param string $str string containing HTTP response message\r
148      *\r
149      * @return   HTTP_Request2_Response\r
150      * @throws   HTTP_Request2_Exception\r
151      */\r
152     public static function createResponseFromString($str)\r
153     {\r
154         $parts       = preg_split('!(\r?\n){2}!m', $str, 2);\r
155         $headerLines = explode("\n", $parts[0]);\r
156         $response    = new HTTP_Request2_Response(array_shift($headerLines));\r
157         foreach ($headerLines as $headerLine) {\r
158             $response->parseHeaderLine($headerLine);\r
159         }\r
160         $response->parseHeaderLine('');\r
161         if (isset($parts[1])) {\r
162             $response->appendBody($parts[1]);\r
163         }\r
164         return $response;\r
165     }\r
166 \r
167     /**\r
168      * Creates a new HTTP_Request2_Response object from a file\r
169      *\r
170      * @param resource $fp file pointer returned by fopen()\r
171      *\r
172      * @return   HTTP_Request2_Response\r
173      * @throws   HTTP_Request2_Exception\r
174      */\r
175     public static function createResponseFromFile($fp)\r
176     {\r
177         $response = new HTTP_Request2_Response(fgets($fp));\r
178         do {\r
179             $headerLine = fgets($fp);\r
180             $response->parseHeaderLine($headerLine);\r
181         } while ('' != trim($headerLine));\r
182 \r
183         while (!feof($fp)) {\r
184             $response->appendBody(fread($fp, 8192));\r
185         }\r
186         return $response;\r
187     }\r
188 }\r
189 ?>