Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.1KB

  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. defmodule Pleroma.HTTP.RequestBuilderTest do
  5. use ExUnit.Case, async: true
  6. use Pleroma.Tests.Helpers
  7. alias Pleroma.HTTP.RequestBuilder
  8. describe "headers/2" do
  9. setup do: clear_config([:http, :send_user_agent])
  10. setup do: clear_config([:http, :user_agent])
  11. test "don't send pleroma user agent" do
  12. assert RequestBuilder.headers(%{}, []) == %{headers: []}
  13. end
  14. test "send pleroma user agent" do
  15. Pleroma.Config.put([:http, :send_user_agent], true)
  16. Pleroma.Config.put([:http, :user_agent], :default)
  17. assert RequestBuilder.headers(%{}, []) == %{
  18. headers: [{"User-Agent", Pleroma.Application.user_agent()}]
  19. }
  20. end
  21. test "send custom user agent" do
  22. Pleroma.Config.put([:http, :send_user_agent], true)
  23. Pleroma.Config.put([:http, :user_agent], "totally-not-pleroma")
  24. assert RequestBuilder.headers(%{}, []) == %{
  25. headers: [{"User-Agent", "totally-not-pleroma"}]
  26. }
  27. end
  28. end
  29. describe "add_optional_params/3" do
  30. test "don't add if keyword is empty" do
  31. assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{}
  32. end
  33. test "add query parameter" do
  34. assert RequestBuilder.add_optional_params(
  35. %{},
  36. %{query: :query, body: :body, another: :val},
  37. [
  38. {:query, "param1=val1&param2=val2"},
  39. {:body, "some body"}
  40. ]
  41. ) == %{query: "param1=val1&param2=val2", body: "some body"}
  42. end
  43. end
  44. describe "add_param/4" do
  45. test "add file parameter" do
  46. %{
  47. body: %Tesla.Multipart{
  48. boundary: _,
  49. content_type_params: [],
  50. parts: [
  51. %Tesla.Multipart.Part{
  52. body: %File.Stream{
  53. line_or_bytes: 2048,
  54. modes: [:raw, :read_ahead, :read, :binary],
  55. path: "some-path/filename.png",
  56. raw: true
  57. },
  58. dispositions: [name: "filename.png", filename: "filename.png"],
  59. headers: []
  60. }
  61. ]
  62. }
  63. } = RequestBuilder.add_param(%{}, :file, "filename.png", "some-path/filename.png")
  64. end
  65. test "add key to body" do
  66. %{
  67. body: %Tesla.Multipart{
  68. boundary: _,
  69. content_type_params: [],
  70. parts: [
  71. %Tesla.Multipart.Part{
  72. body: "\"someval\"",
  73. dispositions: [name: "somekey"],
  74. headers: ["Content-Type": "application/json"]
  75. }
  76. ]
  77. }
  78. } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
  79. end
  80. test "add form parameter" do
  81. assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
  82. body: %{"somename" => "someval"}
  83. }
  84. end
  85. test "add for location" do
  86. assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
  87. some_location: [{"somekey", "someval"}]
  88. }
  89. end
  90. end
  91. end