Skip to content

Commit

Permalink
fix: do not overwrite Content-Type if header already exists (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmohns authored and evilebottnawi committed Mar 6, 2019
1 parent e5bd8f8 commit b2a6fed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ module.exports = function wrapper(context) {
contentType += '; charset=UTF-8';
}

res.setHeader('Content-Type', contentType);
if (!res.getHeader('Content-Type')) {
res.setHeader('Content-Type', contentType);
}
res.setHeader('Content-Length', content.length);

const { headers } = context.options;
Expand Down
21 changes: 21 additions & 0 deletions test/tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,27 @@ describe('Server', () => {
});
});

describe('custom Content-Type', () => {
before((done) => {
app = express();
const compiler = webpack(webpackConfig);
instance = middleware(compiler, {
stats: 'errors-only',
logLevel,
headers: { 'Content-Type': 'application/octet-stream' }
});
app.use(instance);
listen = listenShorthand(done);
});
after(close);

it('Do not guess mime type if Content-Type header is found ', (done) => {
request(app).get('/bundle.js')
.expect('Content-Type', 'application/octet-stream')
.expect(200, done);
});
});

describe('custom mimeTypes', () => {
before((done) => {
app = express();
Expand Down

1 comment on commit b2a6fed

@yanni4night
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We got an exception now:

 UnhandledPromiseRejectionWarning: TypeError: res.getHeader is not a function

Please # to comment.