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