don't test gun itself
[akkoma] / lib / pleroma / gun / api / mock.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Gun.API.Mock do
6 @behaviour Pleroma.Gun.API
7
8 alias Pleroma.Gun.API
9
10 @impl API
11 def open('some-domain.com', 443, _) do
12 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
13
14 Registry.register(API.Mock, conn_pid, %{
15 origin_scheme: "https",
16 origin_host: 'some-domain.com',
17 origin_port: 443
18 })
19
20 {:ok, conn_pid}
21 end
22
23 @impl API
24 def open(ip, port, _)
25 when ip in [{10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}, {127, 0, 0, 1}] and
26 port in [80, 443] do
27 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
28
29 scheme = if port == 443, do: "https", else: "http"
30
31 Registry.register(API.Mock, conn_pid, %{
32 origin_scheme: scheme,
33 origin_host: ip,
34 origin_port: port
35 })
36
37 {:ok, conn_pid}
38 end
39
40 @impl API
41 def open('localhost', 1234, %{
42 protocols: [:socks],
43 proxy: {:socks5, 'localhost', 1234},
44 socks_opts: %{host: 'proxy-socks.com', port: 80, version: 5}
45 }) do
46 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
47
48 Registry.register(API.Mock, conn_pid, %{
49 origin_scheme: "http",
50 origin_host: 'proxy-socks.com',
51 origin_port: 80
52 })
53
54 {:ok, conn_pid}
55 end
56
57 @impl API
58 def open('localhost', 1234, %{
59 protocols: [:socks],
60 proxy: {:socks4, 'localhost', 1234},
61 socks_opts: %{
62 host: 'proxy-socks.com',
63 port: 443,
64 protocols: [:http2],
65 tls_opts: [],
66 transport: :tls,
67 version: 4
68 }
69 }) do
70 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
71
72 Registry.register(API.Mock, conn_pid, %{
73 origin_scheme: "https",
74 origin_host: 'proxy-socks.com',
75 origin_port: 443
76 })
77
78 {:ok, conn_pid}
79 end
80
81 @impl API
82 def open('gun-not-up.com', 80, _opts), do: {:error, :timeout}
83
84 @impl API
85 def open('example.com', port, _) when port in [443, 115] do
86 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
87
88 Registry.register(API.Mock, conn_pid, %{
89 origin_scheme: "https",
90 origin_host: 'example.com',
91 origin_port: 443
92 })
93
94 {:ok, conn_pid}
95 end
96
97 @impl API
98 def open(domain, 80, _) do
99 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
100
101 Registry.register(API.Mock, conn_pid, %{
102 origin_scheme: "http",
103 origin_host: domain,
104 origin_port: 80
105 })
106
107 {:ok, conn_pid}
108 end
109
110 @impl API
111 def open({127, 0, 0, 1}, 8123, _) do
112 Task.start_link(fn -> Process.sleep(1_000) end)
113 end
114
115 @impl API
116 def open('localhost', 9050, _) do
117 Task.start_link(fn -> Process.sleep(1_000) end)
118 end
119
120 @impl API
121 def await_up(_pid, _timeout), do: {:ok, :http}
122
123 @impl API
124 def set_owner(_pid, _owner), do: :ok
125
126 @impl API
127 def connect(pid, %{host: _, port: 80}) do
128 ref = make_ref()
129 Registry.register(API.Mock, ref, pid)
130 ref
131 end
132
133 @impl API
134 def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do
135 ref = make_ref()
136 Registry.register(API.Mock, ref, pid)
137 ref
138 end
139
140 @impl API
141 def await(pid, ref) do
142 [{_, ^pid}] = Registry.lookup(API.Mock, ref)
143 {:response, :fin, 200, []}
144 end
145
146 @impl API
147 def info(pid) do
148 [{_, info}] = Registry.lookup(API.Mock, pid)
149 info
150 end
151
152 @impl API
153 def close(_pid), do: :ok
154 end