1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.FedSockets.SocketInfoTest do
8 alias Pleroma.Web.FedSockets
9 alias Pleroma.Web.FedSockets.SocketInfo
11 describe "uri_for_origin" do
12 test "provides the fed_socket URL given the origin information" do
13 endpoint = "example.com:4000"
14 assert FedSockets.uri_for_origin(endpoint) =~ "ws://"
15 assert FedSockets.uri_for_origin(endpoint) =~ endpoint
20 test "will provide the origin field given a url" do
21 endpoint = "example.com:4000"
22 assert SocketInfo.origin("ws://#{endpoint}") == endpoint
23 assert SocketInfo.origin("http://#{endpoint}") == endpoint
24 assert SocketInfo.origin("https://#{endpoint}") == endpoint
27 test "will proide the origin field given a uri" do
28 endpoint = "example.com:4000"
29 uri = URI.parse("http://#{endpoint}")
31 assert SocketInfo.origin(uri) == endpoint
36 test "will update the TTL" do
37 endpoint = "example.com:4000"
38 socket = SocketInfo.build("ws://#{endpoint}")
40 touched_socket = SocketInfo.touch(socket)
42 assert socket.connected_until < touched_socket.connected_until
46 describe "expired?" do
49 {Pleroma.Web.FedSockets.Supervisor,
52 connection_duration: 5,
53 rejection_duration: 5,
54 fed_socket_rejections: [lazy: true]
61 test "tests if the TTL is exceeded" do
62 endpoint = "example.com:4000"
63 socket = SocketInfo.build("ws://#{endpoint}")
64 refute SocketInfo.expired?(socket)
67 assert SocketInfo.expired?(socket)
71 describe "creating outgoing connection records" do
72 test "can be passed a string" do
73 assert %{conn_pid: :pid, origin: _origin} = SocketInfo.build("example.com:4000", :pid)
76 test "can be passed a URI" do
77 uri = URI.parse("http://example.com:4000")
78 assert %{conn_pid: :pid, origin: origin} = SocketInfo.build(uri, :pid)
79 assert origin =~ "example.com:4000"
82 test "will include the port number" do
83 assert %{conn_pid: :pid, origin: origin} = SocketInfo.build("http://example.com:4000", :pid)
85 assert origin =~ ":4000"
88 test "will provide the port if missing" do
89 assert %{conn_pid: :pid, origin: "example.com:80"} =
90 SocketInfo.build("http://example.com", :pid)
92 assert %{conn_pid: :pid, origin: "example.com:443"} =
93 SocketInfo.build("https://example.com", :pid)
97 describe "creating incoming connection records" do
98 test "can be passed a string" do
99 assert %{pid: _, origin: _origin} = SocketInfo.build("example.com:4000")
102 test "can be passed a URI" do
103 uri = URI.parse("example.com:4000")
104 assert %{pid: _, origin: _origin} = SocketInfo.build(uri)
107 test "will include the port number" do
108 assert %{pid: _, origin: origin} = SocketInfo.build("http://example.com:4000")
110 assert origin =~ ":4000"
113 test "will provide the port if missing" do
114 assert %{pid: _, origin: "example.com:80"} = SocketInfo.build("http://example.com")
115 assert %{pid: _, origin: "example.com:443"} = SocketInfo.build("https://example.com")