Merge branch 'develop' into gun
[akkoma] / test / http_test.exs
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
5 defmodule Pleroma.HTTPTest do
6 use ExUnit.Case
7 use Pleroma.Tests.Helpers
8 import Tesla.Mock
9 alias Pleroma.HTTP
10
11 setup do
12 mock(fn
13 %{
14 method: :get,
15 url: "http://example.com/hello",
16 headers: [{"content-type", "application/json"}]
17 } ->
18 json(%{"my" => "data"})
19
20 %{method: :get, url: "http://example.com/hello"} ->
21 %Tesla.Env{status: 200, body: "hello"}
22
23 %{method: :post, url: "http://example.com/world"} ->
24 %Tesla.Env{status: 200, body: "world"}
25 end)
26
27 :ok
28 end
29
30 describe "get/1" do
31 test "returns successfully result" do
32 assert HTTP.get("http://example.com/hello") == {
33 :ok,
34 %Tesla.Env{status: 200, body: "hello"}
35 }
36 end
37 end
38
39 describe "get/2 (with headers)" do
40 test "returns successfully result for json content-type" do
41 assert HTTP.get("http://example.com/hello", [{"content-type", "application/json"}]) ==
42 {
43 :ok,
44 %Tesla.Env{
45 status: 200,
46 body: "{\"my\":\"data\"}",
47 headers: [{"content-type", "application/json"}]
48 }
49 }
50 end
51 end
52
53 describe "post/2" do
54 test "returns successfully result" do
55 assert HTTP.post("http://example.com/world", "") == {
56 :ok,
57 %Tesla.Env{status: 200, body: "world"}
58 }
59 end
60 end
61
62 describe "connection pools" do
63 @describetag :integration
64 clear_config(Pleroma.Gun) do
65 Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API)
66 end
67
68 test "gun" do
69 adapter = Application.get_env(:tesla, :adapter)
70 Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun)
71
72 on_exit(fn ->
73 Application.put_env(:tesla, :adapter, adapter)
74 end)
75
76 options = [adapter: [pool: :federation]]
77
78 assert {:ok, resp} = HTTP.get("https://httpbin.org/user-agent", [], options)
79
80 assert resp.status == 200
81
82 state = Pleroma.Pool.Connections.get_state(:gun_connections)
83 assert state.conns["https:httpbin.org:443"]
84 end
85 end
86 end