StreamTest.php 8.8 KB

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