3 * Mock adapter intended for testing
\r
9 * Copyright (c) 2008-2012, Alexey Borzov <avb@php.net>
\r
10 * All rights reserved.
\r
12 * Redistribution and use in source and binary forms, with or without
\r
13 * modification, are permitted provided that the following conditions
\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
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
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
45 * Base class for HTTP_Request2 adapters
\r
47 require_once 'HTTP/Request2/Adapter.php';
\r
50 * Mock adapter intended for testing
\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
56 * $mock = new HTTP_Request2_Adapter_Mock();
\r
57 * $mock->addResponse("HTTP/1.1 ... ");
\r
59 * $request = new HTTP_Request2();
\r
60 * $request->setAdapter($mock);
\r
62 * // This will return the response set above
\r
63 * $response = $req->send();
\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
73 class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
\r
76 * A queue of responses to be returned by sendRequest()
\r
79 protected $responses = array();
\r
82 * Returns the next response from the queue built by addResponse()
\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
89 * @param HTTP_Request2 $request HTTP request message
\r
91 * @return HTTP_Request2_Response
\r
94 public function sendRequest(HTTP_Request2 $request)
\r
96 $requestUrl = (string)$request->getUrl();
\r
98 foreach ($this->responses as $k => $v) {
\r
99 if (!$v[1] || $requestUrl == $v[1]) {
\r
101 array_splice($this->responses, $k, 1);
\r
106 return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");
\r
108 } elseif ($response instanceof HTTP_Request2_Response) {
\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
121 * Adds response to the queue
\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
128 * @throws HTTP_Request2_Exception
\r
130 public function addResponse($response, $url = null)
\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
139 throw new HTTP_Request2_Exception('Parameter is not a valid response');
\r
141 $this->responses[] = array($response, $url);
\r
145 * Creates a new HTTP_Request2_Response object from a string
\r
147 * @param string $str string containing HTTP response message
\r
149 * @return HTTP_Request2_Response
\r
150 * @throws HTTP_Request2_Exception
\r
152 public static function createResponseFromString($str)
\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
160 $response->parseHeaderLine('');
\r
161 if (isset($parts[1])) {
\r
162 $response->appendBody($parts[1]);
\r
168 * Creates a new HTTP_Request2_Response object from a file
\r
170 * @param resource $fp file pointer returned by fopen()
\r
172 * @return HTTP_Request2_Response
\r
173 * @throws HTTP_Request2_Exception
\r
175 public static function createResponseFromFile($fp)
\r
177 $response = new HTTP_Request2_Response(fgets($fp));
\r
179 $headerLine = fgets($fp);
\r
180 $response->parseHeaderLine($headerLine);
\r
181 } while ('' != trim($headerLine));
\r
183 while (!feof($fp)) {
\r
184 $response->appendBody(fread($fp, 8192));
\r