AssetMiddlewareTest.php 7.8 KB

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