valid.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { wsValid, httpValid, socketioValid } from "../valid"
  2. describe("wsValid", () => {
  3. test("returns true for valid URL with IP address", () => {
  4. expect(wsValid("wss://174.129.224.73/")).toBe(true)
  5. expect(wsValid("wss://174.129.224.73")).toBe(true)
  6. })
  7. test("returns true for valid URL with Hostname", () => {
  8. expect(wsValid("wss://hoppscotch-websocket.herokuapp.com/")).toBe(true)
  9. expect(wsValid("wss://hoppscotch-websocket.herokuapp.com")).toBe(true)
  10. })
  11. test("returns false for invalid URL with IP address", () => {
  12. expect(wsValid("wss://174.129.")).toBe(false)
  13. expect(wsValid("wss://174.129./")).toBe(false)
  14. })
  15. test("returns false for invalid URL with hostname", () => {
  16. expect(wsValid("wss://echo.websocket./")).toBe(false)
  17. expect(wsValid("wss://echo.websocket.")).toBe(false)
  18. })
  19. test("returns false for non-wss protocol URLs", () => {
  20. expect(wsValid("http://echo.websocket.org/")).toBe(false)
  21. expect(wsValid("http://echo.websocket.org")).toBe(false)
  22. expect(wsValid("http://174.129.224.73/")).toBe(false)
  23. expect(wsValid("http://174.129.224.73")).toBe(false)
  24. })
  25. test("returns true for wss protocol URLs", () => {
  26. expect(wsValid("wss://hoppscotch-websocket.herokuapp.com/")).toBe(true)
  27. expect(wsValid("wss://hoppscotch-websocket.herokuapp.com")).toBe(true)
  28. expect(wsValid("wss://174.129.224.73/")).toBe(true)
  29. expect(wsValid("wss://174.129.224.73")).toBe(true)
  30. })
  31. test("returns true for ws protocol URLs", () => {
  32. expect(wsValid("ws://echo.websocket.org/")).toBe(true)
  33. expect(wsValid("ws://echo.websocket.org")).toBe(true)
  34. expect(wsValid("ws://174.129.224.73/")).toBe(true)
  35. expect(wsValid("ws://174.129.224.73")).toBe(true)
  36. })
  37. })
  38. describe("httpValid", () => {
  39. test("returns true for valid URL with IP address", () => {
  40. expect(httpValid("http://174.129.224.73/")).toBe(true)
  41. expect(httpValid("http://174.129.224.73")).toBe(true)
  42. })
  43. test("returns true for valid URL with Hostname", () => {
  44. expect(httpValid("http://echo.websocket.org/")).toBe(true)
  45. expect(httpValid("http://echo.websocket.org")).toBe(true)
  46. })
  47. test("returns false for invalid URL with IP address", () => {
  48. expect(httpValid("http://174.129./")).toBe(false)
  49. expect(httpValid("http://174.129.")).toBe(false)
  50. })
  51. test("returns false for invalid URL with hostname", () => {
  52. expect(httpValid("http://echo.websocket./")).toBe(false)
  53. expect(httpValid("http://echo.websocket.")).toBe(false)
  54. })
  55. test("returns false for non-http(s) protocol URLs", () => {
  56. expect(httpValid("wss://hoppscotch-websocket.herokuapp.com/")).toBe(false)
  57. expect(httpValid("wss://hoppscotch-websocket.herokuapp.com")).toBe(false)
  58. expect(httpValid("wss://174.129.224.73/")).toBe(false)
  59. expect(httpValid("wss://174.129.224.73")).toBe(false)
  60. })
  61. test("returns true for HTTP protocol URLs", () => {
  62. expect(httpValid("http://echo.websocket.org/")).toBe(true)
  63. expect(httpValid("http://echo.websocket.org")).toBe(true)
  64. expect(httpValid("http://174.129.224.73/")).toBe(true)
  65. expect(httpValid("http://174.129.224.73")).toBe(true)
  66. })
  67. test("returns true for HTTPS protocol URLs", () => {
  68. expect(httpValid("https://echo.websocket.org/")).toBe(true)
  69. expect(httpValid("https://echo.websocket.org")).toBe(true)
  70. expect(httpValid("https://174.129.224.73/")).toBe(true)
  71. expect(httpValid("https://174.129.224.73")).toBe(true)
  72. })
  73. })
  74. describe("socketioValid", () => {
  75. test("returns true for valid URL with IP address", () => {
  76. expect(socketioValid("http://174.129.224.73/")).toBe(true)
  77. expect(socketioValid("http://174.129.224.73")).toBe(true)
  78. })
  79. test("returns true for valid URL with Hostname", () => {
  80. expect(socketioValid("http://echo.websocket.org/")).toBe(true)
  81. expect(socketioValid("http://echo.websocket.org")).toBe(true)
  82. })
  83. test("returns false for invalid URL with IP address", () => {
  84. expect(socketioValid("http://174.129./")).toBe(false)
  85. expect(socketioValid("http://174.129.")).toBe(false)
  86. })
  87. test("returns false for invalid URL with hostname", () => {
  88. expect(socketioValid("http://echo.websocket./")).toBe(false)
  89. expect(socketioValid("http://echo.websocket.")).toBe(false)
  90. })
  91. test("returns false for non-http(s) and non-wss protocol URLs", () => {
  92. expect(socketioValid("ftp://echo.websocket.org/")).toBe(false)
  93. expect(socketioValid("ftp://echo.websocket.org")).toBe(false)
  94. expect(socketioValid("ftp://174.129.224.73/")).toBe(false)
  95. expect(socketioValid("ftp://174.129.224.73")).toBe(false)
  96. })
  97. test("returns true for HTTP protocol URLs", () => {
  98. expect(socketioValid("http://echo.websocket.org/")).toBe(true)
  99. expect(socketioValid("http://echo.websocket.org")).toBe(true)
  100. expect(socketioValid("http://174.129.224.73/")).toBe(true)
  101. expect(socketioValid("http://174.129.224.73")).toBe(true)
  102. })
  103. test("returns true for HTTPS protocol URLs", () => {
  104. expect(socketioValid("https://echo.websocket.org/")).toBe(true)
  105. expect(socketioValid("https://echo.websocket.org")).toBe(true)
  106. expect(socketioValid("https://174.129.224.73/")).toBe(true)
  107. expect(socketioValid("https://174.129.224.73")).toBe(true)
  108. })
  109. test("returns true for wss protocol URLs", () => {
  110. expect(socketioValid("wss://hoppscotch-websocket.herokuapp.com/")).toBe(
  111. true
  112. )
  113. expect(socketioValid("wss://hoppscotch-websocket.herokuapp.com")).toBe(true)
  114. expect(socketioValid("wss://174.129.224.73/")).toBe(true)
  115. expect(socketioValid("wss://174.129.224.73")).toBe(true)
  116. })
  117. test("returns true for ws protocol URLs", () => {
  118. expect(socketioValid("ws://echo.websocket.org/")).toBe(true)
  119. expect(socketioValid("ws://echo.websocket.org")).toBe(true)
  120. expect(socketioValid("ws://174.129.224.73/")).toBe(true)
  121. expect(socketioValid("ws://174.129.224.73")).toBe(true)
  122. })
  123. })