Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): check that serverMode implementation is correct #2051

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/utils/getSocketServerImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@ function getSocketServerImplementation(options) {
);
}

if (
!ServerImplementation.prototype.constructor ||
ServerImplementation.prototype.constructor.length < 1
) {
throw new Error(
'serverMode must have a constructor that takes a single server argument and calls super(server) ' +
"on the superclass BaseServer, found via require('webpack-dev-server/lib/servers/BaseServer')"
);
}

if (
!ServerImplementation.prototype.send ||
ServerImplementation.prototype.send.length < 2
) {
throw new Error(
'serverMode must have a send(connection, message) method that sends the message string to the provided client connection object'
);
}

if (
!ServerImplementation.prototype.close ||
ServerImplementation.prototype.close.length < 1
) {
throw new Error(
'serverMode must have a close(connection) method that closes the provided client connection object'
);
}

if (
!ServerImplementation.prototype.onConnection ||
ServerImplementation.prototype.onConnection.length < 1
) {
throw new Error(
'serverMode must have a onConnection(f) method that calls f(connection) whenever a new client connection is made'
);
}

return ServerImplementation;
}

Expand Down
87 changes: 83 additions & 4 deletions test/server/utils/getSocketServerImplementation.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';

/* eslint-disable constructor-super, no-empty-function, no-useless-constructor, no-unused-vars, class-methods-use-this */

const getSocketServerImplementation = require('../../../lib/utils/getSocketServerImplementation');
const BaseServer = require('../../../lib/servers/BaseServer');
const SockJSServer = require('../../../lib/servers/SockJSServer');

describe('getSocketServerImplementation util', () => {
it("should works with string serverMode ('sockjs')", () => {
it("should work with string serverMode ('sockjs')", () => {
let result;

expect(() => {
Expand All @@ -16,7 +19,7 @@ describe('getSocketServerImplementation util', () => {
expect(result).toEqual(SockJSServer);
});

it('should works with serverMode (SockJSServer class)', () => {
it('should work with serverMode (SockJSServer class)', () => {
let result;

expect(() => {
Expand All @@ -40,11 +43,87 @@ describe('getSocketServerImplementation util', () => {
expect(result).toEqual(SockJSServer);
});

it('should throws with serverMode (bad path)', () => {
it('should throw with serverMode (bad path)', () => {
expect(() => {
getSocketServerImplementation({
serverMode: '/bad/path/to/implementation',
});
}).toThrow(/serverMode must be a string/);
}).toThrow(/serverMode must be a string denoting a default implementation/);
});

it('should throw with serverMode (incorrect constructor)', () => {
expect(() => {
getSocketServerImplementation({
serverMode: class ServerImplementation extends BaseServer {
constructor() {}
},
});
}).toThrow(
"serverMode must have a constructor that takes a single server argument and calls super(server) on the superclass BaseServer, found via require('webpack-dev-server/lib/servers/BaseServer')"
);
});

it('should throw with serverMode (no send method)', () => {
expect(() => {
getSocketServerImplementation({
serverMode: class ServerImplementation extends BaseServer {
constructor(server) {
super(server);
}
},
});
}).toThrow(
'serverMode must have a send(connection, message) method that sends the message string to the provided client connection object'
);
});

it('should throw with serverMode (incorrect send parameters)', () => {
expect(() => {
getSocketServerImplementation({
serverMode: class ServerImplementation extends BaseServer {
constructor(server) {
super(server);
}

send(connection) {}
},
});
}).toThrow(
'serverMode must have a send(connection, message) method that sends the message string to the provided client connection object'
);
});

it('should throw with serverMode (no close method)', () => {
expect(() => {
getSocketServerImplementation({
serverMode: class ServerImplementation extends BaseServer {
constructor(server) {
super(server);
}

send(connection, message) {}
},
});
}).toThrow(
'serverMode must have a close(connection) method that closes the provided client connection object'
);
});

it('should throw with serverMode (no onConnection method)', () => {
expect(() => {
getSocketServerImplementation({
serverMode: class ServerImplementation extends BaseServer {
constructor(server) {
super(server);
}

send(connection, message) {}

close(connection) {}
},
});
}).toThrow(
'serverMode must have a onConnection(f) method that calls f(connection) whenever a new client connection is made'
);
});
});