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
18 * @version $Id: Test.php 8064 2008-02-16 10:58:39Z thomas $
19 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
23 require_once 'external/Zend/Uri/Http.php';
24 require_once 'external/Zend/Http/Response.php';
25 require_once 'external/Zend/Http/Client/Adapter/Interface.php';
28 * A testing-purposes adapter.
30 * Should be used to test all components that rely on Zend_Http_Client,
31 * without actually performing an HTTP request. You should instantiate this
32 * object manually, and then set it as the client's adapter. Then, you can
33 * set the expected response using the setResponse() method.
37 * @subpackage Client_Adapter
38 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
41 class Zend_Http_Client_Adapter_Test
implements Zend_Http_Client_Adapter_Interface
{
47 protected $config = array();
50 * Buffer of responses to be returned by the read() method. Can be
51 * set using setResponse() and addResponse().
55 protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n");
58 * Current position in the response buffer
62 protected $responseIndex = 0;
65 * Adapter constructor, currently empty. Config is set using setConfig()
68 public function __construct() {}
71 * Set the configuration array for the adapter
73 * @param array $config
75 public function setConfig($config = array()) {
76 if (! is_array($config)) {
77 require_once 'external/Zend/Http/Client/Adapter/Exception.php';
78 throw new Zend_Http_Client_Adapter_Exception('$config expects an array, ' . gettype($config) . ' recieved.');
81 foreach ($config as $k => $v) {
82 $this->config
[strtolower($k)] = $v;
87 * Connect to the remote server
91 * @param boolean $secure
94 public function connect($host, $port = 80, $secure = false) {}
97 * Send request to the remote server
99 * @param string $method
100 * @param Zend_Uri_Http $uri
101 * @param string $http_ver
102 * @param array $headers
103 * @param string $body
104 * @return string Request as string
106 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '') {
107 $host = $uri->getHost();
108 $host = (strtolower($uri->getScheme()) == 'https' ?
'sslv2://' . $host : $host);
110 // Build request headers
111 $path = $uri->getPath();
112 if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
113 $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
114 foreach ($headers as $k => $v) {
115 if (is_string($k)) $v = ucfirst($k) . ": $v";
116 $request .= "$v\r\n";
119 // Add the request body
120 $request .= "\r\n" . $body;
122 // Do nothing - just return the request as string
129 * Return the response set in $this->setResponse()
133 public function read() {
134 if ($this->responseIndex
>= count($this->responses
)) {
135 $this->responseIndex
= 0;
137 return $this->responses
[$this->responseIndex ++
];
141 * Close the connection (dummy)
144 public function close() {}
147 * Set the HTTP response(s) to be returned by this adapter
149 * @param Zend_Http_Response|array|string $response
151 public function setResponse($response) {
152 if ($response instanceof Zend_Http_Response
) {
153 $response = $response->asString();
156 $this->responses
= (array)$response;
157 $this->responseIndex
= 0;
161 * Add another response to the response buffer.
163 * @param string $response
165 public function addResponse($response) {
166 $this->responses
[] = $response;
170 * Sets the position of the response buffer. Selects which
171 * response will be returned on the next call to read().
173 * @param integer $index
175 public function setResponseIndex($index) {
176 if ($index < 0 ||
$index >= count($this->responses
)) {
177 require_once 'external/Zend/Http/Client/Adapter/Exception.php';
178 throw new Zend_Http_Client_Adapter_Exception('Index out of range of response buffer size');
180 $this->responseIndex
= $index;