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
9 alias Pleroma.HTTP.Request
10 alias Pleroma.HTTP.RequestBuilder
12 describe "headers/2" do
13 clear_config([:http, :send_user_agent])
15 test "don't send pleroma user agent" do
16 assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}
19 test "send pleroma user agent" do
20 Config.put([:http, :send_user_agent], true)
21 Config.put([:http, :user_agent], :default)
23 assert RequestBuilder.headers(%Request{}, []) == %Request{
24 headers: [{"user-agent", Pleroma.Application.user_agent()}]
28 test "send custom user agent" do
29 Config.put([:http, :send_user_agent], true)
30 Config.put([:http, :user_agent], "totally-not-pleroma")
32 assert RequestBuilder.headers(%Request{}, []) == %Request{
33 headers: [{"user-agent", "totally-not-pleroma"}]
38 describe "add_optional_params/3" do
39 test "don't add if keyword is empty" do
40 assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{}
43 test "add query parameter" do
44 assert RequestBuilder.add_optional_params(
46 %{query: :query, body: :body, another: :val},
48 {:query, "param1=val1¶m2=val2"},
51 ) == %Request{query: "param1=val1¶m2=val2", body: "some body"}
55 describe "add_param/4" do
56 test "add file parameter" do
58 body: %Tesla.Multipart{
60 content_type_params: [],
62 %Tesla.Multipart.Part{
65 modes: [:raw, :read_ahead, :read, :binary],
66 path: "some-path/filename.png",
69 dispositions: [name: "filename.png", filename: "filename.png"],
74 } = RequestBuilder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png")
77 test "add key to body" do
79 body: %Tesla.Multipart{
81 content_type_params: [],
83 %Tesla.Multipart.Part{
85 dispositions: [name: "somekey"],
86 headers: [{"content-type", "application/json"}]
90 } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
93 test "add form parameter" do
94 assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
95 body: %{"somename" => "someval"}
99 test "add for location" do
100 assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
101 some_location: [{"somekey", "someval"}]