1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP.RequestBuilderTest do
6 use ExUnit.Case, async: true
7 alias Pleroma.HTTP.RequestBuilder
9 describe "headers/2" do
10 test "don't send pleroma user agent" do
11 assert RequestBuilder.headers(%{}, []) == %{headers: []}
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)
19 Pleroma.Config.put([:http, :send_user_agent], send)
22 assert RequestBuilder.headers(%{}, []) == %{
23 headers: [{"User-Agent", Pleroma.Application.user_agent()}]
28 describe "add_optional_params/3" do
29 test "don't add if keyword is empty" do
30 assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{}
33 test "add query parameter" do
34 assert RequestBuilder.add_optional_params(
36 %{query: :query, body: :body, another: :val},
38 {:query, "param1=val1¶m2=val2"},
41 ) == %{query: "param1=val1¶m2=val2", body: "some body"}
45 describe "add_param/4" do
46 test "add file parameter" do
48 body: %Tesla.Multipart{
50 content_type_params: [],
52 %Tesla.Multipart.Part{
55 modes: [:raw, :read_ahead, :read, :binary],
56 path: "some-path/filename.png",
59 dispositions: [name: "filename.png", filename: "filename.png"],
64 } = RequestBuilder.add_param(%{}, :file, "filename.png", "some-path/filename.png")
67 test "add key to body" do
69 body: %Tesla.Multipart{
71 content_type_params: [],
73 %Tesla.Multipart.Part{
75 dispositions: [name: "somekey"],
76 headers: ["Content-Type": "application/json"]
80 } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
83 test "add form parameter" do
84 assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
85 body: %{"somename" => "someval"}
89 test "add for location" do
90 assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
91 some_location: [{"somekey", "someval"}]