1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
9 alias Pleroma.HTTP.Request
10 alias Pleroma.HTTP.RequestBuilder
12 describe "headers/2" do
13 setup do: clear_config([:http, :send_user_agent])
14 setup do: clear_config([:http, :user_agent])
16 test "don't send pleroma user agent" do
17 assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}
20 test "send pleroma user agent" do
21 Config.put([:http, :send_user_agent], true)
22 Config.put([:http, :user_agent], :default)
24 assert RequestBuilder.headers(%Request{}, []) == %Request{
25 headers: [{"user-agent", Pleroma.Application.user_agent()}]
29 test "send custom user agent" do
30 Config.put([:http, :send_user_agent], true)
31 Config.put([:http, :user_agent], "totally-not-pleroma")
33 assert RequestBuilder.headers(%Request{}, []) == %Request{
34 headers: [{"user-agent", "totally-not-pleroma"}]
39 describe "add_param/4" do
40 test "add file parameter" do
42 body: %Tesla.Multipart{
44 content_type_params: [],
46 %Tesla.Multipart.Part{
49 modes: [:raw, :read_ahead, :read, :binary],
50 path: "some-path/filename.png",
53 dispositions: [name: "filename.png", filename: "filename.png"],
58 } = RequestBuilder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png")
61 test "add key to body" do
63 body: %Tesla.Multipart{
65 content_type_params: [],
67 %Tesla.Multipart.Part{
69 dispositions: [name: "somekey"],
70 headers: [{"content-type", "application/json"}]
74 } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
77 test "add form parameter" do
78 assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
79 body: %{"somename" => "someval"}
83 test "add for location" do
84 assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
85 some_location: [{"somekey", "someval"}]