7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @subpackage Client_Adapter
19 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
26 require_once 'Zend/Uri/Http.php';
28 * @see Zend_Http_Response
30 require_once 'Zend/Http/Response.php';
32 * @see Zend_Http_Client_Adapter_Interface
34 require_once 'Zend/Http/Client/Adapter/Interface.php';
37 * A testing-purposes adapter.
39 * Should be used to test all components that rely on Zend_Http_Client,
40 * without actually performing an HTTP request. You should instantiate this
41 * object manually, and then set it as the client's adapter. Then, you can
42 * set the expected response using the setResponse() method.
46 * @subpackage Client_Adapter
47 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
48 * @license http://framework.zend.com/license/new-bsd New BSD License
50 class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interface
57 protected $config = array();
60 * Buffer of responses to be returned by the read() method. Can be
61 * set using setResponse() and addResponse().
65 protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n");
68 * Current position in the response buffer
72 protected $responseIndex = 0;
75 * Wether or not the next request will fail with an exception
79 protected $_nextRequestWillFail = false;
82 * Adapter constructor, currently empty. Config is set using setConfig()
85 public function __construct()
89 * Set the nextRequestWillFail flag
91 * @param boolean $flag
92 * @return Zend_Http_Client_Adapter_Test
94 public function setNextRequestWillFail($flag)
96 $this->_nextRequestWillFail = (bool) $flag;
102 * Set the configuration array for the adapter
104 * @param Zend_Config | array $config
106 public function setConfig($config = array())
108 if ($config instanceof Zend_Config) {
109 $config = $config->toArray();
111 } elseif (! is_array($config)) {
112 require_once 'Zend/Http/Client/Adapter/Exception.php';
113 throw new Zend_Http_Client_Adapter_Exception(
114 'Array or Zend_Config object expected, got ' . gettype($config)
118 foreach ($config as $k => $v) {
119 $this->config[strtolower($k)] = $v;
125 * Connect to the remote server
127 * @param string $host
129 * @param boolean $secure
130 * @param int $timeout
131 * @throws Zend_Http_Client_Adapter_Exception
133 public function connect($host, $port = 80, $secure = false)
135 if ($this->_nextRequestWillFail) {
136 $this->_nextRequestWillFail = false;
137 require_once 'Zend/Http/Client/Adapter/Exception.php';
138 throw new Zend_Http_Client_Adapter_Exception('Request failed');
143 * Send request to the remote server
145 * @param string $method
146 * @param Zend_Uri_Http $uri
147 * @param string $http_ver
148 * @param array $headers
149 * @param string $body
150 * @return string Request as string
152 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
154 $host = $uri->getHost();
155 $host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host);
157 // Build request headers
158 $path = $uri->getPath();
159 if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
160 $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
161 foreach ($headers as $k => $v) {
162 if (is_string($k)) $v = ucfirst($k) . ": $v";
163 $request .= "$v\r\n";
166 // Add the request body
167 $request .= "\r\n" . $body;
169 // Do nothing - just return the request as string
175 * Return the response set in $this->setResponse()
179 public function read()
181 if ($this->responseIndex >= count($this->responses)) {
182 $this->responseIndex = 0;
184 return $this->responses[$this->responseIndex++];
188 * Close the connection (dummy)
191 public function close()
195 * Set the HTTP response(s) to be returned by this adapter
197 * @param Zend_Http_Response|array|string $response
199 public function setResponse($response)
201 if ($response instanceof Zend_Http_Response) {
202 $response = $response->asString("\r\n");
205 $this->responses = (array)$response;
206 $this->responseIndex = 0;
210 * Add another response to the response buffer.
212 * @param string Zend_Http_Response|$response
214 public function addResponse($response)
216 if ($response instanceof Zend_Http_Response) {
217 $response = $response->asString("\r\n");
220 $this->responses[] = $response;
224 * Sets the position of the response buffer. Selects which
225 * response will be returned on the next call to read().
227 * @param integer $index
229 public function setResponseIndex($index)
231 if ($index < 0 || $index >= count($this->responses)) {
232 require_once 'Zend/Http/Client/Adapter/Exception.php';
233 throw new Zend_Http_Client_Adapter_Exception(
234 'Index out of range of response buffer size');
236 $this->responseIndex = $index;