WebsocketAddressManagerTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const modulePath = require('path').join(
  4. __dirname,
  5. '../../../app/js/WebsocketAddressManager'
  6. )
  7. describe('WebsocketAddressManager', function () {
  8. beforeEach(function () {
  9. this.WebsocketAddressManager = SandboxedModule.require(modulePath, {
  10. requires: {},
  11. })
  12. })
  13. describe('with a proxy configuration', function () {
  14. beforeEach(function () {
  15. this.websocketAddressManager = new this.WebsocketAddressManager(
  16. true,
  17. '127.0.0.1'
  18. )
  19. })
  20. it('should return the client ip address when behind a proxy', function () {
  21. expect(
  22. this.websocketAddressManager.getRemoteIp({
  23. headers: {
  24. 'x-forwarded-proto': 'https',
  25. 'x-forwarded-for': '123.45.67.89',
  26. },
  27. address: { address: '127.0.0.1' },
  28. })
  29. ).to.equal('123.45.67.89')
  30. })
  31. it('should return the client ip address for a direct connection', function () {
  32. expect(
  33. this.websocketAddressManager.getRemoteIp({
  34. headers: {},
  35. address: { address: '123.45.67.89' },
  36. })
  37. ).to.equal('123.45.67.89')
  38. })
  39. it('should return the client ip address when there are no headers in the handshake', function () {
  40. expect(
  41. this.websocketAddressManager.getRemoteIp({
  42. address: { address: '123.45.67.89' },
  43. })
  44. ).to.equal('123.45.67.89')
  45. })
  46. it('should return a "client-handshake-missing" response when the handshake is missing', function () {
  47. expect(this.websocketAddressManager.getRemoteIp()).to.equal(
  48. 'client-handshake-missing'
  49. )
  50. })
  51. })
  52. describe('without a proxy configuration', function () {
  53. beforeEach(function () {
  54. this.websocketAddressManager = new this.WebsocketAddressManager(false)
  55. })
  56. it('should return the client ip address for a direct connection', function () {
  57. expect(
  58. this.websocketAddressManager.getRemoteIp({
  59. headers: {},
  60. address: { address: '123.45.67.89' },
  61. })
  62. ).to.equal('123.45.67.89')
  63. })
  64. it('should return undefined if the client ip address is not present', function () {
  65. expect(
  66. this.websocketAddressManager.getRemoteIp({
  67. headers: {},
  68. address: { otherAddressProperty: '123.45.67.89' },
  69. })
  70. ).to.be.undefined
  71. })
  72. it('should return the proxy ip address if there is actually a proxy', function () {
  73. expect(
  74. this.websocketAddressManager.getRemoteIp({
  75. headers: {
  76. 'x-forwarded-proto': 'https',
  77. 'x-forwarded-for': '123.45.67.89',
  78. },
  79. address: { address: '127.0.0.1' },
  80. })
  81. ).to.equal('127.0.0.1')
  82. })
  83. it('should return a "client-handshake-missing" response when the handshake is missing', function () {
  84. expect(this.websocketAddressManager.getRemoteIp()).to.equal(
  85. 'client-handshake-missing'
  86. )
  87. })
  88. })
  89. })