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.

94 lines
2.7KB

  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. use Pleroma.Tests.Helpers
  7. alias Pleroma.HTTP.RequestBuilder
  8. describe "headers/2" do
  9. clear_config([:http, :send_user_agent])
  10. test "don't send pleroma user agent" do
  11. assert RequestBuilder.headers(%{}, []) == %{headers: []}
  12. end
  13. test "send pleroma user agent" do
  14. Pleroma.Config.put([:http, :send_user_agent], true)
  15. assert RequestBuilder.headers(%{}, []) == %{
  16. headers: [{"User-Agent", Pleroma.Application.user_agent()}]
  17. }
  18. end
  19. end
  20. describe "add_optional_params/3" do
  21. test "don't add if keyword is empty" do
  22. assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{}
  23. end
  24. test "add query parameter" do
  25. assert RequestBuilder.add_optional_params(
  26. %{},
  27. %{query: :query, body: :body, another: :val},
  28. [
  29. {:query, "param1=val1&param2=val2"},
  30. {:body, "some body"}
  31. ]
  32. ) == %{query: "param1=val1&param2=val2", body: "some body"}
  33. end
  34. end
  35. describe "add_param/4" do
  36. test "add file parameter" do
  37. %{
  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(%{}, :file, "filename.png", "some-path/filename.png")
  55. end
  56. test "add key to body" do
  57. %{
  58. body: %Tesla.Multipart{
  59. boundary: _,
  60. content_type_params: [],
  61. parts: [
  62. %Tesla.Multipart.Part{
  63. body: "\"someval\"",
  64. dispositions: [name: "somekey"],
  65. headers: ["Content-Type": "application/json"]
  66. }
  67. ]
  68. }
  69. } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
  70. end
  71. test "add form parameter" do
  72. assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
  73. body: %{"somename" => "someval"}
  74. }
  75. end
  76. test "add for location" do
  77. assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
  78. some_location: [{"somekey", "someval"}]
  79. }
  80. end
  81. end
  82. end