fix
[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
20 assert RequestBuilder.headers(%{}, []) == %{
21 headers: [{"User-Agent", Pleroma.Application.user_agent()}]
22 }
23 end
24 end
25
26 describe "add_optional_params/3" do
27 test "don't add if keyword is empty" do
28 assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{}
29 end
30
31 test "add query parameter" do
32 assert RequestBuilder.add_optional_params(
33 %{},
34 %{query: :query, body: :body, another: :val},
35 [
36 {:query, "param1=val1&param2=val2"},
37 {:body, "some body"}
38 ]
39 ) == %{query: "param1=val1&param2=val2", body: "some body"}
40 end
41 end
42
43 describe "add_param/4" do
44 test "add file parameter" do
45 %{
46 body: %Tesla.Multipart{
47 boundary: _,
48 content_type_params: [],
49 parts: [
50 %Tesla.Multipart.Part{
51 body: %File.Stream{
52 line_or_bytes: 2048,
53 modes: [:raw, :read_ahead, :read, :binary],
54 path: "some-path/filename.png",
55 raw: true
56 },
57 dispositions: [name: "filename.png", filename: "filename.png"],
58 headers: []
59 }
60 ]
61 }
62 } = RequestBuilder.add_param(%{}, :file, "filename.png", "some-path/filename.png")
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