Skip to content

Commit

Permalink
test: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed May 26, 2021
1 parent 8bd1948 commit a20867a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 52 deletions.
52 changes: 0 additions & 52 deletions test/server/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,25 +301,6 @@ describe('Server', () => {
});
});

it('should always allow any host if options.allowedHosts is enabled', () => {
const options = {
client: {
webSocketURL: 'ws://test.host:80',
},
allowedHosts: true,
};

const headers = {
host: 'bad.host',
};

server = createServer(compiler, options);

if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});

it('should allow any valid options.client.webSocketURL when host is localhost', () => {
const options = {
client: {
Expand Down Expand Up @@ -429,39 +410,6 @@ describe('Server', () => {
throw new Error("Validation didn't fail");
}
});

describe('allowedHosts', () => {
it('should allow hosts in allowedHosts', () => {
const tests = ['test.host', 'test2.host', 'test3.host'];
const options = { allowedHosts: tests };
server = createServer(compiler, options);
tests.forEach((test) => {
const headers = { host: test };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});

it('should allow hosts that pass a wildcard in allowedHosts', () => {
const options = { allowedHosts: ['.example.com'] };
server = createServer(compiler, options);
const tests = [
'www.example.com',
'subdomain.example.com',
'example.com',
'subsubcomain.subdomain.example.com',
'example.com:80',
'subdomain.example.com:80',
];
tests.forEach((test) => {
const headers = { host: test };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});
});
});

describe('Invalidate Callback', () => {
Expand Down
72 changes: 72 additions & 0 deletions test/server/allowedHosts-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const webpack = require('webpack');
const Server = require('../../lib/Server');
const config = require('../fixtures/simple-config/webpack.config');

const createServer = (compiler, options) => new Server(options, compiler);

describe('allowedHosts', () => {
let compiler;
let server;

beforeEach(() => {
compiler = webpack(config);
});

afterEach((done) => {
server.close(() => {
done();
});
});

it('should always allow any host if options.allowedHosts is enabled', () => {
const options = {
client: {
webSocketURL: 'ws://test.host:80',
},
allowedHosts: true,
};

const headers = {
host: 'bad.host',
};

server = createServer(compiler, options);

if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});

it('should allow hosts in allowedHosts', () => {
const tests = ['test.host', 'test2.host', 'test3.host'];
const options = { allowedHosts: tests };
server = createServer(compiler, options);
tests.forEach((test) => {
const headers = { host: test };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});

it('should allow hosts that pass a wildcard in allowedHosts', () => {
const options = { allowedHosts: ['.example.com'] };
server = createServer(compiler, options);
const tests = [
'www.example.com',
'subdomain.example.com',
'example.com',
'subsubcomain.subdomain.example.com',
'example.com:80',
'subdomain.example.com:80',
];
tests.forEach((test) => {
const headers = { host: test };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});
});

0 comments on commit a20867a

Please sign in to comment.