RequestActionController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <https://book.cakephp.org/view/1196/Testing>
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace TestApp\Controller;
  15. use Cake\Http\Exception\NotFoundException;
  16. /**
  17. * RequestActionController class
  18. */
  19. class RequestActionController extends AppController
  20. {
  21. /**
  22. * The default model to use.
  23. *
  24. * @var string
  25. */
  26. public $modelClass = 'Posts';
  27. /**
  28. * test_request_action method
  29. *
  30. * @return \Cake\Http\Response
  31. */
  32. public function test_request_action()
  33. {
  34. return $this->response->withStringBody('This is a test');
  35. }
  36. /**
  37. * another_ra_test method
  38. *
  39. * @param mixed $id
  40. * @param mixed $other
  41. * @return \Cake\Http\Response
  42. */
  43. public function another_ra_test($id, $other)
  44. {
  45. return $this->response->withStringBody($id + $other);
  46. }
  47. /**
  48. * normal_request_action method
  49. *
  50. * @return \Cake\Http\Response
  51. */
  52. public function normal_request_action()
  53. {
  54. return $this->response->withStringBody('Hello World');
  55. }
  56. /**
  57. * returns $this->here as body
  58. *
  59. * @return \Cake\Http\Response
  60. */
  61. public function return_here()
  62. {
  63. return $this->response->withStringBody($this->here);
  64. }
  65. /**
  66. * paginate_request_action method
  67. *
  68. * @return void
  69. */
  70. public function paginate_request_action()
  71. {
  72. $data = $this->paginate();
  73. $this->autoRender = false;
  74. }
  75. /**
  76. * post pass, testing post passing
  77. *
  78. * @return \Cake\Http\Response
  79. */
  80. public function post_pass()
  81. {
  82. return $this->response->withStringBody(json_encode($this->request->getData()));
  83. }
  84. /**
  85. * query pass, testing query passing
  86. *
  87. * @return \Cake\Http\Response
  88. */
  89. public function query_pass()
  90. {
  91. return $this->response->withStringBody(json_encode($this->request->getQueryParams()));
  92. }
  93. /**
  94. * cookie pass, testing cookie passing
  95. *
  96. * @return \Cake\Http\Response
  97. */
  98. public function cookie_pass()
  99. {
  100. return $this->response->withStringBody(json_encode($this->request->getCookieParams()));
  101. }
  102. /**
  103. * test param passing and parsing.
  104. *
  105. * @return \Cake\Http\Response
  106. */
  107. public function params_pass()
  108. {
  109. return $this->response->withStringBody(json_encode([
  110. 'params' => $this->request->getAttribute('params'),
  111. 'base' => $this->request->getAttribute('base'),
  112. 'here' => $this->request->getRequestTarget(),
  113. 'webroot' => $this->request->getAttribute('webroot'),
  114. 'query' => $this->request->getQueryParams(),
  115. 'url' => $this->request->getUri()->getPath(),
  116. 'contentType' => $this->request->contentType(),
  117. ]));
  118. }
  119. /**
  120. * param check method.
  121. *
  122. * @return \Cake\Http\Response
  123. */
  124. public function param_check()
  125. {
  126. $this->autoRender = false;
  127. $content = '';
  128. if ($this->request->getParam('0')) {
  129. $content = 'return found';
  130. }
  131. return $this->response->withStringBody($content);
  132. }
  133. /**
  134. * Tests session transmission
  135. *
  136. * @return \Cake\Http\Response
  137. */
  138. public function session_test()
  139. {
  140. return $this->response->withStringBody($this->request->getSession()->read('foo'));
  141. }
  142. /**
  143. * Tests input data transmission
  144. *
  145. * @return \Cake\Http\Response
  146. */
  147. public function input_test()
  148. {
  149. $text = $this->request->input('json_decode')->hello;
  150. return $this->response->withStringBody($text);
  151. }
  152. /**
  153. * Tests exception handling
  154. *
  155. * @throws \Cake\Http\Exception\NotFoundException
  156. * @return void
  157. */
  158. public function error_method()
  159. {
  160. throw new NotFoundException('Not there or here.');
  161. }
  162. }