forked from zendframework/zendframework
/
CurlTest.php
executable file
·327 lines (285 loc) · 10.5 KB
/
CurlTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Http_Client
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Http\Client;
use Zend\Config\Config;
use Zend\Http\Client\Adapter;
/**
* This Testsuite includes all Zend_Http_Client that require a working web
* server to perform. It was designed to be extendable, so that several
* test suites could be run against several servers, with different client
* adapters and configurations.
*
* Note that $this->baseuri must point to a directory on a web server
* containing all the files under the _files directory. You should symlink
* or copy these files and set 'baseuri' properly.
*
* You can also set the proper constand in your test configuration file to
* point to the right place.
*
* @category Zend
* @package Zend_Http_Client
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_Http
* @group Zend_Http_Client
*/
class CurlTest extends CommonHttpTests
{
/**
* Configuration array
*
* @var array
*/
protected $config = array(
'adapter' => 'Zend\Http\Client\Adapter\Curl'
);
protected function setUp()
{
if (!extension_loaded('curl')) {
$this->markTestSkipped('cURL is not installed, marking all Http Client Curl Adapter tests skipped.');
}
parent::setUp();
}
public function testSimpleRequests()
{
$this->markTestSkipped('Method PATCH not implemented.');
}
/**
* @dataProvider parameterArrayProvider
*/
public function testPatchData($params)
{
$this->markTestSkipped('Method PATCH not implemented.');
}
/**
* Off-line common adapter tests
*/
/**
* Test that we can set a valid configuration array with some options
*
*/
public function testConfigSetAsArray()
{
$config = array(
'timeout' => 500,
'someoption' => 'hasvalue'
);
$this->_adapter->setOptions($config);
$hasConfig = $this->_adapter->getConfig();
foreach($config as $k => $v) {
$this->assertEquals($v, $hasConfig[$k]);
}
}
/**
* Test that a Zend_Config object can be used to set configuration
*
* @link http://framework.zend.com/issues/browse/ZF-5577
*/
public function testConfigSetAsZendConfig()
{
$config = new Config(array(
'timeout' => 400,
'nested' => array(
'item' => 'value',
)
));
$this->_adapter->setOptions($config);
$hasConfig = $this->_adapter->getConfig();
$this->assertEquals($config->timeout, $hasConfig['timeout']);
$this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
}
/**
* Check that an exception is thrown when trying to set invalid config
*
* @dataProvider invalidConfigProvider
*/
public function testSetConfigInvalidConfig($config)
{
$this->setExpectedException(
'Zend\Http\Client\Adapter\Exception\InvalidArgumentException',
'Array or Traversable object expected');
$this->_adapter->setOptions($config);
}
/**
* CURLOPT_CLOSEPOLICY never worked and returns false on setopt always:
* @link http://de2.php.net/manual/en/function.curl-setopt.php#84277
*
* This should throw an exception.
*/
public function testSettingInvalidCurlOption()
{
$config = array(
'adapter' => 'Zend\Http\Client\Adapter\Curl',
'curloptions' => array(CURLOPT_CLOSEPOLICY => true),
);
$this->client = new \Zend\Http\Client($this->client->getUri(true), $config);
$this->setExpectedException(
'Zend\Http\Client\Adapter\Exception\RuntimeException',
'Unknown or erroreous cURL option'
);
$this->client->send();
}
public function testRedirectWithGetOnly()
{
$this->client->setUri($this->baseuri . 'testRedirections.php');
// Set some parameters
$this->client->setParameterGet(array('swallow', 'african'));
// Request
$res = $this->client->send();
$this->assertEquals(3, $this->client->getRedirectionsCount(), 'Redirection counter is not as expected');
// Make sure the body does *not* contain the set parameters
$this->assertNotContains('swallow', $res->getBody());
$this->assertNotContains('Camelot', $res->getBody());
}
/**
* This is a specific problem of the request type: If you let cURL handle redirects internally
* but start with a POST request that sends data then the location ping-pong will lead to an
* Content-Length: x\r\n GET request of the client that the server won't answer because no content is sent.
*
* Set CURLOPT_FOLLOWLOCATION = false for this type of request and let the Zend_Http_Client handle redirects
* in his own loop.
*
*/
public function testRedirectPostToGetWithCurlFollowLocationOptionLeadsToTimeout()
{
$adapter = new Adapter\Curl();
$this->client->setAdapter($adapter);
$adapter->setOptions(array(
'curloptions' => array(
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 1,
))
);
$this->client->setUri($this->baseuri . 'testRedirections.php');
// Set some parameters
$this->client->setParameterGet(array ('swallow' => 'african'));
$this->client->setParameterPost(array ('Camelot' => 'A silly place'));
$this->client->setMethod('POST');
$this->setExpectedException(
'Zend\Http\Client\Adapter\Exception\RuntimeException',
'Error in cURL request: Operation timed out after 1000 milliseconds with 0 bytes received');
$this->client->send();
}
/**
* @group ZF-3758
* @link http://framework.zend.com/issues/browse/ZF-3758
*/
public function testPutFileContentWithHttpClient()
{
// Method 1: Using the binary string of a file to PUT
$this->client->setUri($this->baseuri . 'testRawPostData.php');
$putFileContents = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
'_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg');
$this->client->setRawBody($putFileContents);
$this->client->setMethod('PUT');
$this->client->send();
$this->assertEquals($putFileContents, $this->client->getResponse()->getBody());
}
/**
* @group ZF-3758
* @link http://framework.zend.com/issues/browse/ZF-3758
*/
public function testPutFileHandleWithHttpClient()
{
$this->client->setUri($this->baseuri . 'testRawPostData.php');
$putFileContents = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
'_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg');
// Method 2: Using a File-Handle to the file to PUT the data
$putFilePath = dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
'_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg';
$putFileHandle = fopen($putFilePath, "r");
$putFileSize = filesize($putFilePath);
$adapter = new Adapter\Curl();
$this->client->setAdapter($adapter);
$adapter->setOptions(array(
'curloptions' => array(CURLOPT_INFILE => $putFileHandle, CURLOPT_INFILESIZE => $putFileSize)
));
$this->client->setMethod('PUT');
$this->client->send();
$this->assertEquals(gzcompress($putFileContents), gzcompress($this->client->getResponse()->getBody()));
}
public function testWritingAndNotConnectedWithCurlHandleThrowsException()
{
$adapter = new Adapter\Curl();
$this->setExpectedException('Zend\Http\Client\Adapter\Exception\RuntimeException',
'Trying to write but we are not connected');
$adapter->write("GET", "someUri");
}
public function testSetConfigIsNotArray()
{
$adapter = new Adapter\Curl();
$this->setExpectedException('Zend\Http\Client\Adapter\Exception\InvalidArgumentException');
$adapter->setOptions("foo");
}
public function testSetCurlOptions()
{
$adapter = new Adapter\Curl();
$adapter->setCurlOption('foo', 'bar')
->setCurlOption('bar', 'baz');
$this->assertEquals(
array('curloptions' => array('foo' => 'bar', 'bar' => 'baz')),
$this->readAttribute($adapter, 'config')
);
}
public function testWorkWithProxyConfiguration()
{
$adapter = new Adapter\Curl();
$adapter->setOptions(array(
'proxy_host' => 'localhost',
'proxy_port' => 80,
'proxy_user' => 'foo',
'proxy_pass' => 'baz',
));
$expected = array(
'curloptions' => array(
CURLOPT_PROXYUSERPWD => 'foo:baz',
CURLOPT_PROXY => 'localhost',
CURLOPT_PROXYPORT => 80,
),
);
$this->assertEquals(
$expected, $this->readAttribute($adapter, 'config')
);
}
/**
* @group ZF-7040
*/
public function testGetCurlHandle()
{
$adapter = new Adapter\Curl();
$adapter->setOptions(array('timeout' => 2, 'maxredirects' => 1));
$adapter->connect("http://framework.zend.com");
$this->assertTrue(is_resource($adapter->getHandle()));
}
/**
* @group ZF-9857
*/
public function testHeadRequest()
{
$this->client->setUri($this->baseuri . 'testRawPostData.php');
$adapter = new Adapter\Curl();
$this->client->setAdapter($adapter);
$this->client->setMethod('HEAD');
$this->client->send();
$this->assertEquals('', $this->client->getResponse()->getBody());
}
}