StreamTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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\Network\Http\Adapter\Stream;
  16. use Cake\Network\Http\Request;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * HTTP stream adapter test.
  20. */
  21. class StreamTest extends TestCase
  22. {
  23. public function setUp()
  24. {
  25. parent::setUp();
  26. $this->stream = $this->getMock(
  27. 'Cake\Network\Http\Adapter\Stream',
  28. ['_send']
  29. );
  30. }
  31. /**
  32. * Test the send method
  33. *
  34. * @return void
  35. */
  36. public function testSend()
  37. {
  38. $stream = new Stream();
  39. $request = new Request();
  40. $request->url('http://localhost')
  41. ->header('User-Agent', 'CakePHP TestSuite')
  42. ->cookie('testing', 'value');
  43. try {
  44. $responses = $stream->send($request, []);
  45. } catch (\Cake\Core\Exception\Exception $e) {
  46. $this->markTestSkipped('Could not connect to localhost, skipping');
  47. }
  48. $this->assertInstanceOf('Cake\Network\Http\Response', $responses[0]);
  49. }
  50. /**
  51. * Test building the context headers
  52. *
  53. * @return void
  54. */
  55. public function testBuildingContextHeader()
  56. {
  57. $request = new Request();
  58. $request->url('http://localhost')
  59. ->header([
  60. 'User-Agent' => 'CakePHP TestSuite',
  61. 'Content-Type' => 'application/json'
  62. ])
  63. ->cookie([
  64. 'testing' => 'value',
  65. 'utm_src' => 'awesome',
  66. ]);
  67. $options = [
  68. 'redirect' => 20,
  69. ];
  70. $this->stream->send($request, $options);
  71. $result = $this->stream->contextOptions();
  72. $expected = [
  73. 'Connection: close',
  74. 'User-Agent: CakePHP TestSuite',
  75. 'Content-Type: application/json',
  76. 'Cookie: testing=value; utm_src=awesome',
  77. ];
  78. $this->assertEquals(implode("\r\n", $expected), $result['header']);
  79. $this->assertEquals($options['redirect'], $result['max_redirects']);
  80. $this->assertTrue($result['ignore_errors']);
  81. }
  82. /**
  83. * Test send() + context options with string content.
  84. *
  85. * @return void
  86. */
  87. public function testSendContextContentString()
  88. {
  89. $content = json_encode(['a' => 'b']);
  90. $request = new Request();
  91. $request->url('http://localhost')
  92. ->header([
  93. 'Content-Type' => 'application/json'
  94. ])
  95. ->body($content);
  96. $options = [
  97. 'redirect' => 20
  98. ];
  99. $this->stream->send($request, $options);
  100. $result = $this->stream->contextOptions();
  101. $expected = [
  102. 'Connection: close',
  103. 'User-Agent: CakePHP',
  104. 'Content-Type: application/json',
  105. ];
  106. $this->assertEquals(implode("\r\n", $expected), $result['header']);
  107. $this->assertEquals($content, $result['content']);
  108. }
  109. /**
  110. * Test send() + context options with array content.
  111. *
  112. * @return void
  113. */
  114. public function testSendContextContentArray()
  115. {
  116. $request = new Request();
  117. $request->url('http://localhost')
  118. ->header([
  119. 'Content-Type' => 'application/json'
  120. ])
  121. ->body(['a' => 'my value']);
  122. $this->stream->send($request, []);
  123. $result = $this->stream->contextOptions();
  124. $expected = [
  125. 'Connection: close',
  126. 'User-Agent: CakePHP',
  127. 'Content-Type: application/x-www-form-urlencoded',
  128. ];
  129. $this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
  130. $this->assertContains('a=my+value', $result['content']);
  131. $this->assertContains('my+value', $result['content']);
  132. }
  133. /**
  134. * Test send() + context options with array content.
  135. *
  136. * @return void
  137. */
  138. public function testSendContextContentArrayFiles()
  139. {
  140. $request = new Request();
  141. $request->url('http://localhost')
  142. ->header([
  143. 'Content-Type' => 'application/json'
  144. ])
  145. ->body(['upload' => fopen(CORE_PATH . 'VERSION.txt', 'r')]);
  146. $this->stream->send($request, []);
  147. $result = $this->stream->contextOptions();
  148. $expected = [
  149. 'Connection: close',
  150. 'User-Agent: CakePHP',
  151. 'Content-Type: multipart/form-data',
  152. ];
  153. $this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
  154. $this->assertContains('name="upload"', $result['content']);
  155. $this->assertContains('filename="VERSION.txt"', $result['content']);
  156. }
  157. /**
  158. * Test send() + context options for SSL.
  159. *
  160. * @return void
  161. */
  162. public function testSendContextSsl()
  163. {
  164. $request = new Request();
  165. $request->url('https://localhost.com/test.html');
  166. $options = [
  167. 'ssl_verify_host' => true,
  168. 'ssl_verify_peer' => true,
  169. 'ssl_verify_peer_name' => true,
  170. 'ssl_verify_depth' => 9000,
  171. 'ssl_allow_self_signed' => false,
  172. 'proxy' => [
  173. 'proxy' => '127.0.0.1:8080'
  174. ]
  175. ];
  176. $this->stream->send($request, $options);
  177. $result = $this->stream->contextOptions();
  178. $expected = [
  179. 'peer_name' => 'localhost.com',
  180. 'verify_peer' => true,
  181. 'verify_peer_name' => true,
  182. 'verify_depth' => 9000,
  183. 'allow_self_signed' => false,
  184. 'proxy' => '127.0.0.1:8080',
  185. ];
  186. foreach ($expected as $k => $v) {
  187. $this->assertEquals($v, $result[$k]);
  188. }
  189. $this->assertTrue(is_readable($result['cafile']));
  190. }
  191. /**
  192. * Test send() + context options for SSL.
  193. *
  194. * @return void
  195. */
  196. public function testSendContextSslNoVerifyPeerName()
  197. {
  198. $request = new Request();
  199. $request->url('https://localhost.com/test.html');
  200. $options = [
  201. 'ssl_verify_host' => true,
  202. 'ssl_verify_peer' => true,
  203. 'ssl_verify_peer_name' => false,
  204. 'ssl_verify_depth' => 9000,
  205. 'ssl_allow_self_signed' => false,
  206. 'proxy' => [
  207. 'proxy' => '127.0.0.1:8080'
  208. ]
  209. ];
  210. $this->stream->send($request, $options);
  211. $result = $this->stream->contextOptions();
  212. $expected = [
  213. 'peer_name' => 'localhost.com',
  214. 'verify_peer' => true,
  215. 'verify_peer_name' => false,
  216. 'verify_depth' => 9000,
  217. 'allow_self_signed' => false,
  218. 'proxy' => '127.0.0.1:8080',
  219. ];
  220. foreach ($expected as $k => $v) {
  221. $this->assertEquals($v, $result[$k]);
  222. }
  223. $this->assertTrue(is_readable($result['cafile']));
  224. }
  225. /**
  226. * The PHP stream API returns ALL the headers for ALL the requests when
  227. * there are redirects.
  228. *
  229. * @return void
  230. */
  231. public function testCreateResponseWithRedirects()
  232. {
  233. $headers = [
  234. 'HTTP/1.1 302 Found',
  235. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  236. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  237. 'X-Powered-By: PHP/5.4.9',
  238. 'Location: http://localhost/cake3/tasks/second',
  239. 'Content-Length: 0',
  240. 'Connection: close',
  241. 'Content-Type: text/html; charset=UTF-8',
  242. 'Set-Cookie: first=value',
  243. 'HTTP/1.1 302 Found',
  244. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  245. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  246. 'X-Powered-By: PHP/5.4.9',
  247. 'Location: http://localhost/cake3/tasks/third',
  248. 'Content-Length: 0',
  249. 'Connection: close',
  250. 'Content-Type: text/html; charset=UTF-8',
  251. 'Set-Cookie: second=val',
  252. 'HTTP/1.1 200 OK',
  253. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  254. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  255. 'X-Powered-By: PHP/5.4.9',
  256. 'Content-Length: 22',
  257. 'Connection: close',
  258. 'Content-Type: text/html; charset=UTF-8',
  259. 'Set-Cookie: third=works',
  260. ];
  261. $content = 'This is the third page';
  262. $responses = $this->stream->createResponses($headers, $content);
  263. $this->assertCount(3, $responses);
  264. $this->assertEquals('close', $responses[0]->header('Connection'));
  265. $this->assertEquals('', $responses[0]->body());
  266. $this->assertEquals('', $responses[1]->body());
  267. $this->assertEquals($content, $responses[2]->body());
  268. $this->assertEquals(302, $responses[0]->statusCode());
  269. $this->assertEquals(302, $responses[1]->statusCode());
  270. $this->assertEquals(200, $responses[2]->statusCode());
  271. $this->assertEquals('value', $responses[0]->cookie('first'));
  272. $this->assertEquals(null, $responses[0]->cookie('second'));
  273. $this->assertEquals(null, $responses[0]->cookie('third'));
  274. $this->assertEquals(null, $responses[1]->cookie('first'));
  275. $this->assertEquals('val', $responses[1]->cookie('second'));
  276. $this->assertEquals(null, $responses[1]->cookie('third'));
  277. $this->assertEquals(null, $responses[2]->cookie('first'));
  278. $this->assertEquals(null, $responses[2]->cookie('second'));
  279. $this->assertEquals('works', $responses[2]->cookie('third'));
  280. }
  281. }