Test.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * @version $Id$
  19. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * @see Zend_Uri_Http
  24. */
  25. require_once 'Zend/Uri/Http.php';
  26. /**
  27. * @see Zend_Http_Response
  28. */
  29. require_once 'Zend/Http/Response.php';
  30. /**
  31. * @see Zend_Http_Client_Adapter_Interface
  32. */
  33. require_once 'Zend/Http/Client/Adapter/Interface.php';
  34. /**
  35. * A testing-purposes adapter.
  36. *
  37. * Should be used to test all components that rely on Zend_Http_Client,
  38. * without actually performing an HTTP request. You should instantiate this
  39. * object manually, and then set it as the client's adapter. Then, you can
  40. * set the expected response using the setResponse() method.
  41. *
  42. * @category Zend
  43. * @package Zend_Http
  44. * @subpackage Client_Adapter
  45. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. */
  48. class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interface
  49. {
  50. /**
  51. * Parameters array
  52. *
  53. * @var array
  54. */
  55. protected $config = array();
  56. /**
  57. * Buffer of responses to be returned by the read() method. Can be
  58. * set using setResponse() and addResponse().
  59. *
  60. * @var array
  61. */
  62. protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n");
  63. /**
  64. * Current position in the response buffer
  65. *
  66. * @var integer
  67. */
  68. protected $responseIndex = 0;
  69. /**
  70. * Adapter constructor, currently empty. Config is set using setConfig()
  71. *
  72. */
  73. public function __construct()
  74. { }
  75. /**
  76. * Set the configuration array for the adapter
  77. *
  78. * @param Zend_Config | array $config
  79. */
  80. public function setConfig($config = array())
  81. {
  82. if ($config instanceof Zend_Config) {
  83. $config = $config->toArray();
  84. } elseif (! is_array($config)) {
  85. require_once 'Zend/Http/Client/Adapter/Exception.php';
  86. throw new Zend_Http_Client_Adapter_Exception(
  87. 'Array or Zend_Config object expected, got ' . gettype($config)
  88. );
  89. }
  90. foreach ($config as $k => $v) {
  91. $this->config[strtolower($k)] = $v;
  92. }
  93. }
  94. /**
  95. * Connect to the remote server
  96. *
  97. * @param string $host
  98. * @param int $port
  99. * @param boolean $secure
  100. * @param int $timeout
  101. */
  102. public function connect($host, $port = 80, $secure = false)
  103. { }
  104. /**
  105. * Send request to the remote server
  106. *
  107. * @param string $method
  108. * @param Zend_Uri_Http $uri
  109. * @param string $http_ver
  110. * @param array $headers
  111. * @param string $body
  112. * @return string Request as string
  113. */
  114. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  115. {
  116. $host = $uri->getHost();
  117. $host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host);
  118. // Build request headers
  119. $path = $uri->getPath();
  120. if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
  121. $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
  122. foreach ($headers as $k => $v) {
  123. if (is_string($k)) $v = ucfirst($k) . ": $v";
  124. $request .= "$v\r\n";
  125. }
  126. // Add the request body
  127. $request .= "\r\n" . $body;
  128. // Do nothing - just return the request as string
  129. return $request;
  130. }
  131. /**
  132. * Return the response set in $this->setResponse()
  133. *
  134. * @return string
  135. */
  136. public function read()
  137. {
  138. if ($this->responseIndex >= count($this->responses)) {
  139. $this->responseIndex = 0;
  140. }
  141. return $this->responses[$this->responseIndex++];
  142. }
  143. /**
  144. * Close the connection (dummy)
  145. *
  146. */
  147. public function close()
  148. { }
  149. /**
  150. * Set the HTTP response(s) to be returned by this adapter
  151. *
  152. * @param Zend_Http_Response|array|string $response
  153. */
  154. public function setResponse($response)
  155. {
  156. if ($response instanceof Zend_Http_Response) {
  157. $response = $response->asString("\r\n");
  158. }
  159. $this->responses = (array)$response;
  160. $this->responseIndex = 0;
  161. }
  162. /**
  163. * Add another response to the response buffer.
  164. *
  165. * @param string Zend_Http_Response|$response
  166. */
  167. public function addResponse($response)
  168. {
  169. if ($response instanceof Zend_Http_Response) {
  170. $response = $response->asString("\r\n");
  171. }
  172. $this->responses[] = $response;
  173. }
  174. /**
  175. * Sets the position of the response buffer. Selects which
  176. * response will be returned on the next call to read().
  177. *
  178. * @param integer $index
  179. */
  180. public function setResponseIndex($index)
  181. {
  182. if ($index < 0 || $index >= count($this->responses)) {
  183. require_once 'Zend/Http/Client/Adapter/Exception.php';
  184. throw new Zend_Http_Client_Adapter_Exception(
  185. 'Index out of range of response buffer size');
  186. }
  187. $this->responseIndex = $index;
  188. }
  189. }