CurlTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\net\socket;
  9. use lithium\net\http\Response;
  10. use lithium\net\http\Request;
  11. use lithium\net\socket\Curl;
  12. class CurlTest extends \lithium\test\Unit {
  13. protected $_testConfig = array(
  14. 'persistent' => false,
  15. 'scheme' => 'http',
  16. 'host' => 'google.com',
  17. 'port' => 80,
  18. 'timeout' => 2,
  19. 'classes' => array('request' => 'lithium\net\http\Request')
  20. );
  21. /**
  22. * Skip the test if curl is not available in your PHP installation.
  23. *
  24. * @return void
  25. */
  26. public function skip() {
  27. $message = 'Your PHP installation was not compiled with curl support.';
  28. $this->skipIf(!function_exists('curl_init'), $message);
  29. $config = $this->_testConfig;
  30. $url = "{$config['scheme']}://{$config['host']}";
  31. $message = "Could not open {$url} - skipping " . __CLASS__;
  32. $this->skipIf(!curl_init($url), $message);
  33. $message = "No internet connection established.";
  34. $this->skipIf(!$this->_hasNetwork($this->_testConfig), $message);
  35. }
  36. public function testAllMethodsNoConnection() {
  37. $stream = new Curl(array('scheme' => null));
  38. $this->assertFalse($stream->open());
  39. $this->assertTrue($stream->close());
  40. $this->assertFalse($stream->timeout(2));
  41. $this->assertFalse($stream->encoding('UTF-8'));
  42. $this->assertFalse($stream->write(null));
  43. $this->assertFalse($stream->read());
  44. }
  45. public function testOpen() {
  46. $stream = new Curl($this->_testConfig);
  47. $result = $stream->open();
  48. $this->assertTrue($result);
  49. $result = $stream->resource();
  50. $this->assertTrue(is_resource($result));
  51. }
  52. public function testClose() {
  53. $stream = new Curl($this->_testConfig);
  54. $result = $stream->open();
  55. $this->assertTrue($result);
  56. $result = $stream->close();
  57. $this->assertTrue($result);
  58. $result = $stream->resource();
  59. $this->assertFalse(is_resource($result));
  60. }
  61. public function testTimeout() {
  62. $stream = new Curl($this->_testConfig);
  63. $result = $stream->open();
  64. $stream->timeout(10);
  65. $result = $stream->resource();
  66. $this->assertTrue(is_resource($result));
  67. }
  68. public function testEncoding() {
  69. $stream = new Curl($this->_testConfig);
  70. $result = $stream->open();
  71. $stream->encoding('UTF-8');
  72. $result = $stream->resource();
  73. $this->assertTrue(is_resource($result));
  74. $stream = new Curl($this->_testConfig + array('encoding' => 'UTF-8'));
  75. $result = $stream->open();
  76. $result = $stream->resource();
  77. $this->assertTrue(is_resource($result));
  78. }
  79. public function testWriteAndRead() {
  80. $stream = new Curl($this->_testConfig);
  81. $this->assertTrue(is_resource($stream->open()));
  82. $this->assertTrue(is_resource($stream->resource()));
  83. $this->assertEqual(1, $stream->write());
  84. $this->assertPattern("/^HTTP/", (string) $stream->read());
  85. }
  86. public function testSendWithNull() {
  87. $stream = new Curl($this->_testConfig);
  88. $this->assertTrue(is_resource($stream->open()));
  89. $result = $stream->send(
  90. new Request($this->_testConfig),
  91. array('response' => 'lithium\net\http\Response')
  92. );
  93. $this->assertTrue($result instanceof Response);
  94. $this->assertPattern("/^HTTP/", (string) $result);
  95. }
  96. public function testSendWithArray() {
  97. $stream = new Curl($this->_testConfig);
  98. $this->assertTrue(is_resource($stream->open()));
  99. $result = $stream->send($this->_testConfig,
  100. array('response' => 'lithium\net\http\Response')
  101. );
  102. $this->assertTrue($result instanceof Response);
  103. $this->assertPattern("/^HTTP/", (string) $result);
  104. }
  105. public function testSendWithObject() {
  106. $stream = new Curl($this->_testConfig);
  107. $this->assertTrue(is_resource($stream->open()));
  108. $result = $stream->send(
  109. new Request($this->_testConfig),
  110. array('response' => 'lithium\net\http\Response')
  111. );
  112. $this->assertTrue($result instanceof Response);
  113. $this->assertPattern("/^HTTP/", (string) $result);
  114. }
  115. public function testSettingOfOptions() {
  116. $stream = new Curl($this->_testConfig);
  117. $stream->set('DummyFlag', 'Dummy Value');
  118. $stream->set('DummyFlag', 'Changed Dummy Value');
  119. $this->assertEqual('Changed Dummy Value', $stream->options['DummyFlag']);
  120. }
  121. public function testSettingOfOptionsInConfig() {
  122. $config = $this->_testConfig + array('options' => array('DummyFlag' => 'Dummy Value'));
  123. $stream = new Curl($config);
  124. $stream->open();
  125. $this->assertEqual('Dummy Value', $stream->options['DummyFlag']);
  126. }
  127. public function testSettingOfOptionsInOpen() {
  128. $stream = new Curl($this->_testConfig);
  129. $stream->open(array('options' => array('DummyFlag' => 'Dummy Value')));
  130. $this->assertEqual('Dummy Value', $stream->options['DummyFlag']);
  131. }
  132. public function testSendPostThenGet() {
  133. $postConfig = array('method' => 'POST', 'body' => '{"body"}');
  134. $stream = new Curl($this->_testConfig);
  135. $this->assertTrue(is_resource($stream->open()));
  136. $this->assertTrue($stream->write(new Request($postConfig + $this->_testConfig)));
  137. $this->assertTrue(isset($stream->options[CURLOPT_POST]));
  138. $this->assertTrue($stream->close());
  139. $this->assertTrue(is_resource($stream->open()));
  140. $this->assertTrue($stream->write(new Request($this->_testConfig)));
  141. $this->assertFalse(isset($stream->options[CURLOPT_POST]));
  142. $this->assertTrue($stream->close());
  143. }
  144. public function testSendPutThenGet() {
  145. $postConfig = array('method' => 'PUT', 'body' => '{"body"}');
  146. $stream = new Curl($this->_testConfig);
  147. $this->assertTrue(is_resource($stream->open()));
  148. $this->assertTrue($stream->write(new Request($postConfig + $this->_testConfig)));
  149. $this->assertTrue(isset($stream->options[CURLOPT_CUSTOMREQUEST]));
  150. $this->assertEqual($stream->options[CURLOPT_CUSTOMREQUEST],'PUT');
  151. $this->assertTrue(isset($stream->options[CURLOPT_POSTFIELDS]));
  152. $this->assertEqual($stream->options[CURLOPT_POSTFIELDS],$postConfig['body']);
  153. $this->assertTrue($stream->close());
  154. $this->assertTrue(is_resource($stream->open()));
  155. $this->assertTrue($stream->write(new Request($this->_testConfig)));
  156. $this->assertFalse(isset($stream->options[CURLOPT_CUSTOMREQUEST]));
  157. $this->assertTrue($stream->close());
  158. }
  159. }
  160. ?>