WebsocketAddressManager.test.js 3.0 KB

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