Skip to content

Commit a4c86b7

Browse files
authored
Merge pull request #354 from jiru/fix-svg-content-type
Fix Content-Type of SVG when handled by middleware
2 parents f04e1d9 + 8313658 commit a4c86b7

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Middleware/AssetCompressMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ protected function mapType($build)
115115
$types = [
116116
'css' => 'text/css',
117117
'js' => 'application/javascript',
118+
'svg' => 'image/svg+xml',
118119
];
119120

120121
return $types[$ext] ?? 'application/octet-stream';

tests/TestCase/Middleware/AssetCompressMiddlewareTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,37 @@ public function testBuildFile()
6464
$request = $this->request->withUri($uri);
6565

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

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

73+
public function contentTypesProvider()
74+
{
75+
return [
76+
['/cache_js/libs.js', 'application/javascript'],
77+
['/cache_css/all.css', 'text/css'],
78+
['/cache_svg/foo.bar.svg', 'image/svg+xml'],
79+
];
80+
}
81+
82+
/**
83+
* test returned content types
84+
*
85+
* @dataProvider contentTypesProvider
86+
* @return void
87+
*/
88+
public function testBuildFileContentTypes($path, $expected)
89+
{
90+
$uri = $this->request->getUri()->withPath($path);
91+
$request = $this->request->withUri($uri);
92+
93+
$result = $this->middleware->process($request, $this->handler);
94+
95+
$this->assertEquals($expected, $result->getHeaderLine('Content-Type'));
96+
}
97+
7498
/**
7599
* test building plugin assets.
76100
*
@@ -85,8 +109,6 @@ public function testPluginIniBuildFile()
85109

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

88-
$this->assertEquals('application/javascript', $result->getHeaderLine('Content-Type'));
89-
90112
$body = $result->getBody()->getContents();
91113
$this->assertStringContainsString('var BaseClass = new Class', $body);
92114
$this->assertStringContainsString('var Template = new Class', $body);
@@ -105,7 +127,6 @@ public function testBuildFileIsCached()
105127
$result = $this->middleware->process($request, $this->handler);
106128

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

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

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

0 commit comments

Comments
 (0)