summaryrefslogtreecommitdiff
path: root/test/pleroma/http/request_builder_test.exs
blob: 433beaac113f3e7c3fd87eba42f4931ec1cfa9b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.HTTP.RequestBuilderTest do
  use ExUnit.Case
  use Pleroma.Tests.Helpers
  alias Pleroma.HTTP.Request
  alias Pleroma.HTTP.RequestBuilder

  describe "headers/2" do
    test "don't send pleroma user agent" do
      assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}
    end

    test "send pleroma user agent" do
      clear_config([:http, :send_user_agent], true)
      clear_config([:http, :user_agent], :default)

      assert RequestBuilder.headers(%Request{}, []) == %Request{
               headers: [{"user-agent", Pleroma.Application.user_agent()}]
             }
    end

    test "send custom user agent" do
      clear_config([:http, :send_user_agent], true)
      clear_config([:http, :user_agent], "totally-not-pleroma")

      assert RequestBuilder.headers(%Request{}, []) == %Request{
               headers: [{"user-agent", "totally-not-pleroma"}]
             }
    end
  end

  describe "add_param/4" do
    test "add file parameter" do
      assert match?(
               %Request{
                 body: %Tesla.Multipart{
                   boundary: _,
                   content_type_params: [],
                   parts: [
                     %Tesla.Multipart.Part{
                       body: %File.Stream{
                         line_or_bytes: 2048,
                         modes: [:raw, :read_ahead, :binary],
                         path: "some-path/filename.png",
                         raw: true
                       },
                       dispositions: [name: "filename.png", filename: "filename.png"],
                       headers: []
                     }
                   ]
                 }
               },
               RequestBuilder.add_param(
                 %Request{},
                 :file,
                 "filename.png",
                 "some-path/filename.png"
               )
             )
    end

    test "add key to body" do
      %{
        body: %Tesla.Multipart{
          boundary: _,
          content_type_params: [],
          parts: [
            %Tesla.Multipart.Part{
              body: "\"someval\"",
              dispositions: [name: "somekey"],
              headers: [{"content-type", "application/json"}]
            }
          ]
        }
      } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
    end

    test "add form parameter" do
      assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
               body: %{"somename" => "someval"}
             }
    end

    test "add for location" do
      assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
               some_location: [{"somekey", "someval"}]
             }
    end
  end
end