diff --git a/cometd-nodejs-client.js b/cometd-nodejs-client.js index 997f741..d6816c8 100644 --- a/cometd-nodejs-client.js +++ b/cometd-nodejs-client.js @@ -1,13 +1,30 @@ +/* + * Copyright (c) 2017-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; + module.exports = { - adapt: function(options) { - var url = require('url'); - var httpc = require('http'); - var https = require('https'); - var HttpcProxyAgent = require('http-proxy-agent'); - var HttpsProxyAgent = require('https-proxy-agent'); - var ws = require('ws'); - - var window = global['window']; + adapt: options => { + const url = require('url'); + const httpc = require('http'); + const https = require('https'); + const HttpcProxyAgent = require('http-proxy-agent'); + const HttpsProxyAgent = require('https-proxy-agent'); + const ws = require('ws'); + + let window = global['window']; if (!window) { window = global['window'] = {}; } @@ -19,15 +36,15 @@ module.exports = { window.console.debug = window.console.log; // Fields shared by all XMLHttpRequest instances. - var _agentOptions = { + const _agentOptions = { keepAlive: true }; - var _agentc = new httpc.Agent(_agentOptions); - var _agents = new https.Agent(_agentOptions); + const _agentc = new httpc.Agent(_agentOptions); + const _agents = new https.Agent(_agentOptions); - var _globalCookies = {}; + const _globalCookies = {}; - var _logLevel = options && options.logLevel || 'info'; + const _logLevel = options && options.logLevel || 'info'; function _debug() { if (_logLevel === 'debug') { @@ -41,20 +58,20 @@ module.exports = { // Bare minimum XMLHttpRequest implementation that works with CometD. window.XMLHttpRequest = function() { - var _localCookies = {}; - var _config; - var _request; + const _localCookies = {}; + let _config; + let _request; function _storeCookie(cookieStore, value) { - var host = _config.hostname; - var jar = cookieStore[host]; + const host = _config.hostname; + let jar = cookieStore[host]; if (jar === undefined) { cookieStore[host] = jar = {}; } - var cookies = value.split(';'); - for (var i = 0; i < cookies.length; ++i) { - var cookie = cookies[i].trim(); - var equal = cookie.indexOf('='); + const cookies = value.split(';'); + for (let i = 0; i < cookies.length; ++i) { + const cookie = cookies[i].trim(); + const equal = cookie.indexOf('='); if (equal > 0) { jar[cookie.substring(0, equal)] = cookie; } @@ -62,10 +79,10 @@ module.exports = { } function _concatCookies(cookieStore) { - var cookies = ''; - var jar = cookieStore[_config.hostname]; + let cookies = ''; + const jar = cookieStore[_config.hostname]; if (jar) { - for (var name in jar) { + for (let name in jar) { if (jar.hasOwnProperty(name)) { if (cookies) { cookies += '; '; @@ -78,14 +95,14 @@ module.exports = { } function _chooseAgent(serverURI) { - var serverHostPort = serverURI.host; - var proxy = options && options.httpProxy && options.httpProxy.uri; + const serverHostPort = serverURI.host; + const proxy = options && options.httpProxy && options.httpProxy.uri; if (proxy) { - var isIncluded = true; - var includes = options.httpProxy.includes; + let isIncluded = true; + const includes = options.httpProxy.includes; if (includes && Array.isArray(includes)) { isIncluded = false; - for (var i = 0; i < includes.length; ++i) { + for (let i = 0; i < includes.length; ++i) { if (new RegExp(includes[i]).test(serverHostPort)) { isIncluded = true; break; @@ -93,9 +110,9 @@ module.exports = { } } if (isIncluded) { - var excludes = options.httpProxy.excludes; + const excludes = options.httpProxy.excludes; if (excludes && Array.isArray(excludes)) { - for (var e = 0; e < excludes.length; ++e) { + for (let e = 0; e < excludes.length; ++e) { if (new RegExp(excludes[e]).test(serverHostPort)) { isIncluded = false; break; @@ -105,7 +122,7 @@ module.exports = { } if (isIncluded) { _debug('proxying', serverURI.href, 'via', proxy); - var agentOpts = Object.assign(url.parse(proxy), _agentOptions); + const agentOpts = Object.assign(url.parse(proxy), _agentOptions); return _secure(serverURI) ? new HttpsProxyAgent(agentOpts) : new HttpcProxyAgent(agentOpts); } } @@ -117,7 +134,7 @@ module.exports = { this.readyState = window.XMLHttpRequest.UNSENT; this.responseText = ''; - this.open = function(method, uri) { + this.open = (method, uri) => { _config = url.parse(uri); _config.agent = _chooseAgent(_config); _config.method = method; @@ -125,7 +142,7 @@ module.exports = { this.readyState = window.XMLHttpRequest.OPENED; }; - this.setRequestHeader = function(name, value) { + this.setRequestHeader = (name, value) => { if (/^cookie$/i.test(name)) { _storeCookie(_localCookies, value) } else { @@ -133,71 +150,70 @@ module.exports = { } }; - this.send = function(data) { - var globalCookies = this.context && this.context.cookieStore; + this.send = data => { + let globalCookies = this.context && this.context.cookieStore; if (!globalCookies) { globalCookies = _globalCookies; } - var cookies1 = _concatCookies(globalCookies); - var cookies2 = _concatCookies(_localCookies); - var delim = (cookies1 && cookies2) ? '; ' : ''; - var cookies = cookies1 + delim + cookies2; + const cookies1 = _concatCookies(globalCookies); + const cookies2 = _concatCookies(_localCookies); + const delim = (cookies1 && cookies2) ? '; ' : ''; + const cookies = cookies1 + delim + cookies2; if (cookies) { _config.headers['Cookie'] = cookies; } - var self = this; - var http = _secure(_config) ? https : httpc; - _request = http.request(_config, function(response) { - var success = false; - self.status = response.statusCode; - self.statusText = response.statusMessage; - self.readyState = window.XMLHttpRequest.HEADERS_RECEIVED; - var headers = response.headers; - for (var name in headers) { + const http = _secure(_config) ? https : httpc; + _request = http.request(_config, response => { + let success = false; + this.status = response.statusCode; + this.statusText = response.statusMessage; + this.readyState = window.XMLHttpRequest.HEADERS_RECEIVED; + const headers = response.headers; + for (let name in headers) { if (headers.hasOwnProperty(name)) { if (/^set-cookie$/i.test(name)) { - var header = headers[name]; - for (var i = 0; i < header.length; ++i) { - var whole = header[i]; - var parts = whole.split(';'); - var cookie = parts[0]; + const header = headers[name]; + for (let i = 0; i < header.length; ++i) { + const whole = header[i]; + const parts = whole.split(';'); + const cookie = parts[0]; _storeCookie(globalCookies, cookie); } } } } - response.on('data', function(chunk) { - self.readyState = window.XMLHttpRequest.LOADING; - self.responseText += chunk; + response.on('data', chunk => { + this.readyState = window.XMLHttpRequest.LOADING; + this.responseText += chunk; }); - response.on('end', function() { + response.on('end', () => { success = true; - self.readyState = window.XMLHttpRequest.DONE; - if (self.onload) { - self.onload(); + this.readyState = window.XMLHttpRequest.DONE; + if (this.onload) { + this.onload(); } }); - response.on('close', function() { + response.on('close', () => { if (!success) { - self.readyState = window.XMLHttpRequest.DONE; - if (self.onerror) { - self.onerror(); + this.readyState = window.XMLHttpRequest.DONE; + if (this.onerror) { + this.onerror(); } } }); }); - ['abort', 'aborted', 'error'].forEach(function(event) { - _request.on(event, function(x) { - self.readyState = window.XMLHttpRequest.DONE; + ['abort', 'aborted', 'error'].forEach(event => { + _request.on(event, x => { + this.readyState = window.XMLHttpRequest.DONE; if (x) { - var error = x.message; + const error = x.message; if (error) { - self.statusText = error; + this.statusText = error; } } - if (self.onerror) { - self.onerror(x); + if (this.onerror) { + this.onerror(x); } }); }); @@ -207,15 +223,13 @@ module.exports = { _request.end(); }; - this.abort = function() { + this.abort = () => { if (_request) { _request.abort(); } }; - this._config = function() { - return _config; - }; + this._config = () => _config; }; window.XMLHttpRequest.UNSENT = 0; window.XMLHttpRequest.OPENED = 1; diff --git a/test/client.js b/test/client.js index 9cb2d07..55e7d9c 100644 --- a/test/client.js +++ b/test/client.js @@ -1,31 +1,48 @@ -var cometd = require('..'); -var http = require('http'); +/* + * Copyright (c) 2017-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; -describe('client', function() { - var _lib; - var _server; +const cometd = require('..'); +const http = require('http'); - beforeEach(function() { +describe('client', () => { + let _lib; + let _server; + + beforeEach(() => { cometd.adapt(); _lib = require('cometd'); }); - afterEach(function() { + afterEach(() => { if (_server) { _server.close(); } }); - it('performs handshake', function(done) { - _server = http.createServer(function(request, response) { - var content = ''; - request.addListener('data', function(chunk) { + it('performs handshake', done => { + _server = http.createServer((request, response) => { + let content = ''; + request.addListener('data', chunk => { content += chunk; }); - request.addListener('end', function() { + request.addListener('end', () => { response.statusCode = 200; response.setHeader('Content-Type', 'application/json'); - var content = '[{' + + const content = '[{' + '"id":"1",' + '"version":"1.0",' + '"channel":"/meta/handshake",' + @@ -37,17 +54,17 @@ describe('client', function() { response.end(content, 'utf8'); }); }); - _server.listen(0, 'localhost', function() { - var port = _server.address().port; + _server.listen(0, 'localhost', () => { + const port = _server.address().port; console.log('listening on localhost:' + port); - var cometd = new _lib.CometD(); + const cometd = new _lib.CometD(); cometd.websocketEnabled = false; cometd.configure({ url: 'http://localhost:' + port + '/cometd', logLevel: 'info' }); - cometd.handshake(function(r) { + cometd.handshake(r => { if (r.successful) { done(); } diff --git a/test/cookies.js b/test/cookies.js index b8f9176..39cfeb5 100644 --- a/test/cookies.js +++ b/test/cookies.js @@ -1,25 +1,42 @@ -var assert = require('assert'); -var cometd = require('..'); -var http = require('http'); +/* + * Copyright (c) 2017-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; -describe('cookies', function() { - var _runtime; - var _server; +const assert = require('assert'); +const cometd = require('..'); +const http = require('http'); - beforeEach(function() { +describe('cookies', () => { + let _runtime; + let _server; + + beforeEach(() => { cometd.adapt(); _runtime = global.window; }); - afterEach(function() { + afterEach(() => { if (_server) { _server.close(); } }); - it('receives, stores and sends cookie', function(done) { - var cookie = 'a=b'; - _server = http.createServer(function(request, response) { + it('receives, stores and sends cookie', done => { + const cookie = 'a=b'; + _server = http.createServer((request, response) => { if (/\/1$/.test(request.url)) { response.setHeader('Set-Cookie', cookie); response.end(); @@ -28,18 +45,18 @@ describe('cookies', function() { response.end(); } }); - _server.listen(0, 'localhost', function() { - var port = _server.address().port; + _server.listen(0, 'localhost', () => { + const port = _server.address().port; console.log('listening on localhost:' + port); - var uri = 'http://localhost:' + port; + const uri = 'http://localhost:' + port; - var xhr1 = new _runtime.XMLHttpRequest(); + const xhr1 = new _runtime.XMLHttpRequest(); xhr1.open('GET', uri + '/1'); - xhr1.onload = function() { + xhr1.onload = () => { assert.strictEqual(xhr1.status, 200); - var xhr2 = new _runtime.XMLHttpRequest(); + const xhr2 = new _runtime.XMLHttpRequest(); xhr2.open('GET', uri + '/2'); - xhr2.onload = function() { + xhr2.onload = () => { assert.strictEqual(xhr2.status, 200); done(); }; @@ -49,11 +66,11 @@ describe('cookies', function() { }); }); - it('sends multiple cookies', function(done) { - var cookie1 = 'a=b'; - var cookie2 = 'c=d'; - var cookies = cookie1 + '; ' + cookie2; - _server = http.createServer(function(request, response) { + it('sends multiple cookies', done => { + const cookie1 = 'a=b'; + const cookie2 = 'c=d'; + const cookies = cookie1 + '; ' + cookie2; + _server = http.createServer((request, response) => { if (/\/1$/.test(request.url)) { response.setHeader('Set-Cookie', cookie1); response.end(); @@ -65,22 +82,22 @@ describe('cookies', function() { response.end(); } }); - _server.listen(0, 'localhost', function() { - var port = _server.address().port; + _server.listen(0, 'localhost', () => { + const port = _server.address().port; console.log('listening on localhost:' + port); - var uri = 'http://localhost:' + port; + const uri = 'http://localhost:' + port; - var xhr1 = new _runtime.XMLHttpRequest(); + const xhr1 = new _runtime.XMLHttpRequest(); xhr1.open('GET', uri + '/1'); - xhr1.onload = function() { + xhr1.onload = () => { assert.strictEqual(xhr1.status, 200); - var xhr2 = new _runtime.XMLHttpRequest(); + const xhr2 = new _runtime.XMLHttpRequest(); xhr2.open('GET', uri + '/2'); - xhr2.onload = function() { + xhr2.onload = () => { assert.strictEqual(xhr2.status, 200); - var xhr3 = new _runtime.XMLHttpRequest(); + const xhr3 = new _runtime.XMLHttpRequest(); xhr3.open('GET', uri + '/3'); - xhr3.onload = function() { + xhr3.onload = () => { assert.strictEqual(xhr3.status, 200); done(); }; @@ -92,10 +109,10 @@ describe('cookies', function() { }); }); - it('handles cookies from different hosts', function(done) { - var cookieA = 'a=b'; - var cookieB = 'b=c'; - _server = http.createServer(function(request, response) { + it('handles cookies from different hosts', done => { + const cookieA = 'a=b'; + const cookieB = 'b=c'; + _server = http.createServer((request, response) => { if (/\/hostA\//.test(request.url)) { if (/\/1$/.test(request.url)) { response.setHeader('Set-Cookie', cookieA); @@ -114,26 +131,26 @@ describe('cookies', function() { } } }); - _server.listen(0, 'localhost', function() { - var port = _server.address().port; + _server.listen(0, 'localhost', () => { + const port = _server.address().port; console.log('listening on localhost:' + port); - var xhrA1 = new _runtime.XMLHttpRequest(); + const xhrA1 = new _runtime.XMLHttpRequest(); xhrA1.open('GET', 'http://localhost:' + port + '/hostA/1'); - xhrA1.onload = function() { + xhrA1.onload = () => { assert.strictEqual(xhrA1.status, 200); - var xhrA2 = new _runtime.XMLHttpRequest(); + const xhrA2 = new _runtime.XMLHttpRequest(); xhrA2.open('GET', 'http://localhost:' + port + '/hostA/2'); - xhrA2.onload = function() { + xhrA2.onload = () => { assert.strictEqual(xhrA2.status, 200); - var xhrB1 = new _runtime.XMLHttpRequest(); + const xhrB1 = new _runtime.XMLHttpRequest(); xhrB1.open('GET', 'http://127.0.0.1:' + port + '/hostB/1'); - xhrB1.onload = function() { + xhrB1.onload = () => { assert.strictEqual(xhrB1.status, 200); - var xhrB2 = new _runtime.XMLHttpRequest(); + const xhrB2 = new _runtime.XMLHttpRequest(); xhrB2.open('GET', 'http://127.0.0.1:' + port + '/hostB/2'); - xhrB2.onload = function() { + xhrB2.onload = () => { assert.strictEqual(xhrB2.status, 200); done(); }; @@ -147,11 +164,11 @@ describe('cookies', function() { }); }); - it('handles cookie sent multiple times', function(done) { - var cookieName = 'a'; - var cookieValue = 'b'; - var cookie = cookieName + '=' + cookieValue; - _server = http.createServer(function(request, response) { + it('handles cookie sent multiple times', done => { + const cookieName = 'a'; + const cookieValue = 'b'; + const cookie = cookieName + '=' + cookieValue; + _server = http.createServer((request, response) => { if (/\/verify$/.test(request.url)) { assert.strictEqual(request.headers['cookie'], cookie); response.end(); @@ -160,21 +177,21 @@ describe('cookies', function() { response.end(); } }); - _server.listen(0, 'localhost', function() { - var port = _server.address().port; + _server.listen(0, 'localhost', () => { + const port = _server.address().port; console.log('listening on localhost:' + port); - var xhr1 = new _runtime.XMLHttpRequest(); + const xhr1 = new _runtime.XMLHttpRequest(); xhr1.open('GET', 'http://localhost:' + port + '/1'); - xhr1.onload = function() { + xhr1.onload = () => { assert.strictEqual(xhr1.status, 200); - var xhr2 = new _runtime.XMLHttpRequest(); + const xhr2 = new _runtime.XMLHttpRequest(); xhr2.open('GET', 'http://localhost:' + port + '/2'); - xhr2.onload = function() { + xhr2.onload = () => { assert.strictEqual(xhr2.status, 200); - var xhr3 = new _runtime.XMLHttpRequest(); + const xhr3 = new _runtime.XMLHttpRequest(); xhr3.open('GET', 'http://localhost:' + port + '/verify'); - xhr3.onload = function() { + xhr3.onload = () => { assert.strictEqual(xhr1.status, 200); done(); }; @@ -186,9 +203,9 @@ describe('cookies', function() { }); }); - it('handles cookies as request headers', function(done) { - _server = http.createServer(function(request, response) { - var cookies = request.headers['cookie']; + it('handles cookies as request headers', done => { + _server = http.createServer((request, response) => { + const cookies = request.headers['cookie']; if (/\/1$/.test(request.url)) { response.setHeader('Set-Cookie', 'a=b'); response.end(); @@ -204,22 +221,22 @@ describe('cookies', function() { response.end(); } }); - _server.listen(0, 'localhost', function() { - var port = _server.address().port; + _server.listen(0, 'localhost', () => { + const port = _server.address().port; console.log('listening on localhost:' + port); - var xhr1 = new _runtime.XMLHttpRequest(); + const xhr1 = new _runtime.XMLHttpRequest(); xhr1.open('GET', 'http://localhost:' + port + '/1'); - xhr1.onload = function() { + xhr1.onload = () => { assert.strictEqual(xhr1.status, 200); - var xhr2 = new _runtime.XMLHttpRequest(); + const xhr2 = new _runtime.XMLHttpRequest(); xhr2.open('GET', 'http://localhost:' + port + '/2'); xhr2.setRequestHeader('cookie', 'c=d; e=f'); - xhr2.onload = function() { + xhr2.onload = () => { assert.strictEqual(xhr2.status, 200); - var xhr3 = new _runtime.XMLHttpRequest(); + const xhr3 = new _runtime.XMLHttpRequest(); xhr3.open('GET', 'http://localhost:' + port + '/3'); - xhr3.onload = function() { + xhr3.onload = () => { assert.strictEqual(xhr3.status, 200); done(); }; diff --git a/test/https.js b/test/https.js index 17e6b40..0fafb0a 100644 --- a/test/https.js +++ b/test/https.js @@ -1,40 +1,57 @@ -var assert = require('assert'); -var cometd = require('..'); -var https = require('https'); -var fs = require('fs'); +/* + * Copyright (c) 2017-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; -describe('https', function() { - var _runtime; - var _server; +const assert = require('assert'); +const cometd = require('..'); +const https = require('https'); +const fs = require('fs'); - beforeEach(function() { +describe('https', () => { + let _runtime; + let _server; + + beforeEach(() => { cometd.adapt(); _runtime = global.window; }); - afterEach(function() { + afterEach(() => { if (_server) { _server.close(); } }); - it('supports https', function(done) { - var options = { + it('supports https', done => { + const options = { key: fs.readFileSync('test/tls/private.pem'), cert: fs.readFileSync('test/tls/public.pem') }; - _server = https.createServer(options, function(request, response) { + _server = https.createServer(options, (request, response) => { response.end(); }); - _server.listen(0, 'localhost', function() { - var port = _server.address().port; + _server.listen(0, 'localhost', () => { + const port = _server.address().port; console.log('listening on localhost:' + port); - var uri = 'https://localhost:' + port; - var xhr = new _runtime.XMLHttpRequest(); + const uri = 'https://localhost:' + port; + const xhr = new _runtime.XMLHttpRequest(); xhr.open('GET', uri + '/'); // Allow self-signed certificates. xhr._config().rejectUnauthorized = false; - xhr.onload = function() { + xhr.onload = () => { assert.strictEqual(xhr.status, 200); done(); }; diff --git a/test/library.js b/test/library.js index 10624d0..54155e5 100644 --- a/test/library.js +++ b/test/library.js @@ -1,8 +1,25 @@ -var assert = require('assert'); -var cometd = require('..'); +/* + * Copyright (c) 2017-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; -describe('library', function() { - it('adapter method exported', function() { +const assert = require('assert'); +const cometd = require('..'); + +describe('library', () => { + it('adapter method exported', () => { assert.ok(cometd.adapt); }); }); diff --git a/test/proxy.js b/test/proxy.js index 019bcc9..2624bae 100644 --- a/test/proxy.js +++ b/test/proxy.js @@ -1,32 +1,49 @@ -var assert = require('assert'); -var nodeCometD = require('..'); -var http = require('http'); -var url = require('url'); +/* + * Copyright (c) 2017-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; -describe('proxy', function() { - var _lib = require('cometd'); - var _proxy; +const assert = require('assert'); +const nodeCometD = require('..'); +const http = require('http'); +const url = require('url'); - afterEach(function() { +describe('proxy', () => { + const _lib = require('cometd'); + let _proxy; + + afterEach(() => { if (_proxy) { _proxy.close(); } }); - it('proxies cometd calls', function(done) { - _proxy = http.createServer(function(request, response) { - var serverPort = parseInt(url.parse(request.url).port); + it('proxies cometd calls', done => { + _proxy = http.createServer((request, response) => { + const serverPort = parseInt(url.parse(request.url).port); assert.ok(Number.isInteger(serverPort)); assert.notStrictEqual(serverPort, _proxy.address().port); - var content = ''; - request.addListener('data', function(chunk) { + let content = ''; + request.addListener('data', chunk => { content += chunk; }); - request.addListener('end', function() { + request.addListener('end', () => { response.statusCode = 200; response.setHeader('Content-Type', 'application/json'); - var content = '[{' + + const content = '[{' + '"id":"1",' + '"version":"1.0",' + '"channel":"/meta/handshake",' + @@ -38,8 +55,8 @@ describe('proxy', function() { response.end(content, 'utf8'); }); }); - _proxy.listen(0, 'localhost', function() { - var proxyPort = _proxy.address().port; + _proxy.listen(0, 'localhost', () => { + const proxyPort = _proxy.address().port; console.log('proxy listening on localhost:' + proxyPort); nodeCometD.adapt({ @@ -50,14 +67,14 @@ describe('proxy', function() { }); // Any port will do for the server. - var serverPort = proxyPort + 1; - var cometd = new _lib.CometD(); + const serverPort = proxyPort + 1; + const cometd = new _lib.CometD(); cometd.websocketEnabled = false; cometd.configure({ url: 'http://localhost:' + serverPort + '/cometd', logLevel: 'info' }); - cometd.handshake(function(r) { + cometd.handshake(r => { if (r.successful) { done(); } else { @@ -69,20 +86,20 @@ describe('proxy', function() { }); }); - it('proxies with includes list', function(done) { - _proxy = http.createServer(function(request, response) { - var serverPort = parseInt(url.parse(request.url).port); + it('proxies with includes list', done => { + _proxy = http.createServer((request, response) => { + const serverPort = parseInt(url.parse(request.url).port); assert.ok(Number.isInteger(serverPort)); assert.notStrictEqual(serverPort, _proxy.address().port); - var content = ''; - request.addListener('data', function(chunk) { + let content = ''; + request.addListener('data', chunk => { content += chunk; }); - request.addListener('end', function() { + request.addListener('end', () => { response.statusCode = 200; response.setHeader('Content-Type', 'application/json'); - var content = '[{' + + const content = '[{' + '"id":"1",' + '"version":"1.0",' + '"channel":"/meta/handshake",' + @@ -94,12 +111,12 @@ describe('proxy', function() { response.end(content, 'utf8'); }); }); - _proxy.listen(0, 'localhost', function() { - var proxyPort = _proxy.address().port; + _proxy.listen(0, 'localhost', () => { + const proxyPort = _proxy.address().port; console.log('proxy listening on localhost:' + proxyPort); // Any port will do for the server. - var serverPort1 = proxyPort + 1; - var serverPort2 = proxyPort + 2; + const serverPort1 = proxyPort + 1; + const serverPort2 = proxyPort + 2; nodeCometD.adapt({ httpProxy: { @@ -108,21 +125,21 @@ describe('proxy', function() { } }); - var cometd1 = new _lib.CometD(); + const cometd1 = new _lib.CometD(); cometd1.websocketEnabled = false; cometd1.configure({ url: 'http://localhost:' + serverPort1 + '/cometd', logLevel: 'info' }); - cometd1.handshake(function(r) { + cometd1.handshake(r => { if (r.successful) { - var cometd2 = new _lib.CometD(); + const cometd2 = new _lib.CometD(); cometd2.websocketEnabled = false; cometd2.configure({ url: 'http://localhost:' + serverPort2 + '/cometd', logLevel: 'info' }); - cometd2.handshake(function(r) { + cometd2.handshake(r => { if (r.successful) { done(new Error('must not handshake')); } else { @@ -139,20 +156,20 @@ describe('proxy', function() { }); }); - it('proxies with excludes list', function(done) { - _proxy = http.createServer(function(request, response) { - var serverPort = parseInt(url.parse(request.url).port); + it('proxies with excludes list', done => { + _proxy = http.createServer((request, response) => { + const serverPort = parseInt(url.parse(request.url).port); assert.ok(Number.isInteger(serverPort)); assert.notStrictEqual(serverPort, _proxy.address().port); - var content = ''; - request.addListener('data', function(chunk) { + let content = ''; + request.addListener('data', chunk => { content += chunk; }); - request.addListener('end', function() { + request.addListener('end', () => { response.statusCode = 200; response.setHeader('Content-Type', 'application/json'); - var content = '[{' + + const content = '[{' + '"id":"1",' + '"version":"1.0",' + '"channel":"/meta/handshake",' + @@ -164,12 +181,12 @@ describe('proxy', function() { response.end(content, 'utf8'); }); }); - _proxy.listen(0, 'localhost', function() { - var proxyPort = _proxy.address().port; + _proxy.listen(0, 'localhost', () => { + const proxyPort = _proxy.address().port; console.log('proxy listening on localhost:' + proxyPort); // Any port will do for the server. - var serverPort1 = proxyPort + 1; - var serverPort2 = proxyPort + 2; + const serverPort1 = proxyPort + 1; + const serverPort2 = proxyPort + 2; nodeCometD.adapt({ httpProxy: { @@ -178,25 +195,25 @@ describe('proxy', function() { } }); - var cometd1 = new _lib.CometD(); + const cometd1 = new _lib.CometD(); cometd1.websocketEnabled = false; cometd1.configure({ url: 'http://localhost:' + serverPort1 + '/cometd', logLevel: 'info' }); - cometd1.handshake(function(r) { + cometd1.handshake(r => { if (r.successful) { done(new Error('could not handshake')); } else { // Stop /meta/handshake retries. cometd1.disconnect(); - var cometd2 = new _lib.CometD(); + const cometd2 = new _lib.CometD(); cometd2.websocketEnabled = false; cometd2.configure({ url: 'http://localhost:' + serverPort2 + '/cometd', logLevel: 'info' }); - cometd2.handshake(function(r) { + cometd2.handshake(r => { if (r.successful) { done(); } else { @@ -210,20 +227,20 @@ describe('proxy', function() { }); }); - it('proxies with includes and excludes list', function(done) { - _proxy = http.createServer(function(request, response) { - var serverPort = parseInt(url.parse(request.url).port); + it('proxies with includes and excludes list', done => { + _proxy = http.createServer((request, response) => { + const serverPort = parseInt(url.parse(request.url).port); assert.ok(Number.isInteger(serverPort)); assert.notStrictEqual(serverPort, _proxy.address().port); - var content = ''; - request.addListener('data', function(chunk) { + let content = ''; + request.addListener('data', chunk => { content += chunk; }); - request.addListener('end', function() { + request.addListener('end', () => { response.statusCode = 200; response.setHeader('Content-Type', 'application/json'); - var content = '[{' + + const content = '[{' + '"id":"1",' + '"version":"1.0",' + '"channel":"/meta/handshake",' + @@ -235,12 +252,12 @@ describe('proxy', function() { response.end(content, 'utf8'); }); }); - _proxy.listen(0, 'localhost', function() { - var proxyPort = _proxy.address().port; + _proxy.listen(0, 'localhost', () => { + const proxyPort = _proxy.address().port; console.log('proxy listening on localhost:' + proxyPort); // Any port will do for the server. - var serverPort1 = proxyPort + 1; - var serverPort2 = proxyPort + 2; + const serverPort1 = proxyPort + 1; + const serverPort2 = proxyPort + 2; nodeCometD.adapt({ httpProxy: { @@ -250,25 +267,25 @@ describe('proxy', function() { } }); - var cometd1 = new _lib.CometD(); + const cometd1 = new _lib.CometD(); cometd1.websocketEnabled = false; cometd1.configure({ url: 'http://localhost:' + serverPort1 + '/cometd', logLevel: 'info' }); - cometd1.handshake(function(r) { + cometd1.handshake(r => { if (r.successful) { done(new Error('could not handshake')); } else { // Stop /meta/handshake retries. cometd1.disconnect(); - var cometd2 = new _lib.CometD(); + const cometd2 = new _lib.CometD(); cometd2.websocketEnabled = false; cometd2.configure({ url: 'http://localhost:' + serverPort2 + '/cometd', logLevel: 'info' }); - cometd2.handshake(function(r) { + cometd2.handshake(r => { if (r.successful) { done(); } else { @@ -282,20 +299,20 @@ describe('proxy', function() { }); }); - it('proxies with authentication', function(done) { - _proxy = http.createServer(function(request, response) { - var proxyAuth = request.headers['proxy-authorization']; + it('proxies with authentication', done => { + _proxy = http.createServer((request, response) => { + const proxyAuth = request.headers['proxy-authorization']; assert.ok(proxyAuth); assert.ok(proxyAuth.startsWith('Basic ')); - var content = ''; - request.addListener('data', function(chunk) { + let content = ''; + request.addListener('data', chunk => { content += chunk; }); - request.addListener('end', function() { + request.addListener('end', () => { response.statusCode = 200; response.setHeader('Content-Type', 'application/json'); - var content = '[{' + + const content = '[{' + '"id":"1",' + '"version":"1.0",' + '"channel":"/meta/handshake",' + @@ -307,8 +324,8 @@ describe('proxy', function() { response.end(content, 'utf8'); }); }); - _proxy.listen(0, 'localhost', function() { - var proxyPort = _proxy.address().port; + _proxy.listen(0, 'localhost', () => { + const proxyPort = _proxy.address().port; console.log('proxy listening on localhost:' + proxyPort); nodeCometD.adapt({ @@ -318,14 +335,14 @@ describe('proxy', function() { }); // Any port will do for the server. - var serverPort = proxyPort + 1; - var cometd = new _lib.CometD(); + const serverPort = proxyPort + 1; + const cometd = new _lib.CometD(); cometd.websocketEnabled = false; cometd.configure({ url: 'http://localhost:' + serverPort + '/cometd', logLevel: 'info' }); - cometd.handshake(function(r) { + cometd.handshake(r => { if (r.successful) { done(); } else { diff --git a/test/websocket.js b/test/websocket.js index ca8e207..15e73ec 100644 --- a/test/websocket.js +++ b/test/websocket.js @@ -1,25 +1,42 @@ -var assert = require('assert'); -var nodeCometD = require('..'); -var ws = require("ws"); +/* + * Copyright (c) 2017-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; -describe('websocket', function() { - var _lib = require('cometd'); - var _server; +const assert = require('assert'); +const nodeCometD = require('..'); +const ws = require("ws"); - afterEach(function() { +describe('websocket', () => { + const _lib = require('cometd'); + let _server; + + afterEach(() => { if (_server) { _server.close(); } }); - it('handshakes with websocket', function(done) { - _server = new ws.Server({port: 0}, function() { - _server.on('connection', function(s) { - s.on('message', function(m) { - var handshake = JSON.parse(m)[0]; + it('handshakes with websocket', done => { + _server = new ws.Server({port: 0}, () => { + _server.on('connection', s => { + s.on('message', m => { + const handshake = JSON.parse(m)[0]; assert.strictEqual('/meta/handshake', handshake.channel); assert.ok(handshake.supportedConnectionTypes.indexOf('websocket') >= 0); - var reply = [{ + const reply = [{ id: handshake.id, channel: handshake.channel, successful: true, @@ -35,12 +52,12 @@ describe('websocket', function() { }); nodeCometD.adapt(); - var cometd = new _lib.CometD(); + const cometd = new _lib.CometD(); cometd.configure({ url: 'http://localhost:' + _server.address().port, logLevel: 'info' }); - cometd.handshake(function(r) { + cometd.handshake(r => { if (r.successful) { done(); } else {