ResponseTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, 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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since CakePHP(tm) v 3.0.0
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  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. }
  49. /**
  50. * Test body()
  51. *
  52. * @return void
  53. */
  54. public function testBody() {
  55. $data = [
  56. 'property' => 'value'
  57. ];
  58. $encoded = json_encode($data);
  59. $response = new Response([], $encoded);
  60. $result = $response->body('json_decode');
  61. $this->assertEquals($data['property'], $result->property);
  62. $this->assertEquals($encoded, $response->body());
  63. $this->assertEquals($encoded, $response->body);
  64. $this->assertTrue(isset($response->body));
  65. }
  66. /**
  67. * Test accessor for json
  68. *
  69. * @return void
  70. */
  71. public function testBodyJson() {
  72. $data = [
  73. 'property' => 'value'
  74. ];
  75. $encoded = json_encode($data);
  76. $response = new Response([], $encoded);
  77. $this->assertTrue(isset($response->json));
  78. $this->assertEquals($data['property'], $response->json['property']);
  79. $data = '';
  80. $response = new Response([], $data);
  81. $this->assertFalse(isset($response->json));
  82. }
  83. /**
  84. * Test accessor for xml
  85. *
  86. * @return void
  87. */
  88. public function testBodyXml() {
  89. $data = <<<XML
  90. <?xml version="1.0" encoding="utf-8"?>
  91. <root>
  92. <test>Test</test>
  93. </root>
  94. XML;
  95. $response = new Response([], $data);
  96. $this->assertTrue(isset($response->xml));
  97. $this->assertEquals('Test', (string)$response->xml->test);
  98. $data = '';
  99. $response = new Response([], $data);
  100. $this->assertFalse(isset($response->xml));
  101. }
  102. /**
  103. * Test isOk()
  104. *
  105. * @return void
  106. */
  107. public function testIsOk() {
  108. $headers = [
  109. 'HTTP/1.1 200 OK',
  110. 'Content-Type: text/html'
  111. ];
  112. $response = new Response($headers, 'ok');
  113. $this->assertTrue($response->isOk());
  114. $headers = [
  115. 'HTTP/1.1 201 Created',
  116. 'Content-Type: text/html'
  117. ];
  118. $response = new Response($headers, 'ok');
  119. $this->assertTrue($response->isOk());
  120. $headers = [
  121. 'HTTP/1.1 202 Accepted',
  122. 'Content-Type: text/html'
  123. ];
  124. $response = new Response($headers, 'ok');
  125. $this->assertTrue($response->isOk());
  126. $headers = [
  127. 'HTTP/1.1 301 Moved Permanently',
  128. 'Content-Type: text/html'
  129. ];
  130. $response = new Response($headers, '');
  131. $this->assertFalse($response->isOk());
  132. $headers = [
  133. 'HTTP/1.0 404 Not Found',
  134. 'Content-Type: text/html'
  135. ];
  136. $response = new Response($headers, '');
  137. $this->assertFalse($response->isOk());
  138. }
  139. /**
  140. * Test isRedirect()
  141. *
  142. * @return void
  143. */
  144. public function testIsRedirect() {
  145. $headers = [
  146. 'HTTP/1.1 200 OK',
  147. 'Content-Type: text/html'
  148. ];
  149. $response = new Response($headers, 'ok');
  150. $this->assertFalse($response->isRedirect());
  151. $headers = [
  152. 'HTTP/1.1 301 Moved Permanently',
  153. 'Location: /',
  154. 'Content-Type: text/html'
  155. ];
  156. $response = new Response($headers, '');
  157. $this->assertTrue($response->isRedirect());
  158. $headers = [
  159. 'HTTP/1.0 404 Not Found',
  160. 'Content-Type: text/html'
  161. ];
  162. $response = new Response($headers, '');
  163. $this->assertFalse($response->isRedirect());
  164. }
  165. /**
  166. * Test parsing / getting cookies.
  167. *
  168. * @return void
  169. */
  170. public function testCookie() {
  171. $headers = [
  172. 'HTTP/1.0 200 Ok',
  173. 'Set-Cookie: test=value',
  174. 'Set-Cookie: session=123abc',
  175. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  176. ];
  177. $response = new Response($headers, '');
  178. $this->assertEquals('value', $response->cookie('test'));
  179. $this->assertEquals('123abc', $response->cookie('session'));
  180. $this->assertEquals('soon', $response->cookie('expiring'));
  181. $result = $response->cookie('expiring', true);
  182. $this->assertTrue($result['httponly']);
  183. $this->assertTrue($result['secure']);
  184. $this->assertEquals(
  185. 'Wed, 09-Jun-2021 10:18:14 GMT',
  186. $result['expires']
  187. );
  188. $this->assertEquals('/', $result['path']);
  189. $result = $response->header('set-cookie');
  190. $this->assertCount(3, $result, 'Should be an array.');
  191. $this->assertTrue(isset($response->cookies));
  192. $this->assertEquals(
  193. 'soon',
  194. $response->cookies['expiring']['value']
  195. );
  196. }
  197. /**
  198. * Test statusCode()
  199. *
  200. * @return void
  201. */
  202. public function testStatusCode() {
  203. $headers = [
  204. 'HTTP/1.0 404 Not Found',
  205. 'Content-Type: text/html'
  206. ];
  207. $response = new Response($headers, '');
  208. $this->assertEquals(404, $response->statusCode());
  209. $this->assertEquals(404, $response->code);
  210. $this->assertTrue(isset($response->code));
  211. }
  212. /**
  213. * Test reading the encoding out.
  214. *
  215. * @return void
  216. */
  217. public function testEncoding() {
  218. $headers = [
  219. 'HTTP/1.0 200 Ok',
  220. ];
  221. $response = new Response($headers, '');
  222. $this->assertNull($response->encoding());
  223. $headers = [
  224. 'HTTP/1.0 200 Ok',
  225. 'Content-Type: text/html'
  226. ];
  227. $response = new Response($headers, '');
  228. $this->assertNull($response->encoding());
  229. $headers = [
  230. 'HTTP/1.0 200 Ok',
  231. 'Content-Type: text/html; charset="UTF-8"'
  232. ];
  233. $response = new Response($headers, '');
  234. $this->assertEquals('UTF-8', $response->encoding());
  235. $headers = [
  236. 'HTTP/1.0 200 Ok',
  237. "Content-Type: text/html; charset='ISO-8859-1'"
  238. ];
  239. $response = new Response($headers, '');
  240. $this->assertEquals('ISO-8859-1', $response->encoding());
  241. }
  242. }