StreamTest.php 12 KB

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