3 * Mock adapter intended for testing
\r
9 * Copyright (c) 2008, 2009, 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 CVS: $Id: Mock.php 274406 2009-01-23 18:01: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 * @version Release: 0.4.1
\r
71 class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
\r
74 * A queue of responses to be returned by sendRequest()
\r
77 protected $responses = array();
\r
80 * Returns the next response from the queue built by addResponse()
\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
85 * @param HTTP_Request2
\r
86 * @return HTTP_Request2_Response
\r
89 public function sendRequest(HTTP_Request2 $request)
\r
91 if (count($this->responses) > 0) {
\r
92 $response = array_shift($this->responses);
\r
93 if ($response instanceof HTTP_Request2_Response) {
\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
103 return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");
\r
108 * Adds response to the queue
\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
114 public function addResponse($response)
\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
123 throw new HTTP_Request2_Exception('Parameter is not a valid response');
\r
125 $this->responses[] = $response;
\r
129 * Creates a new HTTP_Request2_Response object from a string
\r
132 * @return HTTP_Request2_Response
\r
133 * @throws HTTP_Request2_Exception
\r
135 public static function createResponseFromString($str)
\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
143 $response->parseHeaderLine('');
\r
144 if (isset($parts[1])) {
\r
145 $response->appendBody($parts[1]);
\r
151 * Creates a new HTTP_Request2_Response object from a file
\r
153 * @param resource file pointer returned by fopen()
\r
154 * @return HTTP_Request2_Response
\r
155 * @throws HTTP_Request2_Exception
\r
157 public static function createResponseFromFile($fp)
\r
159 $response = new HTTP_Request2_Response(fgets($fp));
\r
161 $headerLine = fgets($fp);
\r
162 $response->parseHeaderLine($headerLine);
\r
163 } while ('' != trim($headerLine));
\r
165 while (!feof($fp)) {
\r
166 $response->appendBody(fread($fp, 8192));
\r