RoutingMiddlewareTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Request;
  20. use Zend\Diactoros\Response;
  21. use Zend\Diactoros\ServerRequestFactory;
  22. /**
  23. * Test for RoutingMiddleware
  24. */
  25. class RoutingMiddlewareTest extends TestCase
  26. {
  27. /**
  28. * Setup method
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. Router::reload();
  36. Router::connect('/articles', ['controller' => 'Articles', 'action' => 'index']);
  37. }
  38. /**
  39. * Test redirect responses from redirect routes
  40. *
  41. * @return void
  42. */
  43. public function testRedirectResponse()
  44. {
  45. Router::redirect('/testpath', '/pages');
  46. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/testpath']);
  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/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. }