| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * Mock adapter intended for testing
|
|---|
| 4 | *
|
|---|
| 5 | * PHP version 5
|
|---|
| 6 | *
|
|---|
| 7 | * LICENSE:
|
|---|
| 8 | *
|
|---|
| 9 | * Copyright (c) 2008-2012, Alexey Borzov <[email protected]>
|
|---|
| 10 | * All rights reserved.
|
|---|
| 11 | *
|
|---|
| 12 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | * modification, are permitted provided that the following conditions
|
|---|
| 14 | * are met:
|
|---|
| 15 | *
|
|---|
| 16 | * * Redistributions of source code must retain the above copyright
|
|---|
| 17 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 18 | * * Redistributions in binary form must reproduce the above copyright
|
|---|
| 19 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 20 | * documentation and/or other materials provided with the distribution.
|
|---|
| 21 | * * The names of the authors may not be used to endorse or promote products
|
|---|
| 22 | * derived from this software without specific prior written permission.
|
|---|
| 23 | *
|
|---|
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|---|
| 25 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|---|
| 26 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|---|
| 27 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|---|
| 28 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|---|
| 29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|---|
| 30 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|---|
| 31 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|---|
| 32 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|---|
| 33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|---|
| 34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 35 | *
|
|---|
| 36 | * @category HTTP
|
|---|
| 37 | * @package HTTP_Request2
|
|---|
| 38 | * @author Alexey Borzov <[email protected]>
|
|---|
| 39 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|---|
| 40 | * @version SVN: $Id: Mock.php 324937 2012-04-07 10:05:57Z avb $
|
|---|
| 41 | * @link http://pear.php.net/package/HTTP_Request2
|
|---|
| 42 | */
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Base class for HTTP_Request2 adapters
|
|---|
| 46 | */
|
|---|
| 47 | require_once 'HTTP/Request2/Adapter.php';
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Mock adapter intended for testing
|
|---|
| 51 | *
|
|---|
| 52 | * Can be used to test applications depending on HTTP_Request2 package without
|
|---|
| 53 | * actually performing any HTTP requests. This adapter will return responses
|
|---|
| 54 | * previously added via addResponse()
|
|---|
| 55 | * <code>
|
|---|
| 56 | * $mock = new HTTP_Request2_Adapter_Mock();
|
|---|
| 57 | * $mock->addResponse("HTTP/1.1 ... ");
|
|---|
| 58 | *
|
|---|
| 59 | * $request = new HTTP_Request2();
|
|---|
| 60 | * $request->setAdapter($mock);
|
|---|
| 61 | *
|
|---|
| 62 | * // This will return the response set above
|
|---|
| 63 | * $response = $req->send();
|
|---|
| 64 | * </code>
|
|---|
| 65 | *
|
|---|
| 66 | * @category HTTP
|
|---|
| 67 | * @package HTTP_Request2
|
|---|
| 68 | * @author Alexey Borzov <[email protected]>
|
|---|
| 69 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|---|
| 70 | * @version Release: 2.1.1
|
|---|
| 71 | * @link http://pear.php.net/package/HTTP_Request2
|
|---|
| 72 | */
|
|---|
| 73 | class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
|
|---|
| 74 | {
|
|---|
| 75 | /**
|
|---|
| 76 | * A queue of responses to be returned by sendRequest()
|
|---|
| 77 | * @var array
|
|---|
| 78 | */
|
|---|
| 79 | protected $responses = array();
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Returns the next response from the queue built by addResponse()
|
|---|
| 83 | *
|
|---|
| 84 | * Only responses without explicit URLs or with URLs equal to request URL
|
|---|
| 85 | * will be considered. If matching response is not found or the queue is
|
|---|
| 86 | * empty then default empty response with status 400 will be returned,
|
|---|
| 87 | * if an Exception object was added to the queue it will be thrown.
|
|---|
| 88 | *
|
|---|
| 89 | * @param HTTP_Request2 $request HTTP request message
|
|---|
| 90 | *
|
|---|
| 91 | * @return HTTP_Request2_Response
|
|---|
| 92 | * @throws Exception
|
|---|
| 93 | */
|
|---|
| 94 | public function sendRequest(HTTP_Request2 $request)
|
|---|
| 95 | {
|
|---|
| 96 | $requestUrl = (string)$request->getUrl();
|
|---|
| 97 | $response = null;
|
|---|
| 98 | foreach ($this->responses as $k => $v) {
|
|---|
| 99 | if (!$v[1] || $requestUrl == $v[1]) {
|
|---|
| 100 | $response = $v[0];
|
|---|
| 101 | array_splice($this->responses, $k, 1);
|
|---|
| 102 | break;
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | if (!$response) {
|
|---|
| 106 | return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");
|
|---|
| 107 |
|
|---|
| 108 | } elseif ($response instanceof HTTP_Request2_Response) {
|
|---|
| 109 | return $response;
|
|---|
| 110 |
|
|---|
| 111 | } else {
|
|---|
| 112 | // rethrow the exception
|
|---|
| 113 | $class = get_class($response);
|
|---|
| 114 | $message = $response->getMessage();
|
|---|
| 115 | $code = $response->getCode();
|
|---|
| 116 | throw new $class($message, $code);
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * Adds response to the queue
|
|---|
| 122 | *
|
|---|
| 123 | * @param mixed $response either a string, a pointer to an open file,
|
|---|
| 124 | * an instance of HTTP_Request2_Response or Exception
|
|---|
| 125 | * @param string $url A request URL this response should be valid for
|
|---|
| 126 | * (see {@link http://pear.php.net/bugs/bug.php?id=19276})
|
|---|
| 127 | *
|
|---|
| 128 | * @throws HTTP_Request2_Exception
|
|---|
| 129 | */
|
|---|
| 130 | public function addResponse($response, $url = null)
|
|---|
| 131 | {
|
|---|
| 132 | if (is_string($response)) {
|
|---|
| 133 | $response = self::createResponseFromString($response);
|
|---|
| 134 | } elseif (is_resource($response)) {
|
|---|
| 135 | $response = self::createResponseFromFile($response);
|
|---|
| 136 | } elseif (!$response instanceof HTTP_Request2_Response &&
|
|---|
| 137 | !$response instanceof Exception
|
|---|
| 138 | ) {
|
|---|
| 139 | throw new HTTP_Request2_Exception('Parameter is not a valid response');
|
|---|
| 140 | }
|
|---|
| 141 | $this->responses[] = array($response, $url);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Creates a new HTTP_Request2_Response object from a string
|
|---|
| 146 | *
|
|---|
| 147 | * @param string $str string containing HTTP response message
|
|---|
| 148 | *
|
|---|
| 149 | * @return HTTP_Request2_Response
|
|---|
| 150 | * @throws HTTP_Request2_Exception
|
|---|
| 151 | */
|
|---|
| 152 | public static function createResponseFromString($str)
|
|---|
| 153 | {
|
|---|
| 154 | $parts = preg_split('!(\r?\n){2}!m', $str, 2);
|
|---|
| 155 | $headerLines = explode("\n", $parts[0]);
|
|---|
| 156 | $response = new HTTP_Request2_Response(array_shift($headerLines));
|
|---|
| 157 | foreach ($headerLines as $headerLine) {
|
|---|
| 158 | $response->parseHeaderLine($headerLine);
|
|---|
| 159 | }
|
|---|
| 160 | $response->parseHeaderLine('');
|
|---|
| 161 | if (isset($parts[1])) {
|
|---|
| 162 | $response->appendBody($parts[1]);
|
|---|
| 163 | }
|
|---|
| 164 | return $response;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 168 | * Creates a new HTTP_Request2_Response object from a file
|
|---|
| 169 | *
|
|---|
| 170 | * @param resource $fp file pointer returned by fopen()
|
|---|
| 171 | *
|
|---|
| 172 | * @return HTTP_Request2_Response
|
|---|
| 173 | * @throws HTTP_Request2_Exception
|
|---|
| 174 | */
|
|---|
| 175 | public static function createResponseFromFile($fp)
|
|---|
| 176 | {
|
|---|
| 177 | $response = new HTTP_Request2_Response(fgets($fp));
|
|---|
| 178 | do {
|
|---|
| 179 | $headerLine = fgets($fp);
|
|---|
| 180 | $response->parseHeaderLine($headerLine);
|
|---|
| 181 | } while ('' != trim($headerLine));
|
|---|
| 182 |
|
|---|
| 183 | while (!feof($fp)) {
|
|---|
| 184 | $response->appendBody(fread($fp, 8192));
|
|---|
| 185 | }
|
|---|
| 186 | return $response;
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | ?> |
|---|