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.

96 lines
2.8KB

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