2
0

Test.php 5.4 KB

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