AssetMiddlewareTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\Core\Plugin;
  17. use Cake\Http\ServerRequestFactory;
  18. use Cake\Routing\Middleware\AssetMiddleware;
  19. use Cake\TestSuite\TestCase;
  20. use Zend\Diactoros\Request;
  21. use Zend\Diactoros\Response;
  22. /**
  23. * Test for AssetMiddleware
  24. */
  25. class AssetMiddlewareTest extends TestCase
  26. {
  27. /**
  28. * setup
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. Plugin::load('TestPlugin');
  36. Plugin::load('Company/TestPluginThree');
  37. }
  38. /**
  39. * test that the if modified since header generates 304 responses
  40. *
  41. * @return void
  42. */
  43. public function testCheckIfModifiedHeader()
  44. {
  45. $modified = filemtime(TEST_APP . 'Plugin/TestPlugin/webroot/root.js');
  46. $request = ServerRequestFactory::fromGlobals([
  47. 'REQUEST_URI' => '/test_plugin/root.js',
  48. 'HTTP_IF_MODIFIED_SINCE' => date('D, j M Y G:i:s \G\M\T', $modified)
  49. ]);
  50. $response = new Response();
  51. $next = function ($req, $res) {
  52. return $res;
  53. };
  54. $middleware = new AssetMiddleware();
  55. $res = $middleware($request, $response, $next);
  56. $body = $res->getBody()->getContents();
  57. $this->assertEquals('', $body);
  58. $this->assertEquals(304, $res->getStatusCode());
  59. $this->assertNotEmpty($res->getHeaderLine('Last-Modified'));
  60. }
  61. /**
  62. * test missing plugin assets.
  63. *
  64. * @return void
  65. */
  66. public function testMissingPluginAsset()
  67. {
  68. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/test_plugin/not_found.js']);
  69. $response = new Response();
  70. $next = function ($req, $res) {
  71. return $res;
  72. };
  73. $middleware = new AssetMiddleware();
  74. $res = $middleware($request, $response, $next);
  75. $body = $res->getBody()->getContents();
  76. $this->assertEquals('', $body);
  77. }
  78. /**
  79. * Data provider for assets.
  80. *
  81. * @return array
  82. */
  83. public function assetProvider()
  84. {
  85. return [
  86. // In plugin root.
  87. [
  88. '/test_plugin/root.js',
  89. TEST_APP . 'Plugin/TestPlugin/webroot/root.js'
  90. ],
  91. // Subdirectory
  92. [
  93. '/test_plugin/js/alert.js',
  94. TEST_APP . 'Plugin/TestPlugin/webroot/js/alert.js'
  95. ],
  96. // In path that matches the plugin name
  97. [
  98. '/test_plugin/js/test_plugin/test.js',
  99. TEST_APP . 'Plugin/TestPlugin/webroot/js/test_plugin/test.js'
  100. ],
  101. // In vendored plugin
  102. [
  103. '/company/test_plugin_three/css/company.css',
  104. TEST_APP . 'Plugin/Company/TestPluginThree/webroot/css/company.css'
  105. ],
  106. ];
  107. }
  108. /**
  109. * Test assets in a plugin.
  110. *
  111. * @dataProvider assetProvider
  112. */
  113. public function testPluginAsset($url, $expectedFile)
  114. {
  115. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => $url]);
  116. $response = new Response();
  117. $next = function ($req, $res) {
  118. return $res;
  119. };
  120. $middleware = new AssetMiddleware();
  121. $res = $middleware($request, $response, $next);
  122. $body = $res->getBody()->getContents();
  123. $this->assertEquals(file_get_contents($expectedFile), $body);
  124. }
  125. /**
  126. * Test headers with plugin assets
  127. *
  128. * @return void
  129. */
  130. public function testPluginAssetHeaders()
  131. {
  132. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/test_plugin/root.js']);
  133. $response = new Response();
  134. $next = function ($req, $res) {
  135. return $res;
  136. };
  137. $modified = filemtime(TEST_APP . 'Plugin/TestPlugin/webroot/root.js');
  138. $expires = strtotime('+4 hours');
  139. $time = time();
  140. $middleware = new AssetMiddleware(['cacheTime' => '+4 hours']);
  141. $res = $middleware($request, $response, $next);
  142. $this->assertEquals(
  143. 'application/javascript',
  144. $res->getHeaderLine('Content-Type')
  145. );
  146. $this->assertEquals(
  147. gmdate('D, j M Y G:i:s ', $time) . 'GMT',
  148. $res->getHeaderLine('Date')
  149. );
  150. $this->assertEquals(
  151. 'public,max-age=' . ($expires - $time),
  152. $res->getHeaderLine('Cache-Control')
  153. );
  154. $this->assertEquals(
  155. gmdate('D, j M Y G:i:s ', $modified) . 'GMT',
  156. $res->getHeaderLine('Last-Modified')
  157. );
  158. $this->assertEquals(
  159. gmdate('D, j M Y G:i:s ', $expires) . 'GMT',
  160. $res->getHeaderLine('Expires')
  161. );
  162. }
  163. /**
  164. * Test that content-types can be injected
  165. *
  166. * @return void
  167. */
  168. public function testCustomFileTypes()
  169. {
  170. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/test_plugin/root.js']);
  171. $response = new Response();
  172. $next = function ($req, $res) {
  173. return $res;
  174. };
  175. $middleware = new AssetMiddleware(['types' => ['js' => 'custom/stuff']]);
  176. $res = $middleware($request, $response, $next);
  177. $this->assertEquals(
  178. 'custom/stuff',
  179. $res->getHeaderLine('Content-Type')
  180. );
  181. }
  182. /**
  183. * Test that // results in a 404
  184. *
  185. * @return void
  186. */
  187. public function test404OnDoubleSlash()
  188. {
  189. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '//index.php']);
  190. $response = new Response();
  191. $next = function ($req, $res) {
  192. return $res;
  193. };
  194. $middleware = new AssetMiddleware();
  195. $res = $middleware($request, $response, $next);
  196. $this->assertEmpty($res->getBody()->getContents());
  197. }
  198. /**
  199. * Test that .. results in a 404
  200. *
  201. * @return void
  202. */
  203. public function test404OnDoubleDot()
  204. {
  205. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/test_plugin/../webroot/root.js']);
  206. $response = new Response();
  207. $next = function ($req, $res) {
  208. return $res;
  209. };
  210. $middleware = new AssetMiddleware();
  211. $res = $middleware($request, $response, $next);
  212. $this->assertEmpty($res->getBody()->getContents());
  213. }
  214. }