Merge branch 'develop' into issue/1276
[akkoma] / test / http / request_builder_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.HTTP.RequestBuilderTest do
6 use ExUnit.Case, async: true
7 use Pleroma.Tests.Helpers
8 alias Pleroma.HTTP.RequestBuilder
9
10 describe "headers/2" do
11 setup do: clear_config([:http, :send_user_agent])
12 setup do: clear_config([:http, :user_agent])
13
14 test "don't send pleroma user agent" do
15 assert RequestBuilder.headers(%{}, []) == %{headers: []}
16 end
17
18 test "send pleroma user agent" do
19 Pleroma.Config.put([:http, :send_user_agent], true)
20 Pleroma.Config.put([:http, :user_agent], :default)
21
22 assert RequestBuilder.headers(%{}, []) == %{
23 headers: [{"User-Agent", Pleroma.Application.user_agent()}]
24 }
25 end
26
27 test "send custom user agent" do
28 Pleroma.Config.put([:http, :send_user_agent], true)
29 Pleroma.Config.put([:http, :user_agent], "totally-not-pleroma")
30
31 assert RequestBuilder.headers(%{}, []) == %{
32 headers: [{"User-Agent", "totally-not-pleroma"}]
33 }
34 end
35 end
36
37 describe "add_optional_params/3" do
38 test "don't add if keyword is empty" do
39 assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{}
40 end
41
42 test "add query parameter" do
43 assert RequestBuilder.add_optional_params(
44 %{},
45 %{query: :query, body: :body, another: :val},
46 [
47 {:query, "param1=val1&param2=val2"},
48 {:body, "some body"}
49 ]
50 ) == %{query: "param1=val1&param2=val2", body: "some body"}
51 end
52 end
53
54 describe "add_param/4" do
55 test "add file parameter" do
56 %{
57 body: %Tesla.Multipart{
58 boundary: _,
59 content_type_params: [],
60 parts: [
61 %Tesla.Multipart.Part{
62 body: %File.Stream{
63 line_or_bytes: 2048,
64 modes: [:raw, :read_ahead, :read, :binary],
65 path: "some-path/filename.png",
66 raw: true
67 },
68 dispositions: [name: "filename.png", filename: "filename.png"],
69 headers: []
70 }
71 ]
72 }
73 } = RequestBuilder.add_param(%{}, :file, "filename.png", "some-path/filename.png")
74 end
75
76 test "add key to body" do
77 %{
78 body: %Tesla.Multipart{
79 boundary: _,
80 content_type_params: [],
81 parts: [
82 %Tesla.Multipart.Part{
83 body: "\"someval\"",
84 dispositions: [name: "somekey"],
85 headers: ["Content-Type": "application/json"]
86 }
87 ]
88 }
89 } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
90 end
91
92 test "add form parameter" do
93 assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
94 body: %{"somename" => "someval"}
95 }
96 end
97
98 test "add for location" do
99 assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
100 some_location: [{"somekey", "someval"}]
101 }
102 end
103 end
104 end