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