source: trunk/www.guidonia.net/wp/wp-content/plugins/webtv/Drivers/Zend/Http/Client/Adapter/Test.php@ 44

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