logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

socket_info_test.exs (3581B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.FedSockets.SocketInfoTest do
  5. use ExUnit.Case
  6. alias Pleroma.Web.FedSockets
  7. alias Pleroma.Web.FedSockets.SocketInfo
  8. describe "uri_for_origin" do
  9. test "provides the fed_socket URL given the origin information" do
  10. endpoint = "example.com:4000"
  11. assert FedSockets.uri_for_origin(endpoint) =~ "ws://"
  12. assert FedSockets.uri_for_origin(endpoint) =~ endpoint
  13. end
  14. end
  15. describe "origin" do
  16. test "will provide the origin field given a url" do
  17. endpoint = "example.com:4000"
  18. assert SocketInfo.origin("ws://#{endpoint}") == endpoint
  19. assert SocketInfo.origin("http://#{endpoint}") == endpoint
  20. assert SocketInfo.origin("https://#{endpoint}") == endpoint
  21. end
  22. test "will proide the origin field given a uri" do
  23. endpoint = "example.com:4000"
  24. uri = URI.parse("http://#{endpoint}")
  25. assert SocketInfo.origin(uri) == endpoint
  26. end
  27. end
  28. describe "touch" do
  29. test "will update the TTL" do
  30. endpoint = "example.com:4000"
  31. socket = SocketInfo.build("ws://#{endpoint}")
  32. Process.sleep(2)
  33. touched_socket = SocketInfo.touch(socket)
  34. assert socket.connected_until < touched_socket.connected_until
  35. end
  36. end
  37. describe "expired?" do
  38. setup do
  39. start_supervised(
  40. {Pleroma.Web.FedSockets.Supervisor,
  41. [
  42. ping_interval: 8,
  43. connection_duration: 5,
  44. rejection_duration: 5,
  45. fed_socket_rejections: [lazy: true]
  46. ]}
  47. )
  48. :ok
  49. end
  50. test "tests if the TTL is exceeded" do
  51. endpoint = "example.com:4000"
  52. socket = SocketInfo.build("ws://#{endpoint}")
  53. refute SocketInfo.expired?(socket)
  54. Process.sleep(10)
  55. assert SocketInfo.expired?(socket)
  56. end
  57. end
  58. describe "creating outgoing connection records" do
  59. test "can be passed a string" do
  60. assert %{conn_pid: :pid, origin: _origin} = SocketInfo.build("example.com:4000", :pid)
  61. end
  62. test "can be passed a URI" do
  63. uri = URI.parse("http://example.com:4000")
  64. assert %{conn_pid: :pid, origin: origin} = SocketInfo.build(uri, :pid)
  65. assert origin =~ "example.com:4000"
  66. end
  67. test "will include the port number" do
  68. assert %{conn_pid: :pid, origin: origin} = SocketInfo.build("http://example.com:4000", :pid)
  69. assert origin =~ ":4000"
  70. end
  71. test "will provide the port if missing" do
  72. assert %{conn_pid: :pid, origin: "example.com:80"} =
  73. SocketInfo.build("http://example.com", :pid)
  74. assert %{conn_pid: :pid, origin: "example.com:443"} =
  75. SocketInfo.build("https://example.com", :pid)
  76. end
  77. end
  78. describe "creating incoming connection records" do
  79. test "can be passed a string" do
  80. assert %{pid: _, origin: _origin} = SocketInfo.build("example.com:4000")
  81. end
  82. test "can be passed a URI" do
  83. uri = URI.parse("example.com:4000")
  84. assert %{pid: _, origin: _origin} = SocketInfo.build(uri)
  85. end
  86. test "will include the port number" do
  87. assert %{pid: _, origin: origin} = SocketInfo.build("http://example.com:4000")
  88. assert origin =~ ":4000"
  89. end
  90. test "will provide the port if missing" do
  91. assert %{pid: _, origin: "example.com:80"} = SocketInfo.build("http://example.com")
  92. assert %{pid: _, origin: "example.com:443"} = SocketInfo.build("https://example.com")
  93. end
  94. end
  95. end