logo

pleroma

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

idempotency_plug_test.exs (3208B)


  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.Plugs.IdempotencyPlugTest do
  5. use ExUnit.Case, async: true
  6. use Plug.Test
  7. alias Pleroma.Plugs.IdempotencyPlug
  8. alias Plug.Conn
  9. test "returns result from cache" do
  10. key = "test1"
  11. orig_request_id = "test1"
  12. second_request_id = "test2"
  13. body = "testing"
  14. status = 200
  15. :post
  16. |> conn("/cofe")
  17. |> put_req_header("idempotency-key", key)
  18. |> Conn.put_resp_header("x-request-id", orig_request_id)
  19. |> Conn.put_resp_content_type("application/json")
  20. |> IdempotencyPlug.call([])
  21. |> Conn.send_resp(status, body)
  22. conn =
  23. :post
  24. |> conn("/cofe")
  25. |> put_req_header("idempotency-key", key)
  26. |> Conn.put_resp_header("x-request-id", second_request_id)
  27. |> Conn.put_resp_content_type("application/json")
  28. |> IdempotencyPlug.call([])
  29. assert_raise Conn.AlreadySentError, fn ->
  30. Conn.send_resp(conn, :im_a_teapot, "no cofe")
  31. end
  32. assert conn.resp_body == body
  33. assert conn.status == status
  34. assert [^second_request_id] = Conn.get_resp_header(conn, "x-request-id")
  35. assert [^orig_request_id] = Conn.get_resp_header(conn, "x-original-request-id")
  36. assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
  37. assert ["true"] = Conn.get_resp_header(conn, "idempotent-replayed")
  38. assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn, "content-type")
  39. end
  40. test "pass conn downstream if the cache not found" do
  41. key = "test2"
  42. orig_request_id = "test3"
  43. body = "testing"
  44. status = 200
  45. conn =
  46. :post
  47. |> conn("/cofe")
  48. |> put_req_header("idempotency-key", key)
  49. |> Conn.put_resp_header("x-request-id", orig_request_id)
  50. |> Conn.put_resp_content_type("application/json")
  51. |> IdempotencyPlug.call([])
  52. |> Conn.send_resp(status, body)
  53. assert conn.resp_body == body
  54. assert conn.status == status
  55. assert [] = Conn.get_resp_header(conn, "idempotent-replayed")
  56. assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
  57. end
  58. test "passes conn downstream if idempotency is not present in headers" do
  59. orig_request_id = "test4"
  60. body = "testing"
  61. status = 200
  62. conn =
  63. :post
  64. |> conn("/cofe")
  65. |> Conn.put_resp_header("x-request-id", orig_request_id)
  66. |> Conn.put_resp_content_type("application/json")
  67. |> IdempotencyPlug.call([])
  68. |> Conn.send_resp(status, body)
  69. assert [] = Conn.get_resp_header(conn, "idempotency-key")
  70. end
  71. test "doesn't work with GET/DELETE" do
  72. key = "test3"
  73. body = "testing"
  74. status = 200
  75. conn =
  76. :get
  77. |> conn("/cofe")
  78. |> put_req_header("idempotency-key", key)
  79. |> IdempotencyPlug.call([])
  80. |> Conn.send_resp(status, body)
  81. assert [] = Conn.get_resp_header(conn, "idempotency-key")
  82. conn =
  83. :delete
  84. |> conn("/cofe")
  85. |> put_req_header("idempotency-key", key)
  86. |> IdempotencyPlug.call([])
  87. |> Conn.send_resp(status, body)
  88. assert [] = Conn.get_resp_header(conn, "idempotency-key")
  89. end
  90. end