File indexing completed on 2025-01-19 05:21:14
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Http 0017 * @subpackage Client_Adapter 0018 * @version $Id$ 0019 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0020 * @license http://framework.zend.com/license/new-bsd New BSD License 0021 */ 0022 0023 /** 0024 * @see Zend_Uri_Http 0025 */ 0026 // require_once 'Zend/Uri/Http.php'; 0027 /** 0028 * @see Zend_Http_Response 0029 */ 0030 // require_once 'Zend/Http/Response.php'; 0031 /** 0032 * @see Zend_Http_Client_Adapter_Interface 0033 */ 0034 // require_once 'Zend/Http/Client/Adapter/Interface.php'; 0035 0036 /** 0037 * A testing-purposes adapter. 0038 * 0039 * Should be used to test all components that rely on Zend_Http_Client, 0040 * without actually performing an HTTP request. You should instantiate this 0041 * object manually, and then set it as the client's adapter. Then, you can 0042 * set the expected response using the setResponse() method. 0043 * 0044 * @category Zend 0045 * @package Zend_Http 0046 * @subpackage Client_Adapter 0047 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0048 * @license http://framework.zend.com/license/new-bsd New BSD License 0049 */ 0050 class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interface 0051 { 0052 /** 0053 * Parameters array 0054 * 0055 * @var array 0056 */ 0057 protected $config = array(); 0058 0059 /** 0060 * Buffer of responses to be returned by the read() method. Can be 0061 * set using setResponse() and addResponse(). 0062 * 0063 * @var array 0064 */ 0065 protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n"); 0066 0067 /** 0068 * Current position in the response buffer 0069 * 0070 * @var integer 0071 */ 0072 protected $responseIndex = 0; 0073 0074 /** 0075 * Wether or not the next request will fail with an exception 0076 * 0077 * @var boolean 0078 */ 0079 protected $_nextRequestWillFail = false; 0080 0081 /** 0082 * Adapter constructor, currently empty. Config is set using setConfig() 0083 * 0084 */ 0085 public function __construct() 0086 { } 0087 0088 /** 0089 * Set the nextRequestWillFail flag 0090 * 0091 * @param boolean $flag 0092 * @return Zend_Http_Client_Adapter_Test 0093 */ 0094 public function setNextRequestWillFail($flag) 0095 { 0096 $this->_nextRequestWillFail = (bool) $flag; 0097 0098 return $this; 0099 } 0100 0101 /** 0102 * Set the configuration array for the adapter 0103 * 0104 * @param Zend_Config | array $config 0105 */ 0106 public function setConfig($config = array()) 0107 { 0108 if ($config instanceof Zend_Config) { 0109 $config = $config->toArray(); 0110 0111 } elseif (! is_array($config)) { 0112 // require_once 'Zend/Http/Client/Adapter/Exception.php'; 0113 throw new Zend_Http_Client_Adapter_Exception( 0114 'Array or Zend_Config object expected, got ' . gettype($config) 0115 ); 0116 } 0117 0118 foreach ($config as $k => $v) { 0119 $this->config[strtolower($k)] = $v; 0120 } 0121 } 0122 0123 0124 /** 0125 * Connect to the remote server 0126 * 0127 * @param string $host 0128 * @param int $port 0129 * @param boolean $secure 0130 * @param int $timeout 0131 * @throws Zend_Http_Client_Adapter_Exception 0132 */ 0133 public function connect($host, $port = 80, $secure = false) 0134 { 0135 if ($this->_nextRequestWillFail) { 0136 $this->_nextRequestWillFail = false; 0137 // require_once 'Zend/Http/Client/Adapter/Exception.php'; 0138 throw new Zend_Http_Client_Adapter_Exception('Request failed'); 0139 } 0140 } 0141 0142 /** 0143 * Send request to the remote server 0144 * 0145 * @param string $method 0146 * @param Zend_Uri_Http $uri 0147 * @param string $http_ver 0148 * @param array $headers 0149 * @param string $body 0150 * @return string Request as string 0151 */ 0152 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '') 0153 { 0154 $host = $uri->getHost(); 0155 $host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host); 0156 0157 // Build request headers 0158 $path = $uri->getPath(); 0159 if ($uri->getQuery()) $path .= '?' . $uri->getQuery(); 0160 $request = "{$method} {$path} HTTP/{$http_ver}\r\n"; 0161 foreach ($headers as $k => $v) { 0162 if (is_string($k)) $v = ucfirst($k) . ": $v"; 0163 $request .= "$v\r\n"; 0164 } 0165 0166 // Add the request body 0167 $request .= "\r\n" . $body; 0168 0169 // Do nothing - just return the request as string 0170 0171 return $request; 0172 } 0173 0174 /** 0175 * Return the response set in $this->setResponse() 0176 * 0177 * @return string 0178 */ 0179 public function read() 0180 { 0181 if ($this->responseIndex >= count($this->responses)) { 0182 $this->responseIndex = 0; 0183 } 0184 return $this->responses[$this->responseIndex++]; 0185 } 0186 0187 /** 0188 * Close the connection (dummy) 0189 * 0190 */ 0191 public function close() 0192 { } 0193 0194 /** 0195 * Set the HTTP response(s) to be returned by this adapter 0196 * 0197 * @param Zend_Http_Response|array|string $response 0198 */ 0199 public function setResponse($response) 0200 { 0201 if ($response instanceof Zend_Http_Response) { 0202 $response = $response->asString("\r\n"); 0203 } 0204 0205 $this->responses = (array)$response; 0206 $this->responseIndex = 0; 0207 } 0208 0209 /** 0210 * Add another response to the response buffer. 0211 * 0212 * @param string Zend_Http_Response|$response 0213 */ 0214 public function addResponse($response) 0215 { 0216 if ($response instanceof Zend_Http_Response) { 0217 $response = $response->asString("\r\n"); 0218 } 0219 0220 $this->responses[] = $response; 0221 } 0222 0223 /** 0224 * Sets the position of the response buffer. Selects which 0225 * response will be returned on the next call to read(). 0226 * 0227 * @param integer $index 0228 */ 0229 public function setResponseIndex($index) 0230 { 0231 if ($index < 0 || $index >= count($this->responses)) { 0232 // require_once 'Zend/Http/Client/Adapter/Exception.php'; 0233 throw new Zend_Http_Client_Adapter_Exception( 0234 'Index out of range of response buffer size'); 0235 } 0236 $this->responseIndex = $index; 0237 } 0238 0239 /** 0240 * Retrieve the array of all configuration options 0241 * 0242 * @return array 0243 */ 0244 public function getConfig() 0245 { 0246 return $this->config; 0247 } 0248 }