RoutingMiddlewareTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $request = $request->withAttribute('base', '/subdir');
  48. $response = new Response();
  49. $next = function ($req, $res) {
  50. };
  51. $middleware = new RoutingMiddleware();
  52. $response = $middleware($request, $response, $next);
  53. $this->assertEquals(301, $response->getStatusCode());
  54. $this->assertEquals('http://localhost/subdir/pages', $response->getHeaderLine('Location'));
  55. }
  56. /**
  57. * Test redirects with additional headers
  58. *
  59. * @return void
  60. */
  61. public function testRedirectResponseWithHeaders()
  62. {
  63. Router::redirect('/testpath', '/pages');
  64. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/testpath']);
  65. $response = new Response('php://memory', 200, ['X-testing' => 'Yes']);
  66. $next = function ($req, $res) {
  67. };
  68. $middleware = new RoutingMiddleware();
  69. $response = $middleware($request, $response, $next);
  70. $this->assertEquals(301, $response->getStatusCode());
  71. $this->assertEquals('http://localhost/pages', $response->getHeaderLine('Location'));
  72. $this->assertEquals('Yes', $response->getHeaderLine('X-testing'));
  73. }
  74. /**
  75. * Test that Router sets parameters
  76. *
  77. * @return void
  78. */
  79. public function testRouterSetParams()
  80. {
  81. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
  82. $response = new Response();
  83. $next = function ($req, $res) {
  84. $expected = [
  85. 'controller' => 'Articles',
  86. 'action' => 'index',
  87. 'plugin' => null,
  88. 'pass' => [],
  89. '_matchedRoute' => '/articles'
  90. ];
  91. $this->assertEquals($expected, $req->getAttribute('params'));
  92. };
  93. $middleware = new RoutingMiddleware();
  94. $middleware($request, $response, $next);
  95. }
  96. /**
  97. * Test that routing is not applied if a controller exists already
  98. *
  99. * @return void
  100. */
  101. public function testRouterNoopOnController()
  102. {
  103. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
  104. $request = $request->withAttribute('params', ['controller' => 'Articles']);
  105. $response = new Response();
  106. $next = function ($req, $res) {
  107. $this->assertEquals(['controller' => 'Articles'], $req->getAttribute('params'));
  108. };
  109. $middleware = new RoutingMiddleware();
  110. $middleware($request, $response, $next);
  111. }
  112. /**
  113. * Test missing routes not being caught.
  114. *
  115. * @expectedException \Cake\Routing\Exception\MissingRouteException
  116. */
  117. public function testMissingRouteNotCaught()
  118. {
  119. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/missing']);
  120. $response = new Response();
  121. $next = function ($req, $res) {
  122. };
  123. $middleware = new RoutingMiddleware();
  124. $middleware($request, $response, $next);
  125. }
  126. }