RoutingMiddlewareTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing\Middleware;
  16. use Cake\Routing\Middleware\RoutingMiddleware;
  17. use Cake\Routing\Router;
  18. use Cake\TestSuite\TestCase;
  19. use Zend\Diactoros\Response;
  20. use Zend\Diactoros\ServerRequestFactory;
  21. /**
  22. * Test for RoutingMiddleware
  23. */
  24. class RoutingMiddlewareTest extends TestCase
  25. {
  26. /**
  27. * Setup method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. Router::reload();
  35. Router::connect('/articles', ['controller' => 'Articles', 'action' => 'index']);
  36. }
  37. /**
  38. * Test redirect responses from redirect routes
  39. *
  40. * @return void
  41. */
  42. public function testRedirectResponse()
  43. {
  44. Router::redirect('/testpath', '/pages');
  45. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/testpath']);
  46. $request = $request->withAttribute('base', '/subdir');
  47. $response = new Response();
  48. $next = function ($req, $res) {
  49. };
  50. $middleware = new RoutingMiddleware();
  51. $response = $middleware($request, $response, $next);
  52. $this->assertEquals(301, $response->getStatusCode());
  53. $this->assertEquals('http://localhost/subdir/pages', $response->getHeaderLine('Location'));
  54. }
  55. /**
  56. * Test redirects with additional headers
  57. *
  58. * @return void
  59. */
  60. public function testRedirectResponseWithHeaders()
  61. {
  62. Router::redirect('/testpath', '/pages');
  63. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/testpath']);
  64. $response = new Response('php://memory', 200, ['X-testing' => 'Yes']);
  65. $next = function ($req, $res) {
  66. };
  67. $middleware = new RoutingMiddleware();
  68. $response = $middleware($request, $response, $next);
  69. $this->assertEquals(301, $response->getStatusCode());
  70. $this->assertEquals('http://localhost/pages', $response->getHeaderLine('Location'));
  71. $this->assertEquals('Yes', $response->getHeaderLine('X-testing'));
  72. }
  73. /**
  74. * Test that Router sets parameters
  75. *
  76. * @return void
  77. */
  78. public function testRouterSetParams()
  79. {
  80. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
  81. $response = new Response();
  82. $next = function ($req, $res) {
  83. $expected = [
  84. 'controller' => 'Articles',
  85. 'action' => 'index',
  86. 'plugin' => null,
  87. 'pass' => [],
  88. '_matchedRoute' => '/articles'
  89. ];
  90. $this->assertEquals($expected, $req->getAttribute('params'));
  91. };
  92. $middleware = new RoutingMiddleware();
  93. $middleware($request, $response, $next);
  94. }
  95. /**
  96. * Test that routing is not applied if a controller exists already
  97. *
  98. * @return void
  99. */
  100. public function testRouterNoopOnController()
  101. {
  102. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
  103. $request = $request->withAttribute('params', ['controller' => 'Articles']);
  104. $response = new Response();
  105. $next = function ($req, $res) {
  106. $this->assertEquals(['controller' => 'Articles'], $req->getAttribute('params'));
  107. };
  108. $middleware = new RoutingMiddleware();
  109. $middleware($request, $response, $next);
  110. }
  111. /**
  112. * Test missing routes not being caught.
  113. *
  114. * @expectedException \Cake\Routing\Exception\MissingRouteException
  115. */
  116. public function testMissingRouteNotCaught()
  117. {
  118. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/missing']);
  119. $response = new Response();
  120. $next = function ($req, $res) {
  121. };
  122. $middleware = new RoutingMiddleware();
  123. $middleware($request, $response, $next);
  124. }
  125. /**
  126. * Test route with _method being parsed correctly.
  127. *
  128. * @return void
  129. */
  130. public function testFakedRequestMethodParsed()
  131. {
  132. Router::connect('/articles-patch', [
  133. 'controller' => 'Articles',
  134. 'action' => 'index',
  135. '_method' => 'PATCH'
  136. ]);
  137. $request = ServerRequestFactory::fromGlobals(
  138. [
  139. 'REQUEST_METHOD' => 'POST',
  140. 'REQUEST_URI' => '/articles-patch'
  141. ],
  142. null,
  143. ['_method' => 'PATCH']
  144. );
  145. $response = new Response();
  146. $next = function ($req, $res) {
  147. $expected = [
  148. 'controller' => 'Articles',
  149. 'action' => 'index',
  150. '_method' => 'PATCH',
  151. 'plugin' => null,
  152. 'pass' => [],
  153. '_matchedRoute' => '/articles-patch'
  154. ];
  155. $this->assertEquals($expected, $req->getAttribute('params'));
  156. $this->assertEquals('PATCH', $req->getMethod());
  157. };
  158. $middleware = new RoutingMiddleware();
  159. $middleware($request, $response, $next);
  160. }
  161. }