3 * Mock adapter intended for testing
\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
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
22 * Base class for HTTP_Request2 adapters
\r
24 require_once 'HTTP/Request2/Adapter.php';
\r
27 * Mock adapter intended for testing
\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
33 * $mock = new HTTP_Request2_Adapter_Mock();
\r
34 * $mock->addResponse("HTTP/1.1 ... ");
\r
36 * $request = new HTTP_Request2();
\r
37 * $request->setAdapter($mock);
\r
39 * // This will return the response set above
\r
40 * $response = $req->send();
\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
50 class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
\r
53 * A queue of responses to be returned by sendRequest()
\r
56 protected $responses = array();
\r
59 * Returns the next response from the queue built by addResponse()
\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
66 * @param HTTP_Request2 $request HTTP request message
\r
68 * @return HTTP_Request2_Response
\r
71 public function sendRequest(HTTP_Request2 $request)
\r
73 $requestUrl = (string)$request->getUrl();
\r
75 foreach ($this->responses as $k => $v) {
\r
76 if (!$v[1] || $requestUrl == $v[1]) {
\r
78 array_splice($this->responses, $k, 1);
\r
83 return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");
\r
85 } elseif ($response instanceof HTTP_Request2_Response) {
\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
98 * Adds response to the queue
\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
105 * @throws HTTP_Request2_Exception
\r
107 public function addResponse($response, $url = null)
\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
116 throw new HTTP_Request2_Exception('Parameter is not a valid response');
\r
118 $this->responses[] = array($response, $url);
\r
122 * Creates a new HTTP_Request2_Response object from a string
\r
124 * @param string $str string containing HTTP response message
\r
126 * @return HTTP_Request2_Response
\r
127 * @throws HTTP_Request2_Exception
\r
129 public static function createResponseFromString($str)
\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
137 $response->parseHeaderLine('');
\r
138 if (isset($parts[1])) {
\r
139 $response->appendBody($parts[1]);
\r
145 * Creates a new HTTP_Request2_Response object from a file
\r
147 * @param resource $fp file pointer returned by fopen()
\r
149 * @return HTTP_Request2_Response
\r
150 * @throws HTTP_Request2_Exception
\r
152 public static function createResponseFromFile($fp)
\r
154 $response = new HTTP_Request2_Response(fgets($fp));
\r
156 $headerLine = fgets($fp);
\r
157 $response->parseHeaderLine($headerLine);
\r
158 } while ('' != trim($headerLine));
\r
160 while (!feof($fp)) {
\r
161 $response->appendBody(fread($fp, 8192));
\r