StreamTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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();
  105. $request->url('http://localhost')
  106. ->header('User-Agent', 'CakePHP TestSuite')
  107. ->cookie('testing', 'value');
  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();
  124. $request->url('http://dummy/');
  125. $responses = $stream->send($request, []);
  126. $this->assertInstanceOf('Cake\Http\Client\Response', $responses[0]);
  127. $this->assertEquals(20000, strlen($responses[0]->body()));
  128. }
  129. /**
  130. * Test building the context headers
  131. *
  132. * @return void
  133. */
  134. public function testBuildingContextHeader()
  135. {
  136. $request = new Request();
  137. $request->url('http://localhost')
  138. ->header([
  139. 'User-Agent' => 'CakePHP TestSuite',
  140. 'Content-Type' => 'application/json',
  141. 'Cookie' => 'a=b; c=do%20it'
  142. ]);
  143. $options = [
  144. 'redirect' => 20,
  145. ];
  146. $this->stream->send($request, $options);
  147. $result = $this->stream->contextOptions();
  148. $expected = [
  149. 'Connection: close',
  150. 'User-Agent: CakePHP TestSuite',
  151. 'Content-Type: application/json',
  152. 'Cookie: a=b; c=do%20it',
  153. ];
  154. $this->assertEquals(implode("\r\n", $expected), $result['header']);
  155. $this->assertSame(0, $result['max_redirects']);
  156. $this->assertTrue($result['ignore_errors']);
  157. }
  158. /**
  159. * Test send() + context options with string content.
  160. *
  161. * @return void
  162. */
  163. public function testSendContextContentString()
  164. {
  165. $content = json_encode(['a' => 'b']);
  166. $request = new Request();
  167. $request->url('http://localhost')
  168. ->header([
  169. 'Content-Type' => 'application/json'
  170. ])
  171. ->body($content);
  172. $options = [
  173. 'redirect' => 20
  174. ];
  175. $this->stream->send($request, $options);
  176. $result = $this->stream->contextOptions();
  177. $expected = [
  178. 'Connection: close',
  179. 'User-Agent: CakePHP',
  180. 'Content-Type: application/json',
  181. ];
  182. $this->assertEquals(implode("\r\n", $expected), $result['header']);
  183. $this->assertEquals($content, $result['content']);
  184. }
  185. /**
  186. * Test send() + context options with array content.
  187. *
  188. * @return void
  189. */
  190. public function testSendContextContentArray()
  191. {
  192. $request = new Request();
  193. $request->url('http://localhost')
  194. ->header([
  195. 'Content-Type' => 'application/json'
  196. ])
  197. ->body(['a' => 'my value']);
  198. $this->stream->send($request, []);
  199. $result = $this->stream->contextOptions();
  200. $expected = [
  201. 'Connection: close',
  202. 'User-Agent: CakePHP',
  203. 'Content-Type: application/x-www-form-urlencoded',
  204. ];
  205. $this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
  206. $this->assertContains('a=my+value', $result['content']);
  207. $this->assertContains('my+value', $result['content']);
  208. }
  209. /**
  210. * Test send() + context options with array content.
  211. *
  212. * @return void
  213. */
  214. public function testSendContextContentArrayFiles()
  215. {
  216. $request = new Request();
  217. $request->url('http://localhost')
  218. ->header([
  219. 'Content-Type' => 'application/json'
  220. ])
  221. ->body(['upload' => fopen(CORE_PATH . 'VERSION.txt', 'r')]);
  222. $this->stream->send($request, []);
  223. $result = $this->stream->contextOptions();
  224. $expected = [
  225. 'Connection: close',
  226. 'User-Agent: CakePHP',
  227. 'Content-Type: multipart/form-data',
  228. ];
  229. $this->assertStringStartsWith(implode("\r\n", $expected), $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();
  241. $request->url('https://localhost.com/test.html');
  242. $options = [
  243. 'ssl_verify_host' => true,
  244. 'ssl_verify_peer' => true,
  245. 'ssl_verify_peer_name' => true,
  246. 'ssl_verify_depth' => 9000,
  247. 'ssl_allow_self_signed' => false,
  248. 'proxy' => [
  249. 'proxy' => '127.0.0.1:8080'
  250. ]
  251. ];
  252. $this->stream->send($request, $options);
  253. $result = $this->stream->contextOptions();
  254. $expected = [
  255. 'peer_name' => 'localhost.com',
  256. 'verify_peer' => true,
  257. 'verify_peer_name' => true,
  258. 'verify_depth' => 9000,
  259. 'allow_self_signed' => false,
  260. 'proxy' => '127.0.0.1:8080',
  261. ];
  262. foreach ($expected as $k => $v) {
  263. $this->assertEquals($v, $result[$k]);
  264. }
  265. $this->assertIsReadable($result['cafile']);
  266. }
  267. /**
  268. * Test send() + context options for SSL.
  269. *
  270. * @return void
  271. */
  272. public function testSendContextSslNoVerifyPeerName()
  273. {
  274. $request = new Request();
  275. $request->url('https://localhost.com/test.html');
  276. $options = [
  277. 'ssl_verify_host' => true,
  278. 'ssl_verify_peer' => true,
  279. 'ssl_verify_peer_name' => false,
  280. 'ssl_verify_depth' => 9000,
  281. 'ssl_allow_self_signed' => false,
  282. 'proxy' => [
  283. 'proxy' => '127.0.0.1:8080'
  284. ]
  285. ];
  286. $this->stream->send($request, $options);
  287. $result = $this->stream->contextOptions();
  288. $expected = [
  289. 'peer_name' => 'localhost.com',
  290. 'verify_peer' => true,
  291. 'verify_peer_name' => false,
  292. 'verify_depth' => 9000,
  293. 'allow_self_signed' => false,
  294. 'proxy' => '127.0.0.1:8080',
  295. ];
  296. foreach ($expected as $k => $v) {
  297. $this->assertEquals($v, $result[$k]);
  298. }
  299. $this->assertIsReadable($result['cafile']);
  300. }
  301. /**
  302. * The PHP stream API returns ALL the headers for ALL the requests when
  303. * there are redirects.
  304. *
  305. * @return void
  306. */
  307. public function testCreateResponseWithRedirects()
  308. {
  309. $headers = [
  310. 'HTTP/1.1 302 Found',
  311. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  312. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  313. 'X-Powered-By: PHP/5.4.9',
  314. 'Location: http://localhost/cake3/tasks/second',
  315. 'Content-Length: 0',
  316. 'Connection: close',
  317. 'Content-Type: text/html; charset=UTF-8',
  318. 'Set-Cookie: first=value',
  319. 'HTTP/1.1 302 Found',
  320. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  321. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  322. 'X-Powered-By: PHP/5.4.9',
  323. 'Location: http://localhost/cake3/tasks/third',
  324. 'Content-Length: 0',
  325. 'Connection: close',
  326. 'Content-Type: text/html; charset=UTF-8',
  327. 'Set-Cookie: second=val',
  328. 'HTTP/1.1 200 OK',
  329. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  330. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  331. 'X-Powered-By: PHP/5.4.9',
  332. 'Content-Length: 22',
  333. 'Connection: close',
  334. 'Content-Type: text/html; charset=UTF-8',
  335. 'Set-Cookie: third=works',
  336. ];
  337. $content = 'This is the third page';
  338. $responses = $this->stream->createResponses($headers, $content);
  339. $this->assertCount(3, $responses);
  340. $this->assertEquals('close', $responses[0]->header('Connection'));
  341. $this->assertEquals('', $responses[0]->body());
  342. $this->assertEquals('', $responses[1]->body());
  343. $this->assertEquals($content, $responses[2]->body());
  344. $this->assertEquals(302, $responses[0]->statusCode());
  345. $this->assertEquals(302, $responses[1]->statusCode());
  346. $this->assertEquals(200, $responses[2]->statusCode());
  347. $this->assertEquals('value', $responses[0]->cookie('first'));
  348. $this->assertEquals(null, $responses[0]->cookie('second'));
  349. $this->assertEquals(null, $responses[0]->cookie('third'));
  350. $this->assertEquals(null, $responses[1]->cookie('first'));
  351. $this->assertEquals('val', $responses[1]->cookie('second'));
  352. $this->assertEquals(null, $responses[1]->cookie('third'));
  353. $this->assertEquals(null, $responses[2]->cookie('first'));
  354. $this->assertEquals(null, $responses[2]->cookie('second'));
  355. $this->assertEquals('works', $responses[2]->cookie('third'));
  356. }
  357. /**
  358. * Test that no exception is radied when not timed out.
  359. *
  360. * @return void
  361. */
  362. public function testKeepDeadline()
  363. {
  364. $request = new Request();
  365. $request->url('http://dummy/?sleep');
  366. $options = [
  367. 'timeout' => 5,
  368. ];
  369. $t = microtime(true);
  370. $stream = new Stream();
  371. $stream->send($request, $options);
  372. $this->assertLessThan(5, microtime(true) - $t);
  373. }
  374. /**
  375. * Test that an exception is raised when timed out.
  376. *
  377. * @return void
  378. */
  379. public function testMissDeadline()
  380. {
  381. $this->expectException(\Cake\Network\Exception\HttpException::class);
  382. $this->expectExceptionMessage('Connection timed out http://dummy/?sleep');
  383. $request = new Request();
  384. $request->url('http://dummy/?sleep');
  385. $options = [
  386. 'timeout' => 2,
  387. ];
  388. $stream = new Stream();
  389. $stream->send($request, $options);
  390. }
  391. }