|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Uri/Http.php'; 00030 require_once 'Zend/Http/Response.php'; 00034 require_once 'Zend/Http/Client/Adapter/Interface.php'; 00035 00050 class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interface 00051 { 00057 protected $config = array(); 00058 00065 protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n"); 00066 00072 protected $responseIndex = 0; 00073 00079 protected $_nextRequestWillFail = false; 00080 00085 public function __construct() 00086 { } 00087 00094 public function setNextRequestWillFail($flag) 00095 { 00096 $this->_nextRequestWillFail = (bool) $flag; 00097 00098 return $this; 00099 } 00100 00106 public function setConfig($config = array()) 00107 { 00108 if ($config instanceof Zend_Config) { 00109 $config = $config->toArray(); 00110 00111 } elseif (! is_array($config)) { 00112 require_once 'Zend/Http/Client/Adapter/Exception.php'; 00113 throw new Zend_Http_Client_Adapter_Exception( 00114 'Array or Zend_Config object expected, got ' . gettype($config) 00115 ); 00116 } 00117 00118 foreach ($config as $k => $v) { 00119 $this->config[strtolower($k)] = $v; 00120 } 00121 } 00122 00123 00133 public function connect($host, $port = 80, $secure = false) 00134 { 00135 if ($this->_nextRequestWillFail) { 00136 $this->_nextRequestWillFail = false; 00137 require_once 'Zend/Http/Client/Adapter/Exception.php'; 00138 throw new Zend_Http_Client_Adapter_Exception('Request failed'); 00139 } 00140 } 00141 00152 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '') 00153 { 00154 $host = $uri->getHost(); 00155 $host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host); 00156 00157 // Build request headers 00158 $path = $uri->getPath(); 00159 if ($uri->getQuery()) $path .= '?' . $uri->getQuery(); 00160 $request = "{$method} {$path} HTTP/{$http_ver}\r\n"; 00161 foreach ($headers as $k => $v) { 00162 if (is_string($k)) $v = ucfirst($k) . ": $v"; 00163 $request .= "$v\r\n"; 00164 } 00165 00166 // Add the request body 00167 $request .= "\r\n" . $body; 00168 00169 // Do nothing - just return the request as string 00170 00171 return $request; 00172 } 00173 00179 public function read() 00180 { 00181 if ($this->responseIndex >= count($this->responses)) { 00182 $this->responseIndex = 0; 00183 } 00184 return $this->responses[$this->responseIndex++]; 00185 } 00186 00191 public function close() 00192 { } 00193 00199 public function setResponse($response) 00200 { 00201 if ($response instanceof Zend_Http_Response) { 00202 $response = $response->asString("\r\n"); 00203 } 00204 00205 $this->responses = (array)$response; 00206 $this->responseIndex = 0; 00207 } 00208 00214 public function addResponse($response) 00215 { 00216 if ($response instanceof Zend_Http_Response) { 00217 $response = $response->asString("\r\n"); 00218 } 00219 00220 $this->responses[] = $response; 00221 } 00222 00229 public function setResponseIndex($index) 00230 { 00231 if ($index < 0 || $index >= count($this->responses)) { 00232 require_once 'Zend/Http/Client/Adapter/Exception.php'; 00233 throw new Zend_Http_Client_Adapter_Exception( 00234 'Index out of range of response buffer size'); 00235 } 00236 $this->responseIndex = $index; 00237 } 00238 }