ResponseTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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;
  15. use Cake\Network\Http\Response;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * HTTP response test.
  19. */
  20. class ResponseTest extends TestCase {
  21. /**
  22. * Test parsing headers and capturing content
  23. *
  24. * @return void
  25. */
  26. public function testHeaderParsing() {
  27. $headers = [
  28. 'HTTP/1.0 200 OK',
  29. 'Content-Type : text/html;charset="UTF-8"',
  30. 'date: Tue, 25 Dec 2012 04:43:47 GMT',
  31. ];
  32. $response = new Response($headers, 'ok');
  33. $this->assertEquals('1.0', $response->version());
  34. $this->assertEquals(200, $response->statusCode());
  35. $this->assertEquals(
  36. 'text/html;charset="UTF-8"',
  37. $response->header('content-type')
  38. );
  39. $this->assertEquals(
  40. 'Tue, 25 Dec 2012 04:43:47 GMT',
  41. $response->header('Date')
  42. );
  43. $this->assertEquals(
  44. 'text/html;charset="UTF-8"',
  45. $response->headers['Content-Type']
  46. );
  47. $this->assertTrue(isset($response->headers));
  48. $headers = [
  49. 'HTTP/1.0 200',
  50. ];
  51. $response = new Response($headers, 'ok');
  52. $this->assertEquals('1.0', $response->version());
  53. $this->assertEquals(200, $response->statusCode());
  54. }
  55. /**
  56. * Test body()
  57. *
  58. * @return void
  59. */
  60. public function testBody() {
  61. $data = [
  62. 'property' => 'value'
  63. ];
  64. $encoded = json_encode($data);
  65. $response = new Response([], $encoded);
  66. $result = $response->body('json_decode');
  67. $this->assertEquals($data['property'], $result->property);
  68. $this->assertEquals($encoded, $response->body());
  69. $this->assertEquals($encoded, $response->body);
  70. $this->assertTrue(isset($response->body));
  71. }
  72. /**
  73. * Test accessor for json
  74. *
  75. * @return void
  76. */
  77. public function testBodyJson() {
  78. $data = [
  79. 'property' => 'value'
  80. ];
  81. $encoded = json_encode($data);
  82. $response = new Response([], $encoded);
  83. $this->assertTrue(isset($response->json));
  84. $this->assertEquals($data['property'], $response->json['property']);
  85. $data = '';
  86. $response = new Response([], $data);
  87. $this->assertFalse(isset($response->json));
  88. }
  89. /**
  90. * Test accessor for xml
  91. *
  92. * @return void
  93. */
  94. public function testBodyXml() {
  95. $data = <<<XML
  96. <?xml version="1.0" encoding="utf-8"?>
  97. <root>
  98. <test>Test</test>
  99. </root>
  100. XML;
  101. $response = new Response([], $data);
  102. $this->assertTrue(isset($response->xml));
  103. $this->assertEquals('Test', (string)$response->xml->test);
  104. $data = '';
  105. $response = new Response([], $data);
  106. $this->assertFalse(isset($response->xml));
  107. }
  108. /**
  109. * Test isOk()
  110. *
  111. * @return void
  112. */
  113. public function testIsOk() {
  114. $headers = [
  115. 'HTTP/1.1 200 OK',
  116. 'Content-Type: text/html'
  117. ];
  118. $response = new Response($headers, 'ok');
  119. $this->assertTrue($response->isOk());
  120. $headers = [
  121. 'HTTP/1.1 201 Created',
  122. 'Content-Type: text/html'
  123. ];
  124. $response = new Response($headers, 'ok');
  125. $this->assertTrue($response->isOk());
  126. $headers = [
  127. 'HTTP/1.1 202 Accepted',
  128. 'Content-Type: text/html'
  129. ];
  130. $response = new Response($headers, 'ok');
  131. $this->assertTrue($response->isOk());
  132. $headers = [
  133. 'HTTP/1.1 301 Moved Permanently',
  134. 'Content-Type: text/html'
  135. ];
  136. $response = new Response($headers, '');
  137. $this->assertFalse($response->isOk());
  138. $headers = [
  139. 'HTTP/1.0 404 Not Found',
  140. 'Content-Type: text/html'
  141. ];
  142. $response = new Response($headers, '');
  143. $this->assertFalse($response->isOk());
  144. }
  145. /**
  146. * Test isRedirect()
  147. *
  148. * @return void
  149. */
  150. public function testIsRedirect() {
  151. $headers = [
  152. 'HTTP/1.1 200 OK',
  153. 'Content-Type: text/html'
  154. ];
  155. $response = new Response($headers, 'ok');
  156. $this->assertFalse($response->isRedirect());
  157. $headers = [
  158. 'HTTP/1.1 301 Moved Permanently',
  159. 'Location: /',
  160. 'Content-Type: text/html'
  161. ];
  162. $response = new Response($headers, '');
  163. $this->assertTrue($response->isRedirect());
  164. $headers = [
  165. 'HTTP/1.0 404 Not Found',
  166. 'Content-Type: text/html'
  167. ];
  168. $response = new Response($headers, '');
  169. $this->assertFalse($response->isRedirect());
  170. }
  171. /**
  172. * Test parsing / getting cookies.
  173. *
  174. * @return void
  175. */
  176. public function testCookie() {
  177. $headers = [
  178. 'HTTP/1.0 200 Ok',
  179. 'Set-Cookie: test=value',
  180. 'Set-Cookie: session=123abc',
  181. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  182. ];
  183. $response = new Response($headers, '');
  184. $this->assertEquals('value', $response->cookie('test'));
  185. $this->assertEquals('123abc', $response->cookie('session'));
  186. $this->assertEquals('soon', $response->cookie('expiring'));
  187. $result = $response->cookie('expiring', true);
  188. $this->assertTrue($result['httponly']);
  189. $this->assertTrue($result['secure']);
  190. $this->assertEquals(
  191. 'Wed, 09-Jun-2021 10:18:14 GMT',
  192. $result['expires']
  193. );
  194. $this->assertEquals('/', $result['path']);
  195. $result = $response->header('set-cookie');
  196. $this->assertCount(3, $result, 'Should be an array.');
  197. $this->assertTrue(isset($response->cookies));
  198. $this->assertEquals(
  199. 'soon',
  200. $response->cookies['expiring']['value']
  201. );
  202. }
  203. /**
  204. * Test statusCode()
  205. *
  206. * @return void
  207. */
  208. public function testStatusCode() {
  209. $headers = [
  210. 'HTTP/1.0 404 Not Found',
  211. 'Content-Type: text/html'
  212. ];
  213. $response = new Response($headers, '');
  214. $this->assertEquals(404, $response->statusCode());
  215. $this->assertEquals(404, $response->code);
  216. $this->assertTrue(isset($response->code));
  217. }
  218. /**
  219. * Test reading the encoding out.
  220. *
  221. * @return void
  222. */
  223. public function testEncoding() {
  224. $headers = [
  225. 'HTTP/1.0 200 Ok',
  226. ];
  227. $response = new Response($headers, '');
  228. $this->assertNull($response->encoding());
  229. $headers = [
  230. 'HTTP/1.0 200 Ok',
  231. 'Content-Type: text/html'
  232. ];
  233. $response = new Response($headers, '');
  234. $this->assertNull($response->encoding());
  235. $headers = [
  236. 'HTTP/1.0 200 Ok',
  237. 'Content-Type: text/html; charset="UTF-8"'
  238. ];
  239. $response = new Response($headers, '');
  240. $this->assertEquals('UTF-8', $response->encoding());
  241. $headers = [
  242. 'HTTP/1.0 200 Ok',
  243. "Content-Type: text/html; charset='ISO-8859-1'"
  244. ];
  245. $response = new Response($headers, '');
  246. $this->assertEquals('ISO-8859-1', $response->encoding());
  247. }
  248. }