4
* @subpackage UnitTests
7
require_once dirname(__FILE__)."/../../TestHelper.php";
9
/** Zend_Rest_Client */
10
require_once 'Zend/Rest/Client.php';
12
/** Zend_Http_Client_Adapter_Test */
13
require_once 'Zend/Http/Client/Adapter/Test.php';
15
/** PHPUnit Test Case */
16
require_once 'PHPUnit/Framework/TestCase.php';
19
* Test cases for Zend_Rest_Client
22
* @subpackage UnitTests
24
class Zend_Rest_ClientTest extends PHPUnit_Framework_TestCase
26
public function setUp()
28
$this->path = dirname(__FILE__) . '/responses/';
30
$this->adapter = new Zend_Http_Client_Adapter_Test();
31
$client = new Zend_Http_Client(null, array(
32
'adapter' => $this->adapter
34
Zend_Rest_Client::setHttpClient($client);
36
$this->rest = new Zend_Rest_Client('http://framework.zend.com/');
39
public function testUri()
41
$client = new Zend_Rest_Client('http://framework.zend.com/rest/');
42
$uri = $client->getUri();
43
$this->assertTrue($uri instanceof Zend_Uri_Http);
44
$this->assertEquals('http://framework.zend.com/rest/', $uri->getUri());
46
$client->setUri(Zend_Uri::factory('http://framework.zend.com/soap/'));
47
$uri = $client->getUri();
48
$this->assertTrue($uri instanceof Zend_Uri_Http);
49
$this->assertEquals('http://framework.zend.com/soap/', $uri->getUri());
51
$client->setUri('http://framework.zend.com/xmlrpc/');
52
$uri = $client->getUri();
53
$this->assertTrue($uri instanceof Zend_Uri_Http);
54
$this->assertEquals('http://framework.zend.com/xmlrpc/', $uri->getUri());
57
public function testRestGetThrowsExceptionWithNoUri()
59
$expXml = file_get_contents($this->path . 'returnString.xml');
60
$response = "HTTP/1.0 200 OK\r\n"
61
. "X-powered-by: PHP/5.2.0\r\n"
62
. "Content-type: text/xml\r\n"
63
. "Content-length: " . strlen($expXml) . "\r\n"
64
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
65
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
66
. "Connection: close\r\n"
69
$this->adapter->setResponse($response);
71
$rest = new Zend_Rest_Client();
74
$response = $rest->restGet('/rest/');
75
$this->fail('Should throw exception if no URI in object');
76
} catch (Exception $e) {
81
public function testRestFixesPathWithMissingSlashes()
83
$expXml = file_get_contents($this->path . 'returnString.xml');
84
$response = "HTTP/1.0 200 OK\r\n"
85
. "X-powered-by: PHP/5.2.0\r\n"
86
. "Content-type: text/xml\r\n"
87
. "Content-length: " . strlen($expXml) . "\r\n"
88
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
89
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
90
. "Connection: close\r\n"
93
$this->adapter->setResponse($response);
95
$rest = new Zend_Rest_Client('http://framework.zend.com');
97
$response = $rest->restGet('rest');
98
$this->assertTrue($response instanceof Zend_Http_Response);
99
$this->assertContains($expXml, $response->getBody());
102
public function testRestGet()
104
$expXml = file_get_contents($this->path . 'returnString.xml');
105
$response = "HTTP/1.0 200 OK\r\n"
106
. "X-powered-by: PHP/5.2.0\r\n"
107
. "Content-type: text/xml\r\n"
108
. "Content-length: " . strlen($expXml) . "\r\n"
109
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
110
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
111
. "Connection: close\r\n"
114
$this->adapter->setResponse($response);
116
$response = $this->rest->restGet('/rest/');
117
$this->assertTrue($response instanceof Zend_Http_Response);
118
$this->assertContains($expXml, $response->getBody());
121
public function testRestPost()
123
$expXml = file_get_contents($this->path . 'returnString.xml');
124
$response = "HTTP/1.0 200 OK\r\n"
125
. "X-powered-by: PHP/5.2.0\r\n"
126
. "Content-type: text/xml\r\n"
127
. "Content-length: " . strlen($expXml) . "\r\n"
128
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
129
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
130
. "Connection: close\r\n"
133
$this->adapter->setResponse($response);
135
$reqXml = file_get_contents($this->path . 'returnInt.xml');
136
$response = $this->rest->restPost('/rest/', $reqXml);
137
$this->assertTrue($response instanceof Zend_Http_Response);
138
$body = $response->getBody();
139
$this->assertContains($expXml, $response->getBody());
141
$request = Zend_Rest_Client::getHttpClient()->getLastRequest();
142
$this->assertContains($reqXml, $request, $request);
145
public function testRestPostWithArrayData()
147
$expXml = file_get_contents($this->path . 'returnString.xml');
148
$response = "HTTP/1.0 200 OK\r\n"
149
. "X-powered-by: PHP/5.2.0\r\n"
150
. "Content-type: text/xml\r\n"
151
. "Content-length: " . strlen($expXml) . "\r\n"
152
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
153
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
154
. "Connection: close\r\n"
157
$this->adapter->setResponse($response);
159
$response = $this->rest->restPost('/rest/', array('foo' => 'bar', 'baz' => 'bat'));
160
$this->assertTrue($response instanceof Zend_Http_Response);
161
$body = $response->getBody();
162
$this->assertContains($expXml, $response->getBody());
164
$request = Zend_Rest_Client::getHttpClient()->getLastRequest();
165
$this->assertContains('foo=bar&baz=bat', $request, $request);
168
public function testRestPut()
170
$expXml = file_get_contents($this->path . 'returnString.xml');
171
$response = "HTTP/1.0 200 OK\r\n"
172
. "X-powered-by: PHP/5.2.0\r\n"
173
. "Content-type: text/xml\r\n"
174
. "Content-length: " . strlen($expXml) . "\r\n"
175
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
176
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
177
. "Connection: close\r\n"
180
$this->adapter->setResponse($response);
182
$reqXml = file_get_contents($this->path . 'returnInt.xml');
183
$response = $this->rest->restPut('/rest/', $reqXml);
184
$this->assertTrue($response instanceof Zend_Http_Response);
185
$body = $response->getBody();
186
$this->assertContains($expXml, $response->getBody());
188
$request = Zend_Rest_Client::getHttpClient()->getLastRequest();
189
$this->assertContains($reqXml, $request, $request);
192
public function testRestDelete()
194
$expXml = file_get_contents($this->path . 'returnString.xml');
195
$response = "HTTP/1.0 200 OK\r\n"
196
. "X-powered-by: PHP/5.2.0\r\n"
197
. "Content-type: text/xml\r\n"
198
. "Content-length: " . strlen($expXml) . "\r\n"
199
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
200
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
201
. "Connection: close\r\n"
204
$this->adapter->setResponse($response);
206
$reqXml = file_get_contents($this->path . 'returnInt.xml');
207
$response = $this->rest->restDelete('/rest/');
208
$this->assertTrue($response instanceof Zend_Http_Response);
209
$body = $response->getBody();
210
$this->assertContains($expXml, $response->getBody());
213
public function testCallWithHttpMethod()
215
$expXml = file_get_contents($this->path . 'returnString.xml');
216
$response = "HTTP/1.0 200 OK\r\n"
217
. "X-powered-by: PHP/5.2.0\r\n"
218
. "Content-type: text/xml\r\n"
219
. "Content-length: " . strlen($expXml) . "\r\n"
220
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
221
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
222
. "Connection: close\r\n"
225
$this->adapter->setResponse($response);
227
$response = $this->rest->get('/rest/');
228
$this->assertTrue($response instanceof Zend_Rest_Client_Result);
229
$this->assertTrue($response->isSuccess());
230
$this->assertEquals('string', $response->response());
233
public function testCallAsObjectMethodReturnsClient()
235
$expXml = file_get_contents($this->path . 'returnString.xml');
236
$response = "HTTP/1.0 200 OK\r\n"
237
. "X-powered-by: PHP/5.2.0\r\n"
238
. "Content-type: text/xml\r\n"
239
. "Content-length: " . strlen($expXml) . "\r\n"
240
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
241
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
242
. "Connection: close\r\n"
245
$this->adapter->setResponse($response);
247
$response = $this->rest->doStuff('why', 'not');
248
$this->assertTrue($response instanceof Zend_Rest_Client);
249
$this->assertSame($this->rest, $response);
252
public function testCallAsObjectMethodChainPerformsRequest()
254
$expXml = file_get_contents($this->path . 'returnString.xml');
255
$response = "HTTP/1.0 200 OK\r\n"
256
. "X-powered-by: PHP/5.2.0\r\n"
257
. "Content-type: text/xml\r\n"
258
. "Content-length: " . strlen($expXml) . "\r\n"
259
. "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
260
. "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
261
. "Connection: close\r\n"
264
$this->adapter->setResponse($response);
266
$response = $this->rest->doStuff('why', 'not')->get();
267
$this->assertTrue($response instanceof Zend_Rest_Client_Result);
268
$this->assertEquals('string', $response->response());
275
public function testInvalidXmlInClientResultLeadsToException()
278
$result = new Zend_Rest_Client_Result("invalidxml");
280
} catch(Zend_Rest_Client_Result_Exception $e) {