StreamTest.php 13 KB

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