ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ClientTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\HTTP;
4 
6 
8 
9  $client = new ClientMock();
10  $client->addCurlSetting(CURLOPT_POSTREDIR, 0);
11 
12  $request = new Request('GET', 'http://example.org/', ['X-Foo' => 'bar']);
13 
14  $settings = [
15  CURLOPT_RETURNTRANSFER => true,
16  CURLOPT_HEADER => true,
17  CURLOPT_POSTREDIR => 0,
18  CURLOPT_HTTPHEADER => ['X-Foo: bar'],
19  CURLOPT_NOBODY => false,
20  CURLOPT_URL => 'http://example.org/',
21  CURLOPT_CUSTOMREQUEST => 'GET',
22  CURLOPT_POSTFIELDS => '',
23  CURLOPT_PUT => false,
24  CURLOPT_USERAGENT => 'sabre-http/' . Version::VERSION . ' (http://sabre.io/)',
25  ];
26 
27  // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM
28  // at least if this unit test fails in the future we know it is :)
29  if (defined('HHVM_VERSION') === false) {
30  $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
31  $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
32  }
33 
34 
35  $this->assertEquals($settings, $client->createCurlSettingsArray($request));
36 
37  }
38 
40 
41  $client = new ClientMock();
42  $request = new Request('HEAD', 'http://example.org/', ['X-Foo' => 'bar']);
43 
44 
45  $settings = [
46  CURLOPT_RETURNTRANSFER => true,
47  CURLOPT_HEADER => true,
48  CURLOPT_NOBODY => true,
49  CURLOPT_CUSTOMREQUEST => 'HEAD',
50  CURLOPT_HTTPHEADER => ['X-Foo: bar'],
51  CURLOPT_URL => 'http://example.org/',
52  CURLOPT_POSTFIELDS => '',
53  CURLOPT_PUT => false,
54  CURLOPT_USERAGENT => 'sabre-http/' . Version::VERSION . ' (http://sabre.io/)',
55  ];
56 
57  // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM
58  // at least if this unit test fails in the future we know it is :)
59  if (defined('HHVM_VERSION') === false) {
60  $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
61  $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
62  }
63 
64  $this->assertEquals($settings, $client->createCurlSettingsArray($request));
65 
66  }
67 
69 
70  $client = new ClientMock();
71  $request = new Request('HEAD', 'http://example.org/', ['X-Foo' => 'bar']);
72 
73  // Parsing the settings for this method, and discarding the result.
74  // This will cause the client to automatically persist previous
75  // settings and will help us detect problems.
76  $client->createCurlSettingsArray($request);
77 
78  // This is the real request.
79  $request = new Request('GET', 'http://example.org/', ['X-Foo' => 'bar']);
80 
81  $settings = [
82  CURLOPT_CUSTOMREQUEST => 'GET',
83  CURLOPT_RETURNTRANSFER => true,
84  CURLOPT_HEADER => true,
85  CURLOPT_HTTPHEADER => ['X-Foo: bar'],
86  CURLOPT_NOBODY => false,
87  CURLOPT_URL => 'http://example.org/',
88  CURLOPT_POSTFIELDS => '',
89  CURLOPT_PUT => false,
90  CURLOPT_USERAGENT => 'sabre-http/' . Version::VERSION . ' (http://sabre.io/)',
91  ];
92 
93  // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM
94  // at least if this unit test fails in the future we know it is :)
95  if (defined('HHVM_VERSION') === false) {
96  $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
97  $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
98  }
99 
100  $this->assertEquals($settings, $client->createCurlSettingsArray($request));
101 
102  }
103 
105 
106  $client = new ClientMock();
107 
108  $h = fopen('php://memory', 'r+');
109  fwrite($h, 'booh');
110  $request = new Request('PUT', 'http://example.org/', ['X-Foo' => 'bar'], $h);
111 
112  $settings = [
113  CURLOPT_RETURNTRANSFER => true,
114  CURLOPT_HEADER => true,
115  CURLOPT_PUT => true,
116  CURLOPT_INFILE => $h,
117  CURLOPT_NOBODY => false,
118  CURLOPT_CUSTOMREQUEST => 'PUT',
119  CURLOPT_HTTPHEADER => ['X-Foo: bar'],
120  CURLOPT_URL => 'http://example.org/',
121  CURLOPT_USERAGENT => 'sabre-http/' . Version::VERSION . ' (http://sabre.io/)',
122  ];
123 
124  // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM
125  // at least if this unit test fails in the future we know it is :)
126  if (defined('HHVM_VERSION') === false) {
127  $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
128  $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
129  }
130 
131  $this->assertEquals($settings, $client->createCurlSettingsArray($request));
132 
133  }
134 
136 
137  $client = new ClientMock();
138  $request = new Request('PUT', 'http://example.org/', ['X-Foo' => 'bar'], 'boo');
139 
140  $settings = [
141  CURLOPT_RETURNTRANSFER => true,
142  CURLOPT_HEADER => true,
143  CURLOPT_NOBODY => false,
144  CURLOPT_POSTFIELDS => 'boo',
145  CURLOPT_CUSTOMREQUEST => 'PUT',
146  CURLOPT_HTTPHEADER => ['X-Foo: bar'],
147  CURLOPT_URL => 'http://example.org/',
148  CURLOPT_USERAGENT => 'sabre-http/' . Version::VERSION . ' (http://sabre.io/)',
149  ];
150 
151  // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM
152  // at least if this unit test fails in the future we know it is :)
153  if (defined('HHVM_VERSION') === false) {
154  $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
155  $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
156  }
157 
158  $this->assertEquals($settings, $client->createCurlSettingsArray($request));
159 
160  }
161 
162  function testSend() {
163 
164  $client = new ClientMock();
165  $request = new Request('GET', 'http://example.org/');
166 
167  $client->on('doRequest', function($request, &$response) {
168  $response = new Response(200);
169  });
170 
171  $response = $client->send($request);
172 
173  $this->assertEquals(200, $response->getStatus());
174 
175  }
176 
177  function testSendClientError() {
178 
179  $client = new ClientMock();
180  $request = new Request('GET', 'http://example.org/');
181 
182  $client->on('doRequest', function($request, &$response) {
183  throw new ClientException('aaah', 1);
184  });
185  $called = false;
186  $client->on('exception', function() use (&$called) {
187  $called = true;
188  });
189 
190  try {
191  $client->send($request);
192  $this->fail('send() should have thrown an exception');
193  } catch (ClientException $e) {
194 
195  }
196  $this->assertTrue($called);
197 
198  }
199 
200  function testSendHttpError() {
201 
202  $client = new ClientMock();
203  $request = new Request('GET', 'http://example.org/');
204 
205  $client->on('doRequest', function($request, &$response) {
206  $response = new Response(404);
207  });
208  $called = 0;
209  $client->on('error', function() use (&$called) {
210  $called++;
211  });
212  $client->on('error:404', function() use (&$called) {
213  $called++;
214  });
215 
216  $client->send($request);
217  $this->assertEquals(2, $called);
218 
219  }
220 
221  function testSendRetry() {
222 
223  $client = new ClientMock();
224  $request = new Request('GET', 'http://example.org/');
225 
226  $called = 0;
227  $client->on('doRequest', function($request, &$response) use (&$called) {
228  $called++;
229  if ($called < 3) {
230  $response = new Response(404);
231  } else {
232  $response = new Response(200);
233  }
234  });
235 
236  $errorCalled = 0;
237  $client->on('error', function($request, $response, &$retry, $retryCount) use (&$errorCalled) {
238 
239  $errorCalled++;
240  $retry = true;
241 
242  });
243 
244  $response = $client->send($request);
245  $this->assertEquals(3, $called);
246  $this->assertEquals(2, $errorCalled);
247  $this->assertEquals(200, $response->getStatus());
248 
249  }
250 
252 
253  $client = new ClientMock();
254  $client->setThrowExceptions(true);
255  $request = new Request('GET', 'http://example.org/');
256 
257  $client->on('doRequest', function($request, &$response) {
258  $response = new Response(404);
259  });
260 
261  try {
262  $client->send($request);
263  $this->fail('An exception should have been thrown');
264  } catch (ClientHttpException $e) {
265  $this->assertEquals(404, $e->getHttpStatus());
266  $this->assertInstanceOf('Sabre\HTTP\Response', $e->getResponse());
267  }
268 
269  }
270 
271  function testParseCurlResult() {
272 
273  $client = new ClientMock();
274  $client->on('curlStuff', function(&$return) {
275 
276  $return = [
277  [
278  'header_size' => 33,
279  'http_code' => 200,
280  ],
281  0,
282  '',
283  ];
284 
285  });
286 
287  $body = "HTTP/1.1 200 OK\r\nHeader1:Val1\r\n\r\nFoo";
288  $result = $client->parseCurlResult($body, 'foobar');
289 
290  $this->assertEquals(Client::STATUS_SUCCESS, $result['status']);
291  $this->assertEquals(200, $result['http_code']);
292  $this->assertEquals(200, $result['response']->getStatus());
293  $this->assertEquals(['Header1' => ['Val1']], $result['response']->getHeaders());
294  $this->assertEquals('Foo', $result['response']->getBodyAsString());
295 
296  }
297 
298  function testParseCurlError() {
299 
300  $client = new ClientMock();
301  $client->on('curlStuff', function(&$return) {
302 
303  $return = [
304  [],
305  1,
306  'Curl error',
307  ];
308 
309  });
310 
311  $body = "HTTP/1.1 200 OK\r\nHeader1:Val1\r\n\r\nFoo";
312  $result = $client->parseCurlResult($body, 'foobar');
313 
314  $this->assertEquals(Client::STATUS_CURLERROR, $result['status']);
315  $this->assertEquals(1, $result['curl_errno']);
316  $this->assertEquals('Curl error', $result['curl_errmsg']);
317 
318  }
319 
320  function testDoRequest() {
321 
322  $client = new ClientMock();
323  $request = new Request('GET', 'http://example.org/');
324  $client->on('curlExec', function(&$return) {
325 
326  $return = "HTTP/1.1 200 OK\r\nHeader1:Val1\r\n\r\nFoo";
327 
328  });
329  $client->on('curlStuff', function(&$return) {
330 
331  $return = [
332  [
333  'header_size' => 33,
334  'http_code' => 200,
335  ],
336  0,
337  '',
338  ];
339 
340  });
341  $response = $client->doRequest($request);
342  $this->assertEquals(200, $response->getStatus());
343  $this->assertEquals(['Header1' => ['Val1']], $response->getHeaders());
344  $this->assertEquals('Foo', $response->getBodyAsString());
345 
346  }
347 
349 
350  $client = new ClientMock();
351  $request = new Request('GET', 'http://example.org/');
352  $client->on('curlExec', function(&$return) {
353 
354  $return = "";
355 
356  });
357  $client->on('curlStuff', function(&$return) {
358 
359  $return = [
360  [],
361  1,
362  'Curl error',
363  ];
364 
365  });
366 
367  try {
368  $response = $client->doRequest($request);
369  $this->fail('This should have thrown an exception');
370  } catch (ClientException $e) {
371  $this->assertEquals(1, $e->getCode());
372  $this->assertEquals('Curl error', $e->getMessage());
373  }
374 
375  }
376 
377 }
378 
379 class ClientMock extends Client {
380 
381  protected $persistedSettings = [];
382 
394 
395  $settings = parent::createCurlSettingsArray($request);
396  $settings = $settings + $this->persistedSettings;
397  $this->persistedSettings = $settings;
398  return $settings;
399 
400  }
404  function parseCurlResult($response, $curlHandle) {
405 
406  return parent::parseCurlResult($response, $curlHandle);
407 
408  }
409 
417 
418  $response = null;
419  $this->emit('doRequest', [$request, &$response]);
420 
421  // If nothing modified $response, we're using the default behavior.
422  if (is_null($response)) {
423  return parent::doRequest($request);
424  } else {
425  return $response;
426  }
427 
428  }
429 
438  protected function curlStuff($curlHandle) {
439 
440  $return = null;
441  $this->emit('curlStuff', [&$return]);
442 
443  // If nothing modified $return, we're using the default behavior.
444  if (is_null($return)) {
445  return parent::curlStuff($curlHandle);
446  } else {
447  return $return;
448  }
449 
450  }
451 
460  protected function curlExec($curlHandle) {
461 
462  $return = null;
463  $this->emit('curlExec', [&$return]);
464 
465  // If nothing modified $return, we're using the default behavior.
466  if (is_null($return)) {
467  return parent::curlExec($curlHandle);
468  } else {
469  return $return;
470  }
471 
472  }
473 
474 }
This exception may be emitted by the HTTP class, in case there was a problem emitting the request...
The RequestInterface represents a HTTP request.
getHttpStatus()
The http status code for the error.
const STATUS_SUCCESS
Definition: Client.php:455
createCurlSettingsArray(RequestInterface $request)
Making this method public.
Definition: ClientTest.php:393
$result
curlExec($curlHandle)
Calls curl_exec.
Definition: ClientTest.php:460
foreach($paths as $path) $request
Definition: asyncclient.php:32
The Request class represents a single HTTP request.
Definition: Request.php:18
$h
parseCurlResult($response, $curlHandle)
Making this method public.
Definition: ClientTest.php:404
This exception represents a HTTP error coming from the Client.
const STATUS_CURLERROR
Definition: Client.php:456
testCreateCurlSettingsArrayGETAfterHEAD()
Definition: ClientTest.php:68
doRequest(RequestInterface $request)
This method is responsible for performing a single request.
Definition: ClientTest.php:416
if($_SERVER['argc']< 4) $client
Definition: cron.php:12
getHeaders()
Returns all the HTTP headers as an array.
getResponse()
Returns the full response object.
curlStuff($curlHandle)
Returns a bunch of information about a curl request.
Definition: ClientTest.php:438
This class represents a single HTTP response.
Definition: Response.php:12
emit($eventName, array $arguments=[], callable $continueCallBack=null)
Emits an event.
getBodyAsString()
Returns the body as a string.
$response
const VERSION
Full version number.
Definition: Version.php:17
A rudimentary HTTP client.
Definition: Client.php:44