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