]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/HTTP/Request2/Adapter/Mock.php
PEAR::HTTP_Request2 updated to 2.2.1
[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  * This source file is subject to BSD 3-Clause License that is bundled\r
10  * with this package in the file LICENSE and available at the URL\r
11  * https://raw.github.com/pear/HTTP_Request2/trunk/docs/LICENSE\r
12  *\r
13  * @category  HTTP\r
14  * @package   HTTP_Request2\r
15  * @author    Alexey Borzov <avb@php.net>\r
16  * @copyright 2008-2014 Alexey Borzov <avb@php.net>\r
17  * @license   http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License\r
18  * @link      http://pear.php.net/package/HTTP_Request2\r
19  */\r
20 \r
21 /**\r
22  * Base class for HTTP_Request2 adapters\r
23  */\r
24 require_once 'HTTP/Request2/Adapter.php';\r
25 \r
26 /**\r
27  * Mock adapter intended for testing\r
28  *\r
29  * Can be used to test applications depending on HTTP_Request2 package without\r
30  * actually performing any HTTP requests. This adapter will return responses\r
31  * previously added via addResponse()\r
32  * <code>\r
33  * $mock = new HTTP_Request2_Adapter_Mock();\r
34  * $mock->addResponse("HTTP/1.1 ... ");\r
35  *\r
36  * $request = new HTTP_Request2();\r
37  * $request->setAdapter($mock);\r
38  *\r
39  * // This will return the response set above\r
40  * $response = $req->send();\r
41  * </code>\r
42  *\r
43  * @category HTTP\r
44  * @package  HTTP_Request2\r
45  * @author   Alexey Borzov <avb@php.net>\r
46  * @license  http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License\r
47  * @version  Release: 2.2.1\r
48  * @link     http://pear.php.net/package/HTTP_Request2\r
49  */\r
50 class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter\r
51 {\r
52     /**\r
53      * A queue of responses to be returned by sendRequest()\r
54      * @var  array\r
55      */\r
56     protected $responses = array();\r
57 \r
58     /**\r
59      * Returns the next response from the queue built by addResponse()\r
60      *\r
61      * Only responses without explicit URLs or with URLs equal to request URL\r
62      * will be considered. If matching response is not found or the queue is\r
63      * empty then default empty response with status 400 will be returned,\r
64      * if an Exception object was added to the queue it will be thrown.\r
65      *\r
66      * @param HTTP_Request2 $request HTTP request message\r
67      *\r
68      * @return   HTTP_Request2_Response\r
69      * @throws   Exception\r
70      */\r
71     public function sendRequest(HTTP_Request2 $request)\r
72     {\r
73         $requestUrl = (string)$request->getUrl();\r
74         $response   = null;\r
75         foreach ($this->responses as $k => $v) {\r
76             if (!$v[1] || $requestUrl == $v[1]) {\r
77                 $response = $v[0];\r
78                 array_splice($this->responses, $k, 1);\r
79                 break;\r
80             }\r
81         }\r
82         if (!$response) {\r
83             return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");\r
84 \r
85         } elseif ($response instanceof HTTP_Request2_Response) {\r
86             return $response;\r
87 \r
88         } else {\r
89             // rethrow the exception\r
90             $class   = get_class($response);\r
91             $message = $response->getMessage();\r
92             $code    = $response->getCode();\r
93             throw new $class($message, $code);\r
94         }\r
95     }\r
96 \r
97     /**\r
98      * Adds response to the queue\r
99      *\r
100      * @param mixed  $response either a string, a pointer to an open file,\r
101      *                         an instance of HTTP_Request2_Response or Exception\r
102      * @param string $url      A request URL this response should be valid for\r
103      *                         (see {@link http://pear.php.net/bugs/bug.php?id=19276})\r
104      *\r
105      * @throws   HTTP_Request2_Exception\r
106      */\r
107     public function addResponse($response, $url = null)\r
108     {\r
109         if (is_string($response)) {\r
110             $response = self::createResponseFromString($response);\r
111         } elseif (is_resource($response)) {\r
112             $response = self::createResponseFromFile($response);\r
113         } elseif (!$response instanceof HTTP_Request2_Response &&\r
114                   !$response instanceof Exception\r
115         ) {\r
116             throw new HTTP_Request2_Exception('Parameter is not a valid response');\r
117         }\r
118         $this->responses[] = array($response, $url);\r
119     }\r
120 \r
121     /**\r
122      * Creates a new HTTP_Request2_Response object from a string\r
123      *\r
124      * @param string $str string containing HTTP response message\r
125      *\r
126      * @return   HTTP_Request2_Response\r
127      * @throws   HTTP_Request2_Exception\r
128      */\r
129     public static function createResponseFromString($str)\r
130     {\r
131         $parts       = preg_split('!(\r?\n){2}!m', $str, 2);\r
132         $headerLines = explode("\n", $parts[0]);\r
133         $response    = new HTTP_Request2_Response(array_shift($headerLines));\r
134         foreach ($headerLines as $headerLine) {\r
135             $response->parseHeaderLine($headerLine);\r
136         }\r
137         $response->parseHeaderLine('');\r
138         if (isset($parts[1])) {\r
139             $response->appendBody($parts[1]);\r
140         }\r
141         return $response;\r
142     }\r
143 \r
144     /**\r
145      * Creates a new HTTP_Request2_Response object from a file\r
146      *\r
147      * @param resource $fp file pointer returned by fopen()\r
148      *\r
149      * @return   HTTP_Request2_Response\r
150      * @throws   HTTP_Request2_Exception\r
151      */\r
152     public static function createResponseFromFile($fp)\r
153     {\r
154         $response = new HTTP_Request2_Response(fgets($fp));\r
155         do {\r
156             $headerLine = fgets($fp);\r
157             $response->parseHeaderLine($headerLine);\r
158         } while ('' != trim($headerLine));\r
159 \r
160         while (!feof($fp)) {\r
161             $response->appendBody(fread($fp, 8192));\r
162         }\r
163         return $response;\r
164     }\r
165 }\r
166 ?>