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.GunMock do
12 def open('some-domain.com', 443, _) do
13 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
15 Registry.register(GunMock, conn_pid, %{
16 origin_scheme: "https",
17 origin_host: 'some-domain.com',
26 when ip in [{10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}, {127, 0, 0, 1}] and
28 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
30 scheme = if port == 443, do: "https", else: "http"
32 Registry.register(GunMock, conn_pid, %{
33 origin_scheme: scheme,
42 def open('localhost', 1234, %{
44 proxy: {:socks5, 'localhost', 1234},
45 socks_opts: %{host: 'proxy-socks.com', port: 80, version: 5}
47 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
49 Registry.register(GunMock, conn_pid, %{
50 origin_scheme: "http",
51 origin_host: 'proxy-socks.com',
59 def open('localhost', 1234, %{
61 proxy: {:socks4, 'localhost', 1234},
63 host: 'proxy-socks.com',
71 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
73 Registry.register(GunMock, conn_pid, %{
74 origin_scheme: "https",
75 origin_host: 'proxy-socks.com',
83 def open('gun-not-up.com', 80, _opts), do: {:error, :timeout}
86 def open('example.com', port, _) when port in [443, 115] do
87 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
89 Registry.register(GunMock, conn_pid, %{
90 origin_scheme: "https",
91 origin_host: 'example.com',
99 def open(domain, 80, _) do
100 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
102 Registry.register(GunMock, conn_pid, %{
103 origin_scheme: "http",
112 def open({127, 0, 0, 1}, 8123, _) do
113 Task.start_link(fn -> Process.sleep(1_000) end)
117 def open('localhost', 9050, _) do
118 Task.start_link(fn -> Process.sleep(1_000) end)
122 def await_up(_pid, _timeout), do: {:ok, :http}
125 def set_owner(_pid, _owner), do: :ok
128 def connect(pid, %{host: _, port: 80}) do
130 Registry.register(GunMock, ref, pid)
135 def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do
137 Registry.register(GunMock, ref, pid)
142 def await(pid, ref) do
143 [{_, ^pid}] = Registry.lookup(GunMock, ref)
144 {:response, :fin, 200, []}
149 [{_, info}] = Registry.lookup(GunMock, pid)
154 def close(_pid), do: :ok