StreamTest.php 6.3 KB

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