Skip to content

Commit

Permalink
Add error message for missing content-type header (#2197)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
brandones and joeldenning committed Jun 27, 2020
1 parent d37f7ca commit 809e91f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/extras/module-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions test/browser/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/browser/content-type-none.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ok": "content but",
"no": "content-type"
}
3 changes: 3 additions & 0 deletions test/fixtures/browser/content-type-xml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"strange": "content-type header"
}
9 changes: 6 additions & 3 deletions test/server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 809e91f

Please sign in to comment.