Add error message for missing content-type header (#2197)

* Add error message for missing content-type header

* Update src/extras/module-types.js

Co-authored-by: Joel Denning <joeldenning@gmail.com>

* Add tests for missing or invalid content-type headers

Co-authored-by: Joel Denning <joeldenning@gmail.com>
This commit is contained in:
Brandon Istenes 2020-06-26 20:39:54 -07:00 committed by GitHub
parent d37f7cade3
commit 809e91f4ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 3 deletions

View File

@ -26,6 +26,8 @@ import { errMsg } from '../err-msg.js';
if (!res.ok)
throw Error(errMsg(7, process.env.SYSTEM_PRODUCTION ? [res.status, res.statusText, url, parent].join(', ') : res.status + ' ' + res.statusText + ', loading ' + url + (parent ? ' from ' + parent : '')));
var contentType = res.headers.get('content-type');
if (!contentType)
throw Error(errMsg(4, process.env.SYSTEM_PRODUCTION ? [url, parent] : 'Missing header "Content-Type", loading ' + url + (parent ? ' from ' + parent : '')));
if (contentType.match(/^(text|application)\/(x-)?javascript(;|$)/)) {
return res.text().then(function (source) {
(0, eval)(source);

View File

@ -131,6 +131,19 @@ suite('SystemJS Standard Tests', function() {
});
});
test('Errors for bad Content-Type headers', function () {
return System.import('fixtures/content-type-none.json')
.catch(function (err) {
assert.ok(/missing.*content-type.*error#4/i.test(err));
})
.then(function () {
return System.import('fixtures/content-type-xml.json')
})
.catch(function (err) {
assert.ok(/unknown module type.*xml.*error#4/i.test(err));
})
});
if (typeof Worker !== 'undefined')
test('Using SystemJS in a Web Worker', function () {
const worker = new Worker('./browser/worker.js');

View File

@ -0,0 +1,4 @@
{
"ok": "content but",
"no": "content-type"
}

View File

@ -0,0 +1,3 @@
{
"strange": "content-type header"
}

View File

@ -84,12 +84,15 @@ http.createServer(async function (req, res) {
let mime;
if (filePath.endsWith('javascript.css'))
mime = 'application/javascript';
else if (filePath.endsWith('content-type-xml.json'))
mime = 'application/xml';
else
mime = mimes[path.extname(filePath)] || 'text/plain';
res.writeHead(200, {
'content-type': mime
});
const headers = filePath.endsWith('content-type-none.json') ?
{} : { 'content-type': mime }
res.writeHead(200, headers);
fileStream.pipe(res);
await once(fileStream, 'end');
res.end();