Fix digest mix task on OTP releases
[akkoma] / test / http / request_builder_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.RequestBuilderTest do
6 use ExUnit.Case, async: true
7 use Pleroma.Tests.Helpers
8 alias Pleroma.Config
9 alias Pleroma.HTTP.Request
10 alias Pleroma.HTTP.RequestBuilder
11
12 describe "headers/2" do
13 setup do: clear_config([:http, :send_user_agent])
14 setup do: clear_config([:http, :user_agent])
15
16 test "don't send pleroma user agent" do
17 assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}
18 end
19
20 test "send pleroma user agent" do
21 Config.put([:http, :send_user_agent], true)
22 Config.put([:http, :user_agent], :default)
23
24 assert RequestBuilder.headers(%Request{}, []) == %Request{
25 headers: [{"user-agent", Pleroma.Application.user_agent()}]
26 }
27 end
28
29 test "send custom user agent" do
30 Config.put([:http, :send_user_agent], true)
31 Config.put([:http, :user_agent], "totally-not-pleroma")
32
33 assert RequestBuilder.headers(%Request{}, []) == %Request{
34 headers: [{"user-agent", "totally-not-pleroma"}]
35 }
36 end
37 end
38
39 describe "add_param/4" do
40 test "add file parameter" do
41 %Request{
42 body: %Tesla.Multipart{
43 boundary: _,
44 content_type_params: [],
45 parts: [
46 %Tesla.Multipart.Part{
47 body: %File.Stream{
48 line_or_bytes: 2048,
49 modes: [:raw, :read_ahead, :read, :binary],
50 path: "some-path/filename.png",
51 raw: true
52 },
53 dispositions: [name: "filename.png", filename: "filename.png"],
54 headers: []
55 }
56 ]
57 }
58 } = RequestBuilder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png")
59 end
60
61 test "add key to body" do
62 %{
63 body: %Tesla.Multipart{
64 boundary: _,
65 content_type_params: [],
66 parts: [
67 %Tesla.Multipart.Part{
68 body: "\"someval\"",
69 dispositions: [name: "somekey"],
70 headers: [{"content-type", "application/json"}]
71 }
72 ]
73 }
74 } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
75 end
76
77 test "add form parameter" do
78 assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
79 body: %{"somename" => "someval"}
80 }
81 end
82
83 test "add for location" do
84 assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
85 some_location: [{"somekey", "someval"}]
86 }
87 end
88 end
89 end