overleaf/services/real-time/test/unit/js/WebsocketAddressManagerTests.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

100 lines
3 KiB
JavaScript

const SandboxedModule = require('sandboxed-module')
const { expect } = require('chai')
const modulePath = require('node:path').join(
__dirname,
'../../../app/js/WebsocketAddressManager'
)
describe('WebsocketAddressManager', function () {
beforeEach(function () {
this.WebsocketAddressManager = SandboxedModule.require(modulePath, {
requires: {},
})
})
describe('with a proxy configuration', function () {
beforeEach(function () {
this.websocketAddressManager = new this.WebsocketAddressManager(
true,
'127.0.0.1'
)
})
it('should return the client ip address when behind a proxy', function () {
expect(
this.websocketAddressManager.getRemoteIp({
headers: {
'x-forwarded-proto': 'https',
'x-forwarded-for': '123.45.67.89',
},
address: { address: '127.0.0.1' },
})
).to.equal('123.45.67.89')
})
it('should return the client ip address for a direct connection', function () {
expect(
this.websocketAddressManager.getRemoteIp({
headers: {},
address: { address: '123.45.67.89' },
})
).to.equal('123.45.67.89')
})
it('should return the client ip address when there are no headers in the handshake', function () {
expect(
this.websocketAddressManager.getRemoteIp({
address: { address: '123.45.67.89' },
})
).to.equal('123.45.67.89')
})
it('should return a "client-handshake-missing" response when the handshake is missing', function () {
expect(this.websocketAddressManager.getRemoteIp()).to.equal(
'client-handshake-missing'
)
})
})
describe('without a proxy configuration', function () {
beforeEach(function () {
this.websocketAddressManager = new this.WebsocketAddressManager(false)
})
it('should return the client ip address for a direct connection', function () {
expect(
this.websocketAddressManager.getRemoteIp({
headers: {},
address: { address: '123.45.67.89' },
})
).to.equal('123.45.67.89')
})
it('should return undefined if the client ip address is not present', function () {
expect(
this.websocketAddressManager.getRemoteIp({
headers: {},
address: { otherAddressProperty: '123.45.67.89' },
})
).to.be.undefined
})
it('should return the proxy ip address if there is actually a proxy', function () {
expect(
this.websocketAddressManager.getRemoteIp({
headers: {
'x-forwarded-proto': 'https',
'x-forwarded-for': '123.45.67.89',
},
address: { address: '127.0.0.1' },
})
).to.equal('127.0.0.1')
})
it('should return a "client-handshake-missing" response when the handshake is missing', function () {
expect(this.websocketAddressManager.getRemoteIp()).to.equal(
'client-handshake-missing'
)
})
})
})