1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Gun.API.Mock do
6 @behaviour Pleroma.Gun.API
11 def open('some-domain.com', 443, _) do
12 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
14 Registry.register(API.Mock, conn_pid, %{
15 origin_scheme: "https",
16 origin_host: 'some-domain.com',
25 when ip in [{10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}, {127, 0, 0, 1}] and
27 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
29 scheme = if port == 443, do: "https", else: "http"
31 Registry.register(API.Mock, conn_pid, %{
32 origin_scheme: scheme,
41 def open('localhost', 1234, %{
43 proxy: {:socks5, 'localhost', 1234},
44 socks_opts: %{host: 'proxy-socks.com', port: 80, version: 5}
46 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
48 Registry.register(API.Mock, conn_pid, %{
49 origin_scheme: "http",
50 origin_host: 'proxy-socks.com',
58 def open('localhost', 1234, %{
60 proxy: {:socks4, 'localhost', 1234},
62 host: 'proxy-socks.com',
70 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
72 Registry.register(API.Mock, conn_pid, %{
73 origin_scheme: "https",
74 origin_host: 'proxy-socks.com',
82 def open('gun-not-up.com', 80, _opts), do: {:error, :timeout}
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)
88 Registry.register(API.Mock, conn_pid, %{
89 origin_scheme: "https",
90 origin_host: 'example.com',
98 def open(domain, 80, _) do
99 {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
101 Registry.register(API.Mock, conn_pid, %{
102 origin_scheme: "http",
111 def open({127, 0, 0, 1}, 8123, _) do
112 Task.start_link(fn -> Process.sleep(1_000) end)
116 def open('localhost', 9050, _) do
117 Task.start_link(fn -> Process.sleep(1_000) end)
121 def await_up(_pid, _timeout), do: {:ok, :http}
124 def set_owner(_pid, _owner), do: :ok
127 def connect(pid, %{host: _, port: 80}) do
129 Registry.register(API.Mock, ref, pid)
134 def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do
136 Registry.register(API.Mock, ref, pid)
141 def await(pid, ref) do
142 [{_, ^pid}] = Registry.lookup(API.Mock, ref)
143 {:response, :fin, 200, []}
148 [{_, info}] = Registry.lookup(API.Mock, pid)
153 def close(_pid), do: :ok