]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - Zend/Http/Client/Adapter/Test.php
Release 6.5.0
[Github/sugarcrm.git] / Zend / Http / Client / Adapter / Test.php
1 <?php
2 /**
3  * Zend Framework
4  *
5  * LICENSE
6  *
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.
14  *
15  * @category   Zend
16  * @package    Zend_Http
17  * @subpackage Client_Adapter
18
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
21  */
22
23 /**
24  * @see Zend_Uri_Http
25  */
26 require_once 'Zend/Uri/Http.php';
27 /**
28  * @see Zend_Http_Response
29  */
30 require_once 'Zend/Http/Response.php';
31 /**
32  * @see Zend_Http_Client_Adapter_Interface
33  */
34 require_once 'Zend/Http/Client/Adapter/Interface.php';
35
36 /**
37  * A testing-purposes adapter.
38  *
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.
43  *
44  * @category   Zend
45  * @package    Zend_Http
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
49  */
50 class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interface
51 {
52     /**
53      * Parameters array
54      *
55      * @var array
56      */
57     protected $config = array();
58
59     /**
60      * Buffer of responses to be returned by the read() method.  Can be
61      * set using setResponse() and addResponse().
62      *
63      * @var array
64      */
65     protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n");
66
67     /**
68      * Current position in the response buffer
69      *
70      * @var integer
71      */
72     protected $responseIndex = 0;
73
74     /**
75      * Wether or not the next request will fail with an exception
76      *
77      * @var boolean
78      */
79     protected $_nextRequestWillFail = false;
80
81     /**
82      * Adapter constructor, currently empty. Config is set using setConfig()
83      *
84      */
85     public function __construct()
86     { }
87
88     /**
89      * Set the nextRequestWillFail flag
90      *
91      * @param boolean $flag
92      * @return Zend_Http_Client_Adapter_Test
93      */
94     public function setNextRequestWillFail($flag)
95     {
96         $this->_nextRequestWillFail = (bool) $flag;
97
98         return $this;
99     }
100
101     /**
102      * Set the configuration array for the adapter
103      *
104      * @param Zend_Config | array $config
105      */
106     public function setConfig($config = array())
107     {
108         if ($config instanceof Zend_Config) {
109             $config = $config->toArray();
110
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)
115             );
116         }
117
118         foreach ($config as $k => $v) {
119             $this->config[strtolower($k)] = $v;
120         }
121     }
122
123
124     /**
125      * Connect to the remote server
126      *
127      * @param string  $host
128      * @param int     $port
129      * @param boolean $secure
130      * @param int     $timeout
131      * @throws Zend_Http_Client_Adapter_Exception
132      */
133     public function connect($host, $port = 80, $secure = false)
134     {
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');
139         }
140     }
141
142     /**
143      * Send request to the remote server
144      *
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
151      */
152     public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
153     {
154         $host = $uri->getHost();
155             $host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host);
156
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";
164         }
165
166         // Add the request body
167         $request .= "\r\n" . $body;
168
169         // Do nothing - just return the request as string
170
171         return $request;
172     }
173
174     /**
175      * Return the response set in $this->setResponse()
176      *
177      * @return string
178      */
179     public function read()
180     {
181         if ($this->responseIndex >= count($this->responses)) {
182             $this->responseIndex = 0;
183         }
184         return $this->responses[$this->responseIndex++];
185     }
186
187     /**
188      * Close the connection (dummy)
189      *
190      */
191     public function close()
192     { }
193
194     /**
195      * Set the HTTP response(s) to be returned by this adapter
196      *
197      * @param Zend_Http_Response|array|string $response
198      */
199     public function setResponse($response)
200     {
201         if ($response instanceof Zend_Http_Response) {
202             $response = $response->asString("\r\n");
203         }
204
205         $this->responses = (array)$response;
206         $this->responseIndex = 0;
207     }
208
209     /**
210      * Add another response to the response buffer.
211      *
212      * @param string Zend_Http_Response|$response
213      */
214     public function addResponse($response)
215     {
216          if ($response instanceof Zend_Http_Response) {
217             $response = $response->asString("\r\n");
218         }
219
220         $this->responses[] = $response;
221     }
222
223     /**
224      * Sets the position of the response buffer.  Selects which
225      * response will be returned on the next call to read().
226      *
227      * @param integer $index
228      */
229     public function setResponseIndex($index)
230     {
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');
235         }
236         $this->responseIndex = $index;
237     }
238 }