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 use Pleroma.Tests.Helpers
8 alias Pleroma.HTTP.RequestBuilder
10 describe "headers/2" do
11 clear_config([:http, :send_user_agent])
13 test "don't send pleroma user agent" do
14 assert RequestBuilder.headers(%{}, []) == %{headers: []}
17 test "send pleroma user agent" do
18 Pleroma.Config.put([:http, :send_user_agent], true)
19 Pleroma.Config.put([:http, :user_agent], :default)
21 assert RequestBuilder.headers(%{}, []) == %{
22 headers: [{"User-Agent", Pleroma.Application.user_agent()}]
26 test "send custom user agent" do
27 Pleroma.Config.put([:http, :send_user_agent], true)
28 Pleroma.Config.put([:http, :user_agent], "totally-not-pleroma")
30 assert RequestBuilder.headers(%{}, []) == %{
31 headers: [{"User-Agent", "totally-not-pleroma"}]
36 describe "add_optional_params/3" do
37 test "don't add if keyword is empty" do
38 assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{}
41 test "add query parameter" do
42 assert RequestBuilder.add_optional_params(
44 %{query: :query, body: :body, another: :val},
46 {:query, "param1=val1¶m2=val2"},
49 ) == %{query: "param1=val1¶m2=val2", body: "some body"}
53 describe "add_param/4" do
54 test "add file parameter" do
56 body: %Tesla.Multipart{
58 content_type_params: [],
60 %Tesla.Multipart.Part{
63 modes: [:raw, :read_ahead, :read, :binary],
64 path: "some-path/filename.png",
67 dispositions: [name: "filename.png", filename: "filename.png"],
72 } = RequestBuilder.add_param(%{}, :file, "filename.png", "some-path/filename.png")
75 test "add key to body" do
77 body: %Tesla.Multipart{
79 content_type_params: [],
81 %Tesla.Multipart.Part{
83 dispositions: [name: "somekey"],
84 headers: ["Content-Type": "application/json"]
88 } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
91 test "add form parameter" do
92 assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
93 body: %{"somename" => "someval"}
97 test "add for location" do
98 assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
99 some_location: [{"somekey", "someval"}]