StreamTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. public function setUp() {
  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. $stream = new Stream();
  38. $request = new Request();
  39. $request->url('http://localhost')
  40. ->header('User-Agent', 'CakePHP TestSuite')
  41. ->cookie('testing', 'value');
  42. try {
  43. $responses = $stream->send($request, []);
  44. } catch (SocketException $e) {
  45. $this->markTestSkipped('Could not connect to localhost, skipping');
  46. }
  47. $this->assertInstanceOf('Cake\Network\Http\Response', $responses[0]);
  48. }
  49. /**
  50. * Test building the context headers
  51. *
  52. * @return void
  53. */
  54. public function testBuildingContextHeader() {
  55. $request = new Request();
  56. $request->url('http://localhost')
  57. ->header([
  58. 'User-Agent' => 'CakePHP TestSuite',
  59. 'Content-Type' => 'application/json'
  60. ])
  61. ->cookie([
  62. 'testing' => 'value',
  63. 'utm_src' => 'awesome',
  64. ]);
  65. $options = [
  66. 'redirect' => 20
  67. ];
  68. $this->stream->send($request, $options);
  69. $result = $this->stream->contextOptions();
  70. $expected = [
  71. 'Connection: close',
  72. 'User-Agent: CakePHP TestSuite',
  73. 'Content-Type: application/json',
  74. 'Cookie: testing=value; utm_src=awesome',
  75. ];
  76. $this->assertEquals(implode("\r\n", $expected), $result['header']);
  77. $this->assertEquals($options['redirect'], $result['max_redirects']);
  78. $this->assertTrue($result['ignore_errors']);
  79. }
  80. /**
  81. * Test send() + context options with string content.
  82. *
  83. * @return void
  84. */
  85. public function testSendContextContentString() {
  86. $content = json_encode(['a' => 'b']);
  87. $request = new Request();
  88. $request->url('http://localhost')
  89. ->header([
  90. 'Content-Type' => 'application/json'
  91. ])
  92. ->body($content);
  93. $options = [
  94. 'redirect' => 20
  95. ];
  96. $this->stream->send($request, $options);
  97. $result = $this->stream->contextOptions();
  98. $expected = [
  99. 'Connection: close',
  100. 'User-Agent: CakePHP',
  101. 'Content-Type: application/json',
  102. ];
  103. $this->assertEquals(implode("\r\n", $expected), $result['header']);
  104. $this->assertEquals($content, $result['content']);
  105. }
  106. /**
  107. * Test send() + context options with array content.
  108. *
  109. * @return void
  110. */
  111. public function testSendContextContentArray() {
  112. $request = new Request();
  113. $request->url('http://localhost')
  114. ->header([
  115. 'Content-Type' => 'application/json'
  116. ])
  117. ->body(['a' => 'my value']);
  118. $this->stream->send($request, []);
  119. $result = $this->stream->contextOptions();
  120. $expected = [
  121. 'Connection: close',
  122. 'User-Agent: CakePHP',
  123. 'Content-Type: multipart/form-data; boundary="',
  124. ];
  125. $this->assertStringStartsWith(implode("\r\n", $expected), $result['header']);
  126. $this->assertContains('Content-Disposition: form-data; name="a"', $result['content']);
  127. $this->assertContains('my value', $result['content']);
  128. }
  129. /**
  130. * Test send() + context options for SSL.
  131. *
  132. * @return void
  133. */
  134. public function testSendContextSsl() {
  135. $request = new Request();
  136. $request->url('https://localhost.com/test.html');
  137. $options = [
  138. 'ssl_verify_host' => true,
  139. 'ssl_verify_peer' => true,
  140. 'ssl_verify_depth' => 9000,
  141. 'ssl_allow_self_signed' => false,
  142. ];
  143. $this->stream->send($request, $options);
  144. $result = $this->stream->contextOptions();
  145. $expected = [
  146. 'CN_match' => 'localhost.com',
  147. 'verify_peer' => true,
  148. 'verify_depth' => 9000,
  149. 'allow_self_signed' => false,
  150. ];
  151. foreach ($expected as $k => $v) {
  152. $this->assertEquals($v, $result[$k]);
  153. }
  154. }
  155. /**
  156. * The PHP stream API returns ALL the headers for ALL the requests when
  157. * there are redirects.
  158. *
  159. * @return void
  160. */
  161. public function testCreateResponseWithRedirects() {
  162. $headers = [
  163. 'HTTP/1.1 302 Found',
  164. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  165. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  166. 'X-Powered-By: PHP/5.4.9',
  167. 'Location: http://localhost/cake3/tasks/second',
  168. 'Content-Length: 0',
  169. 'Connection: close',
  170. 'Content-Type: text/html; charset=UTF-8',
  171. 'Set-Cookie: first=value',
  172. 'HTTP/1.1 302 Found',
  173. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  174. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  175. 'X-Powered-By: PHP/5.4.9',
  176. 'Location: http://localhost/cake3/tasks/third',
  177. 'Content-Length: 0',
  178. 'Connection: close',
  179. 'Content-Type: text/html; charset=UTF-8',
  180. 'Set-Cookie: second=val',
  181. 'HTTP/1.1 200 OK',
  182. 'Date: Mon, 31 Dec 2012 16:53:16 GMT',
  183. 'Server: Apache/2.2.22 (Unix) DAV/2 PHP/5.4.9 mod_ssl/2.2.22 OpenSSL/0.9.8r',
  184. 'X-Powered-By: PHP/5.4.9',
  185. 'Content-Length: 22',
  186. 'Connection: close',
  187. 'Content-Type: text/html; charset=UTF-8',
  188. 'Set-Cookie: third=works',
  189. ];
  190. $content = 'This is the third page';
  191. $responses = $this->stream->createResponses($headers, $content);
  192. $this->assertCount(3, $responses);
  193. $this->assertEquals('close', $responses[0]->header('Connection'));
  194. $this->assertEquals('', $responses[0]->body());
  195. $this->assertEquals('', $responses[1]->body());
  196. $this->assertEquals($content, $responses[2]->body());
  197. $this->assertEquals(302, $responses[0]->statusCode());
  198. $this->assertEquals(302, $responses[1]->statusCode());
  199. $this->assertEquals(200, $responses[2]->statusCode());
  200. $this->assertEquals('value', $responses[0]->cookie('first'));
  201. $this->assertEquals(null, $responses[0]->cookie('second'));
  202. $this->assertEquals(null, $responses[0]->cookie('third'));
  203. $this->assertEquals(null, $responses[1]->cookie('first'));
  204. $this->assertEquals('val', $responses[1]->cookie('second'));
  205. $this->assertEquals(null, $responses[1]->cookie('third'));
  206. $this->assertEquals(null, $responses[2]->cookie('first'));
  207. $this->assertEquals(null, $responses[2]->cookie('second'));
  208. $this->assertEquals('works', $responses[2]->cookie('third'));
  209. }
  210. }