Test.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 array $config
  79. */
  80. public function setConfig($config = array())
  81. {
  82. if (! is_array($config)) {
  83. require_once 'Zend/Http/Client/Adapter/Exception.php';
  84. throw new Zend_Http_Client_Adapter_Exception(
  85. '$config expects an array, ' . gettype($config) . ' recieved.');
  86. }
  87. foreach ($config as $k => $v) {
  88. $this->config[strtolower($k)] = $v;
  89. }
  90. }
  91. /**
  92. * Connect to the remote server
  93. *
  94. * @param string $host
  95. * @param int $port
  96. * @param boolean $secure
  97. * @param int $timeout
  98. */
  99. public function connect($host, $port = 80, $secure = false)
  100. { }
  101. /**
  102. * Send request to the remote server
  103. *
  104. * @param string $method
  105. * @param Zend_Uri_Http $uri
  106. * @param string $http_ver
  107. * @param array $headers
  108. * @param string $body
  109. * @return string Request as string
  110. */
  111. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  112. {
  113. $host = $uri->getHost();
  114. $host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host);
  115. // Build request headers
  116. $path = $uri->getPath();
  117. if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
  118. $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
  119. foreach ($headers as $k => $v) {
  120. if (is_string($k)) $v = ucfirst($k) . ": $v";
  121. $request .= "$v\r\n";
  122. }
  123. // Add the request body
  124. $request .= "\r\n" . $body;
  125. // Do nothing - just return the request as string
  126. return $request;
  127. }
  128. /**
  129. * Return the response set in $this->setResponse()
  130. *
  131. * @return string
  132. */
  133. public function read()
  134. {
  135. if ($this->responseIndex >= count($this->responses)) {
  136. $this->responseIndex = 0;
  137. }
  138. return $this->responses[$this->responseIndex++];
  139. }
  140. /**
  141. * Close the connection (dummy)
  142. *
  143. */
  144. public function close()
  145. { }
  146. /**
  147. * Set the HTTP response(s) to be returned by this adapter
  148. *
  149. * @param Zend_Http_Response|array|string $response
  150. */
  151. public function setResponse($response)
  152. {
  153. if ($response instanceof Zend_Http_Response) {
  154. $response = $response->asString("\r\n");
  155. }
  156. $this->responses = (array)$response;
  157. $this->responseIndex = 0;
  158. }
  159. /**
  160. * Add another response to the response buffer.
  161. *
  162. * @param string Zend_Http_Response|$response
  163. */
  164. public function addResponse($response)
  165. {
  166. if ($response instanceof Zend_Http_Response) {
  167. $response = $response->asString("\r\n");
  168. }
  169. $this->responses[] = $response;
  170. }
  171. /**
  172. * Sets the position of the response buffer. Selects which
  173. * response will be returned on the next call to read().
  174. *
  175. * @param integer $index
  176. */
  177. public function setResponseIndex($index)
  178. {
  179. if ($index < 0 || $index >= count($this->responses)) {
  180. require_once 'Zend/Http/Client/Adapter/Exception.php';
  181. throw new Zend_Http_Client_Adapter_Exception(
  182. 'Index out of range of response buffer size');
  183. }
  184. $this->responseIndex = $index;
  185. }
  186. }