StreamTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Http\Client\Adapter;
  15. use Cake\Http\Client\Adapter\Stream;
  16. use Cake\Http\Client\Request;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * CakeStreamWrapper class
  20. */
  21. class CakeStreamWrapper implements \ArrayAccess
  22. {
  23. private $_stream;
  24. private $_query = [];
  25. private $_data = [
  26. 'headers' => [
  27. 'HTTP/1.1 200 OK',
  28. ],
  29. ];
  30. public function stream_open($path, $mode, $options, &$openedPath)
  31. {
  32. $query = parse_url($path, PHP_URL_QUERY);
  33. if ($query) {
  34. parse_str($query, $this->_query);
  35. }
  36. $this->_stream = fopen('php://memory', 'rb+');
  37. fwrite($this->_stream, str_repeat('x', 20000));
  38. rewind($this->_stream);
  39. return true;
  40. }
  41. public function stream_close()
  42. {
  43. return fclose($this->_stream);
  44. }
  45. public function stream_read($count)
  46. {
  47. if (isset($this->_query['sleep'])) {
  48. sleep(1);
  49. }
  50. return fread($this->_stream, $count);
  51. }
  52. public function stream_eof()
  53. {
  54. return feof($this->_stream);
  55. }
  56. public function stream_set_option($option, $arg1, $arg2)
  57. {
  58. return false;
  59. }
  60. public function offsetExists($offset)
  61. {
  62. return isset($this->_data[$offset]);
  63. }
  64. public function offsetGet($offset)
  65. {
  66. return $this->_data[$offset];
  67. }
  68. public function offsetSet($offset, $value)
  69. {
  70. $this->_data[$offset] = $value;
  71. }
  72. public function offsetUnset($offset)
  73. {
  74. unset($this->_data[$offset]);
  75. }
  76. }
  77. /**
  78. * HTTP stream adapter test.
  79. */
  80. class StreamTest extends TestCase
  81. {
  82. public function setUp()
  83. {
  84. parent::setUp();
  85. $this->stream = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  86. ->setMethods(['_send'])
  87. ->getMock();
  88. stream_wrapper_unregister('http');
  89. stream_wrapper_register('http', __NAMESPACE__ . '\CakeStreamWrapper');
  90. }
  91. public function tearDown()
  92. {
  93. parent::tearDown();
  94. stream_wrapper_restore('http');
  95. }
  96. /**
  97. * Test the send method
  98. *
  99. * @return void
  100. */
  101. public function testSend()
  102. {
  103. $stream = new Stream();
  104. $request = new Request('http://localhost', 'GET', [
  105. 'User-Agent' => 'CakePHP TestSuite',
  106. 'Cookie' => 'testing=value'
  107. ]);
  108. try {
  109. $responses = $stream->send($request, []);
  110. } catch (\Cake\Core\Exception\Exception $e) {
  111. $this->markTestSkipped('Could not connect to localhost, skipping');
  112. }
  113. $this->assertInstanceOf('Cake\Http\Client\Response', $responses[0]);
  114. }
  115. /**
  116. * Test the send method by using cakephp:// protocol.
  117. *
  118. * @return void
  119. */
  120. public function testSendByUsingCakephpProtocol()
  121. {
  122. $stream = new Stream();
  123. $request = new Request('http://dummy/');
  124. $responses = $stream->send($request, []);
  125. $this->assertInstanceOf('Cake\Http\Client\Response', $responses[0]);
  126. $this->assertEquals(20000, strlen($responses[0]->body()));
  127. }
  128. /**
  129. * Test building the context headers
  130. *
  131. * @return void
  132. */
  133. public function testBuildingContextHeader()
  134. {
  135. $request = new Request(
  136. 'http://localhost',
  137. 'GET',
  138. [
  139. 'User-Agent' => 'CakePHP TestSuite',
  140. 'Content-Type' => 'application/json',
  141. 'Cookie' => 'a=b; c=do%20it'
  142. ]
  143. );
  144. $options = [
  145. 'redirect' => 20,
  146. ];
  147. $this->stream->send($request, $options);
  148. $result = $this->stream->contextOptions();
  149. $expected = [
  150. 'User-Agent: CakePHP TestSuite',
  151. 'Content-Type: application/json',
  152. 'Cookie: a=b; c=do%20it',
  153. 'Connection: close',
  154. ];
  155. $this->assertEquals(implode("\r\n", $expected), $result['header']);
  156. $this->assertSame(0, $result['max_redirects']);
  157. $this->assertTrue($result['ignore_errors']);
  158. }
  159. /**
  160. * Test send() + context options with string content.
  161. *
  162. * @return void
  163. */
  164. public function testSendContextContentString()
  165. {
  166. $content = json_encode(['a' => 'b']);
  167. $request = new Request(
  168. 'http://localhost',
  169. 'GET',
  170. ['Content-Type' => 'application/json'],
  171. $content
  172. );
  173. $options = [
  174. 'redirect' => 20
  175. ];
  176. $this->stream->send($request, $options);
  177. $result = $this->stream->contextOptions();
  178. $expected = [
  179. 'Content-Type: application/json',
  180. 'Connection: close',
  181. 'User-Agent: CakePHP',
  182. ];
  183. $this->assertEquals(implode("\r\n", $expected), $result['header']);
  184. $this->assertEquals($content, $result['content']);
  185. }
  186. /**
  187. * Test send() + context options with array content.
  188. *
  189. * @return void
  190. */
  191. public function testSendContextContentArray()
  192. {
  193. $request = new Request(
  194. 'http://localhost',
  195. 'GET',
  196. [
  197. 'Content-Type' => 'application/json'
  198. ],
  199. ['a' => 'my value']
  200. );
  201. $this->stream->send($request, []);
  202. $result = $this->stream->contextOptions();
  203. $expected = [
  204. 'Content-Type: application/x-www-form-urlencoded',
  205. 'Connection: close',
  206. 'User-Agent: CakePHP',
  207. ];
  208. $this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
  209. $this->assertContains('a=my+value', $result['content']);
  210. $this->assertContains('my+value', $result['content']);
  211. }
  212. /**
  213. * Test send() + context options with array content.
  214. *
  215. * @return void
  216. */
  217. public function testSendContextContentArrayFiles()
  218. {
  219. $request = new Request(
  220. 'http://localhost',
  221. 'GET',
  222. ['Content-Type' => 'application/json'],
  223. ['upload' => fopen(CORE_PATH . 'VERSION.txt', 'r')]
  224. );
  225. $this->stream->send($request, []);
  226. $result = $this->stream->contextOptions();
  227. $this->assertContains("Content-Type: multipart/form-data", $result['header']);
  228. $this->assertContains("Connection: close\r\n", $result['header']);
  229. $this->assertContains("User-Agent: CakePHP", $result['header']);
  230. $this->assertContains('name="upload"', $result['content']);
  231. $this->assertContains('filename="VERSION.txt"', $result['content']);
  232. }
  233. /**
  234. * Test send() + context options for SSL.
  235. *
  236. * @return void
  237. */
  238. public function testSendContextSsl()
  239. {
  240. $request = new Request('https://localhost.com/test.html');
  241. $options = [
  242. 'ssl_verify_host' => true,
  243. 'ssl_verify_peer' => true,
  244. 'ssl_verify_peer_name' => true,
  245. 'ssl_verify_depth' => 9000,
  246. 'ssl_allow_self_signed' => false,
  247. 'proxy' => [
  248. 'proxy' => '127.0.0.1:8080'
  249. ]
  250. ];
  251. $this->stream->send($request, $options);
  252. $result = $this->stream->contextOptions();
  253. $expected = [
  254. 'peer_name' => 'localhost.com',
  255. 'verify_peer' => true,
  256. 'verify_peer_name' => true,
  257. 'verify_depth' => 9000,
  258. 'allow_self_signed' => false,
  259. 'proxy' => '127.0.0.1:8080',
  260. ];
  261. foreach ($expected as $k => $v) {
  262. $this->assertEquals($v, $result[$k]);
  263. }
  264. $this->assertIsReadable($result['cafile']);
  265. }
  266. /**
  267. * Test send() + context options for SSL.
  268. *
  269. * @return void
  270. */
  271. public function testSendContextSslNoVerifyPeerName()
  272. {
  273. $request = new Request('https://localhost.com/test.html');
  274. $options = [
  275. 'ssl_verify_host' => true,
  276. 'ssl_verify_peer' => true,
  277. 'ssl_verify_peer_name' => false,
  278. 'ssl_verify_depth' => 9000,
  279. 'ssl_allow_self_signed' => false,
  280. 'proxy' => [
  281. 'proxy' => '127.0.0.1:8080'
  282. ]
  283. ];
  284. $this->stream->send($request, $options);
  285. $result = $this->stream->contextOptions();
  286. $expected = [
  287. 'peer_name' => 'localhost.com',
  288. 'verify_peer' => true,
  289. 'verify_peer_name' => false,
  290. 'verify_depth' => 9000,
  291. 'allow_self_signed' => false,
  292. 'proxy' => '127.0.0.1:8080',
  293. ];
  294. foreach ($expected as $k => $v) {
  295. $this->assertEquals($v, $result[$k]);
  296. }
  297. $this->assertIsReadable($result['cafile']);
  298. }
  299. /**
  300. * The PHP stream API returns ALL the headers for ALL the requests when
  301. * there are redirects.
  302. *
  303. * @return void
  304. */
  305. public function testCreateResponseWithRedirects()
  306. {
  307. $headers = [
  308. 'HTTP/1.1 302 Found',
  309. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  310. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  311. 'X-Powered-By: PHP/5.4.9',
  312. 'Location: http://localhost/cake3/tasks/second',
  313. 'Content-Length: 0',
  314. 'Connection: close',
  315. 'Content-Type: text/html; charset=UTF-8',
  316. 'Set-Cookie: first=value',
  317. 'HTTP/1.1 302 Found',
  318. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  319. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  320. 'X-Powered-By: PHP/5.4.9',
  321. 'Location: http://localhost/cake3/tasks/third',
  322. 'Content-Length: 0',
  323. 'Connection: close',
  324. 'Content-Type: text/html; charset=UTF-8',
  325. 'Set-Cookie: second=val',
  326. 'HTTP/1.1 200 OK',
  327. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  328. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  329. 'X-Powered-By: PHP/5.4.9',
  330. 'Content-Length: 22',
  331. 'Connection: close',
  332. 'Content-Type: text/html; charset=UTF-8',
  333. 'Set-Cookie: third=works',
  334. ];
  335. $content = 'This is the third page';
  336. $responses = $this->stream->createResponses($headers, $content);
  337. $this->assertCount(3, $responses);
  338. $this->assertEquals('close', $responses[0]->getHeaderLine('Connection'));
  339. $this->assertEquals('', (string)$responses[0]->getBody());
  340. $this->assertEquals('', (string)$responses[1]->getBody());
  341. $this->assertEquals($content, (string)$responses[2]->getBody());
  342. $this->assertEquals(302, $responses[0]->getStatusCode());
  343. $this->assertEquals(302, $responses[1]->getStatusCode());
  344. $this->assertEquals(200, $responses[2]->getStatusCode());
  345. $this->assertEquals('value', $responses[0]->getCookie('first'));
  346. $this->assertNull($responses[0]->getCookie('second'));
  347. $this->assertNull($responses[0]->getCookie('third'));
  348. $this->assertNull($responses[1]->getCookie('first'));
  349. $this->assertEquals('val', $responses[1]->getCookie('second'));
  350. $this->assertNull($responses[1]->getCookie('third'));
  351. $this->assertNull($responses[2]->getCookie('first'));
  352. $this->assertNull($responses[2]->getCookie('second'));
  353. $this->assertEquals('works', $responses[2]->getCookie('third'));
  354. }
  355. /**
  356. * Test that no exception is radied when not timed out.
  357. *
  358. * @return void
  359. */
  360. public function testKeepDeadline()
  361. {
  362. $request = new Request('http://dummy/?sleep');
  363. $options = [
  364. 'timeout' => 5,
  365. ];
  366. $t = microtime(true);
  367. $stream = new Stream();
  368. $stream->send($request, $options);
  369. $this->assertLessThan(5, microtime(true) - $t);
  370. }
  371. /**
  372. * Test that an exception is raised when timed out.
  373. *
  374. * @return void
  375. */
  376. public function testMissDeadline()
  377. {
  378. $this->expectException(\Cake\Http\Exception\HttpException::class);
  379. $this->expectExceptionMessage('Connection timed out http://dummy/?sleep');
  380. $request = new Request('http://dummy/?sleep');
  381. $options = [
  382. 'timeout' => 2,
  383. ];
  384. $stream = new Stream();
  385. $stream->send($request, $options);
  386. }
  387. }