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 clear_config([:http, :send_user_agent])
14 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_optional_params/3" do
40 test "don't add if keyword is empty" do
41 assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{}
44 test "add query parameter" do
45 assert RequestBuilder.add_optional_params(
47 %{query: :query, body: :body, another: :val},
49 {:query, "param1=val1¶m2=val2"},
52 ) == %Request{query: "param1=val1¶m2=val2", body: "some body"}
56 describe "add_param/4" do
57 test "add file parameter" do
59 body: %Tesla.Multipart{
61 content_type_params: [],
63 %Tesla.Multipart.Part{
66 modes: [:raw, :read_ahead, :read, :binary],
67 path: "some-path/filename.png",
70 dispositions: [name: "filename.png", filename: "filename.png"],
75 } = RequestBuilder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png")
78 test "add key to body" do
80 body: %Tesla.Multipart{
82 content_type_params: [],
84 %Tesla.Multipart.Part{
86 dispositions: [name: "somekey"],
87 headers: [{"content-type", "application/json"}]
91 } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
94 test "add form parameter" do
95 assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
96 body: %{"somename" => "someval"}
100 test "add for location" do
101 assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
102 some_location: [{"somekey", "someval"}]