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