mirror of
https://github.com/systemjs/systemjs.git
synced 2026-01-18 14:53:14 +00:00
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:
parent
d37f7cade3
commit
809e91f4ee
@ -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);
|
||||
|
||||
@ -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');
|
||||
|
||||
4
test/fixtures/browser/content-type-none.json
vendored
Normal file
4
test/fixtures/browser/content-type-none.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"ok": "content but",
|
||||
"no": "content-type"
|
||||
}
|
||||
3
test/fixtures/browser/content-type-xml.json
vendored
Normal file
3
test/fixtures/browser/content-type-xml.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"strange": "content-type header"
|
||||
}
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user