First
[anni] / test / pleroma / http / request_builder_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.RequestBuilderTest do
6   use ExUnit.Case
7   use Pleroma.Tests.Helpers
8   alias Pleroma.HTTP.Request
9   alias Pleroma.HTTP.RequestBuilder
10
11   describe "headers/2" do
12     test "don't send pleroma user agent" do
13       assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}
14     end
15
16     test "send pleroma user agent" do
17       clear_config([:http, :send_user_agent], true)
18       clear_config([:http, :user_agent], :default)
19
20       assert RequestBuilder.headers(%Request{}, []) == %Request{
21                headers: [{"user-agent", Pleroma.Application.user_agent()}]
22              }
23     end
24
25     test "send custom user agent" do
26       clear_config([:http, :send_user_agent], true)
27       clear_config([:http, :user_agent], "totally-not-pleroma")
28
29       assert RequestBuilder.headers(%Request{}, []) == %Request{
30                headers: [{"user-agent", "totally-not-pleroma"}]
31              }
32     end
33   end
34
35   describe "add_param/4" do
36     test "add file parameter" do
37       assert match?(
38                %Request{
39                  body: %Tesla.Multipart{
40                    boundary: _,
41                    content_type_params: [],
42                    parts: [
43                      %Tesla.Multipart.Part{
44                        body: %File.Stream{
45                          line_or_bytes: 2048,
46                          modes: [:raw, :read_ahead, :binary],
47                          path: "some-path/filename.png",
48                          raw: true
49                        },
50                        dispositions: [name: "filename.png", filename: "filename.png"],
51                        headers: []
52                      }
53                    ]
54                  }
55                },
56                RequestBuilder.add_param(
57                  %Request{},
58                  :file,
59                  "filename.png",
60                  "some-path/filename.png"
61                )
62              )
63     end
64
65     test "add key to body" do
66       %{
67         body: %Tesla.Multipart{
68           boundary: _,
69           content_type_params: [],
70           parts: [
71             %Tesla.Multipart.Part{
72               body: "\"someval\"",
73               dispositions: [name: "somekey"],
74               headers: [{"content-type", "application/json"}]
75             }
76           ]
77         }
78       } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
79     end
80
81     test "add form parameter" do
82       assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
83                body: %{"somename" => "someval"}
84              }
85     end
86
87     test "add for location" do
88       assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
89                some_location: [{"somekey", "someval"}]
90              }
91     end
92   end
93 end