logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

request_builder_test.exs (2610B)


  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
  6. use Pleroma.Tests.Helpers
  7. alias Pleroma.HTTP.Request
  8. alias Pleroma.HTTP.RequestBuilder
  9. describe "headers/2" do
  10. test "don't send pleroma user agent" do
  11. assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}
  12. end
  13. test "send pleroma user agent" do
  14. clear_config([:http, :send_user_agent], true)
  15. clear_config([:http, :user_agent], :default)
  16. assert RequestBuilder.headers(%Request{}, []) == %Request{
  17. headers: [{"user-agent", Pleroma.Application.user_agent()}]
  18. }
  19. end
  20. test "send custom user agent" do
  21. clear_config([:http, :send_user_agent], true)
  22. clear_config([:http, :user_agent], "totally-not-pleroma")
  23. assert RequestBuilder.headers(%Request{}, []) == %Request{
  24. headers: [{"user-agent", "totally-not-pleroma"}]
  25. }
  26. end
  27. end
  28. describe "add_param/4" do
  29. test "add file parameter" do
  30. %Request{
  31. body: %Tesla.Multipart{
  32. boundary: _,
  33. content_type_params: [],
  34. parts: [
  35. %Tesla.Multipart.Part{
  36. body: %File.Stream{
  37. line_or_bytes: 2048,
  38. modes: [:raw, :read_ahead, :read, :binary],
  39. path: "some-path/filename.png",
  40. raw: true
  41. },
  42. dispositions: [name: "filename.png", filename: "filename.png"],
  43. headers: []
  44. }
  45. ]
  46. }
  47. } = RequestBuilder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png")
  48. end
  49. test "add key to body" do
  50. %{
  51. body: %Tesla.Multipart{
  52. boundary: _,
  53. content_type_params: [],
  54. parts: [
  55. %Tesla.Multipart.Part{
  56. body: "\"someval\"",
  57. dispositions: [name: "somekey"],
  58. headers: [{"content-type", "application/json"}]
  59. }
  60. ]
  61. }
  62. } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
  63. end
  64. test "add form parameter" do
  65. assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
  66. body: %{"somename" => "someval"}
  67. }
  68. end
  69. test "add for location" do
  70. assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
  71. some_location: [{"somekey", "someval"}]
  72. }
  73. end
  74. end
  75. end