Skip to content

Commit

Permalink
fix(Server): validate express.static.mime.types (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy authored and evilebottnawi committed Apr 5, 2019
1 parent 1b3cd4f commit 919ff77
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ class Server {
if (options.lazy && !options.filename) {
throw new Error("'filename' option must be set in lazy mode.");
}

// if the user enables http2, we can safely enable https
if (options.http2 && !options.https) {
options.https = true;
}

updateCompiler(compiler, options);

this.stats =
Expand Down Expand Up @@ -173,8 +173,11 @@ class Server {
const app = (this.app = new express());

// ref: https://github.com/webpack/webpack-dev-server/issues/1575
// ref: https://github.com/webpack/webpack-dev-server/issues/1724
// remove this when send@^0.16.3
express.static.mime.types.wasm = 'application/wasm';
if (express.static && express.static.mime && express.static.mime.types) {
express.static.mime.types.wasm = 'application/wasm';
}

app.all('*', (req, res, next) => {
if (this.checkHost(req.headers)) {
Expand Down
49 changes: 49 additions & 0 deletions test/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,55 @@ const allStats = [
];

describe('Server', () => {
// issue: https://github.com/webpack/webpack-dev-server/issues/1724
describe('express.static.mine.types', () => {
beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
jest.unmock('express');
});

it("should success even if mine.types doesn't exist", () => {
// eslint-disable-next-line
const Server = require('../lib/Server');

jest.mock('express', () => {
const data = jest.requireActual('express');
const { static: st } = data;
const { mime } = st;

delete mime.types;

expect(typeof mime.types).toEqual('undefined');

return Object.assign(data, {
static: Object.assign(st, {
mime,
}),
});
});

return new Promise((res) => {
const compiler = webpack(config);
const server = new Server(compiler);

compiler.hooks.done.tap('webpack-dev-server', (s) => {
const output = server.getStats(s);
expect(output.errors.length).toEqual(0);

server.close(() => {
res();
});
});

compiler.run(() => {});
server.listen(8080, 'localhost');
});
});
});

it('should cascade warningsFilter', () => {
const stats = { warningsFilter: 'test' };
return new Promise((res) => {
Expand Down

0 comments on commit 919ff77

Please sign in to comment.