Skip to content

Commit

Permalink
Merge pull request #354 from jiru/fix-svg-content-type
Browse files Browse the repository at this point in the history
Fix Content-Type of SVG when handled by middleware
  • Loading branch information
markstory authored Aug 16, 2020
2 parents f04e1d9 + 8313658 commit a4c86b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Middleware/AssetCompressMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ protected function mapType($build)
$types = [
'css' => 'text/css',
'js' => 'application/javascript',
'svg' => 'image/svg+xml',
];

return $types[$ext] ?? 'application/octet-stream';
Expand Down
30 changes: 25 additions & 5 deletions tests/TestCase/Middleware/AssetCompressMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,37 @@ public function testBuildFile()
$request = $this->request->withUri($uri);

$result = $this->middleware->process($request, $this->handler);
$this->assertEquals('application/javascript', $result->getHeaderLine('Content-Type'));

$body = $result->getBody()->getContents();
$this->assertStringContainsString('var BaseClass = new Class', $body);
$this->assertStringContainsString('var Template = new Class', $body);
}

public function contentTypesProvider()
{
return [
['/cache_js/libs.js', 'application/javascript'],
['/cache_css/all.css', 'text/css'],
['/cache_svg/foo.bar.svg', 'image/svg+xml'],
];
}

/**
* test returned content types
*
* @dataProvider contentTypesProvider
* @return void
*/
public function testBuildFileContentTypes($path, $expected)
{
$uri = $this->request->getUri()->withPath($path);
$request = $this->request->withUri($uri);

$result = $this->middleware->process($request, $this->handler);

$this->assertEquals($expected, $result->getHeaderLine('Content-Type'));
}

/**
* test building plugin assets.
*
Expand All @@ -85,8 +109,6 @@ public function testPluginIniBuildFile()

$result = $this->middleware->process($request, $this->handler);

$this->assertEquals('application/javascript', $result->getHeaderLine('Content-Type'));

$body = $result->getBody()->getContents();
$this->assertStringContainsString('var BaseClass = new Class', $body);
$this->assertStringContainsString('var Template = new Class', $body);
Expand All @@ -105,7 +127,6 @@ public function testBuildFileIsCached()
$result = $this->middleware->process($request, $this->handler);

$body = $result->getBody()->getContents();
$this->assertEquals('application/javascript', $result->getHeaderLine('Content-Type'));
$this->assertStringContainsString('BaseClass', $body);

$this->assertTrue(file_exists(CACHE . 'asset_compress' . DS . 'libs.js'), 'Cache file was created.');
Expand Down Expand Up @@ -142,7 +163,6 @@ public function testBuildThemedAsset()
$result = $this->middleware->process($request, $this->handler);

$body = $result->getBody()->getContents();
$this->assertEquals('text/css', $result->getHeaderLine('Content-Type'));
$this->assertStringContainsString('color: blue', $body);
}

Expand Down

0 comments on commit a4c86b7

Please sign in to comment.