Unverified Commit a60a47ae authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Serve correct mime type on release dev server (#43907)

parent 353721a1
......@@ -354,28 +354,23 @@ class ReleaseAssetServer extends AssetServer {
final Uri artifactUri = fs.directory(getWebBuildDirectory()).uri.resolveUri(request.url);
final File file = fs.file(artifactUri);
if (file.existsSync()) {
return Response.ok(file.readAsBytesSync(), headers: <String, String>{
'Content-Type': _guessExtension(file),
final Uint8List bytes = file.readAsBytesSync();
// Fallback to "application/octet-stream" on null which
// makes no claims as to the structure of the data.
final String mimeType = mime.lookupMimeType(file.path, headerBytes: bytes)
?? 'application/octet-stream';
return Response.ok(bytes, headers: <String, String>{
'Content-Type': mimeType,
});
}
if (request.url.path == '') {
final File file = fs.file(fs.path.join(getWebBuildDirectory(), 'index.html'));
return Response.ok(file.readAsBytesSync(), headers: <String, String>{
'Content-Type': _guessExtension(file),
'Content-Type': 'text/html',
});
}
return Response.notFound('');
}
String _guessExtension(File file) {
switch (fs.path.extension(file.path)) {
case '.js':
return 'text/javascript';
case '.html':
return 'text/html';
}
return 'text';
}
}
class DebugAssetServer extends AssetServer {
......
......@@ -78,4 +78,46 @@ void main() {
expect(response.statusCode, 404);
}));
test('release asset server serves correct mime type and content length for png', () => testbed.run(() async {
assetServer = ReleaseAssetServer();
fs.file(fs.path.join('build', 'web', 'assets', 'foo.png'))
..createSync(recursive: true)
..writeAsBytesSync(kTransparentImage);
final Response response = await assetServer
.handle(Request('GET', Uri.parse('http://localhost:8080/assets/foo.png')));
expect(response.headers, <String, String>{
'Content-Type': 'image/png',
'content-length': '64',
});
}));
test('release asset server serves correct mime type and content length for JavaScript', () => testbed.run(() async {
assetServer = ReleaseAssetServer();
fs.file(fs.path.join('build', 'web', 'assets', 'foo.js'))
..createSync(recursive: true)
..writeAsStringSync('function main() {}');
final Response response = await assetServer
.handle(Request('GET', Uri.parse('http://localhost:8080/assets/foo.js')));
expect(response.headers, <String, String>{
'Content-Type': 'application/javascript',
'content-length': '18',
});
}));
test('release asset server serves correct mime type and content length for html', () => testbed.run(() async {
assetServer = ReleaseAssetServer();
fs.file(fs.path.join('build', 'web', 'assets', 'foo.html'))
..createSync(recursive: true)
..writeAsStringSync('<!doctype html><html></html>');
final Response response = await assetServer
.handle(Request('GET', Uri.parse('http://localhost:8080/assets/foo.html')));
expect(response.headers, <String, String>{
'Content-Type': 'text/html',
'content-length': '28',
});
}));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment