2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-07-09 10:54:13 -06:00
|
|
|
defmodule Pleroma.HTTP.RequestBuilderTest do
|
2020-05-11 06:07:05 -06:00
|
|
|
use ExUnit.Case
|
2019-08-19 09:34:29 -06:00
|
|
|
use Pleroma.Tests.Helpers
|
2020-02-11 00:12:57 -07:00
|
|
|
alias Pleroma.HTTP.Request
|
2019-07-09 10:54:13 -06:00
|
|
|
alias Pleroma.HTTP.RequestBuilder
|
|
|
|
|
|
|
|
describe "headers/2" do
|
|
|
|
test "don't send pleroma user agent" do
|
2020-02-11 00:12:57 -07:00
|
|
|
assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}
|
2019-07-09 10:54:13 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "send pleroma user agent" do
|
2020-05-11 06:07:05 -06:00
|
|
|
clear_config([:http, :send_user_agent], true)
|
|
|
|
clear_config([:http, :user_agent], :default)
|
2019-07-09 10:54:13 -06:00
|
|
|
|
2020-02-11 00:12:57 -07:00
|
|
|
assert RequestBuilder.headers(%Request{}, []) == %Request{
|
|
|
|
headers: [{"user-agent", Pleroma.Application.user_agent()}]
|
2019-07-09 10:54:13 -06:00
|
|
|
}
|
|
|
|
end
|
2019-11-25 02:53:11 -07:00
|
|
|
|
|
|
|
test "send custom user agent" do
|
2020-05-11 06:07:05 -06:00
|
|
|
clear_config([:http, :send_user_agent], true)
|
|
|
|
clear_config([:http, :user_agent], "totally-not-pleroma")
|
2019-11-25 02:53:11 -07:00
|
|
|
|
2020-02-11 00:12:57 -07:00
|
|
|
assert RequestBuilder.headers(%Request{}, []) == %Request{
|
|
|
|
headers: [{"user-agent", "totally-not-pleroma"}]
|
2019-11-25 02:53:11 -07:00
|
|
|
}
|
|
|
|
end
|
2019-07-09 10:54:13 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "add_param/4" do
|
|
|
|
test "add file parameter" do
|
2021-05-31 02:14:12 -06:00
|
|
|
assert match?(
|
|
|
|
%Request{
|
|
|
|
body: %Tesla.Multipart{
|
|
|
|
boundary: _,
|
|
|
|
content_type_params: [],
|
|
|
|
parts: [
|
|
|
|
%Tesla.Multipart.Part{
|
|
|
|
body: %File.Stream{
|
|
|
|
line_or_bytes: 2048,
|
|
|
|
modes: [:raw, :read_ahead, :binary],
|
|
|
|
path: "some-path/filename.png",
|
|
|
|
raw: true
|
|
|
|
},
|
|
|
|
dispositions: [name: "filename.png", filename: "filename.png"],
|
|
|
|
headers: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RequestBuilder.add_param(
|
|
|
|
%Request{},
|
|
|
|
:file,
|
|
|
|
"filename.png",
|
|
|
|
"some-path/filename.png"
|
|
|
|
)
|
|
|
|
)
|
2019-07-09 10:54:13 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "add key to body" do
|
|
|
|
%{
|
|
|
|
body: %Tesla.Multipart{
|
|
|
|
boundary: _,
|
|
|
|
content_type_params: [],
|
|
|
|
parts: [
|
|
|
|
%Tesla.Multipart.Part{
|
|
|
|
body: "\"someval\"",
|
|
|
|
dispositions: [name: "somekey"],
|
2020-02-11 00:12:57 -07:00
|
|
|
headers: [{"content-type", "application/json"}]
|
2019-07-09 10:54:13 -06:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
} = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add form parameter" do
|
|
|
|
assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
|
|
|
|
body: %{"somename" => "someval"}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add for location" do
|
|
|
|
assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
|
|
|
|
some_location: [{"somekey", "someval"}]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|