1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.IdempotencyPlugTest do
6 use ExUnit.Case, async: true
9 alias Pleroma.Plugs.IdempotencyPlug
12 test "returns result from cache" do
14 orig_request_id = "test1"
15 second_request_id = "test2"
21 |> put_req_header("idempotency-key", key)
22 |> Conn.put_resp_header("x-request-id", orig_request_id)
23 |> Conn.put_resp_content_type("application/json")
24 |> IdempotencyPlug.call([])
25 |> Conn.send_resp(status, body)
30 |> put_req_header("idempotency-key", key)
31 |> Conn.put_resp_header("x-request-id", second_request_id)
32 |> Conn.put_resp_content_type("application/json")
33 |> IdempotencyPlug.call([])
35 assert_raise Conn.AlreadySentError, fn ->
36 Conn.send_resp(conn, :im_a_teapot, "no cofe")
39 assert conn.resp_body == body
40 assert conn.status == status
42 assert [^second_request_id] = Conn.get_resp_header(conn, "x-request-id")
43 assert [^orig_request_id] = Conn.get_resp_header(conn, "x-original-request-id")
44 assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
45 assert ["true"] = Conn.get_resp_header(conn, "idempotent-replayed")
46 assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn, "content-type")
49 test "pass conn downstream if the cache not found" do
51 orig_request_id = "test3"
58 |> put_req_header("idempotency-key", key)
59 |> Conn.put_resp_header("x-request-id", orig_request_id)
60 |> Conn.put_resp_content_type("application/json")
61 |> IdempotencyPlug.call([])
62 |> Conn.send_resp(status, body)
64 assert conn.resp_body == body
65 assert conn.status == status
67 assert [] = Conn.get_resp_header(conn, "idempotent-replayed")
68 assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
71 test "passes conn downstream if idempotency is not present in headers" do
72 orig_request_id = "test4"
79 |> Conn.put_resp_header("x-request-id", orig_request_id)
80 |> Conn.put_resp_content_type("application/json")
81 |> IdempotencyPlug.call([])
82 |> Conn.send_resp(status, body)
84 assert [] = Conn.get_resp_header(conn, "idempotency-key")
87 test "doesn't work with GET/DELETE" do
95 |> put_req_header("idempotency-key", key)
96 |> IdempotencyPlug.call([])
97 |> Conn.send_resp(status, body)
99 assert [] = Conn.get_resp_header(conn, "idempotency-key")
104 |> put_req_header("idempotency-key", key)
105 |> IdempotencyPlug.call([])
106 |> Conn.send_resp(status, body)
108 assert [] = Conn.get_resp_header(conn, "idempotency-key")