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