1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.IdempotencyPlugTest do
6 # Relies on Cachex, has to stay synchronous
10 alias Pleroma.Web.Plugs.IdempotencyPlug
13 test "returns result from cache" do
15 orig_request_id = "test1"
16 second_request_id = "test2"
22 |> put_req_header("idempotency-key", key)
23 |> Conn.put_resp_header("x-request-id", orig_request_id)
24 |> Conn.put_resp_content_type("application/json")
25 |> IdempotencyPlug.call([])
26 |> Conn.send_resp(status, body)
31 |> put_req_header("idempotency-key", key)
32 |> Conn.put_resp_header("x-request-id", second_request_id)
33 |> Conn.put_resp_content_type("application/json")
34 |> IdempotencyPlug.call([])
36 assert_raise Conn.AlreadySentError, fn ->
37 Conn.send_resp(conn, :im_a_teapot, "no cofe")
40 assert conn.resp_body == body
41 assert conn.status == status
43 assert [^second_request_id] = Conn.get_resp_header(conn, "x-request-id")
44 assert [^orig_request_id] = Conn.get_resp_header(conn, "x-original-request-id")
45 assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
46 assert ["true"] = Conn.get_resp_header(conn, "idempotent-replayed")
47 assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn, "content-type")
50 test "pass conn downstream if the cache not found" do
52 orig_request_id = "test3"
59 |> put_req_header("idempotency-key", key)
60 |> Conn.put_resp_header("x-request-id", orig_request_id)
61 |> Conn.put_resp_content_type("application/json")
62 |> IdempotencyPlug.call([])
63 |> Conn.send_resp(status, body)
65 assert conn.resp_body == body
66 assert conn.status == status
68 assert [] = Conn.get_resp_header(conn, "idempotent-replayed")
69 assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
72 test "passes conn downstream if idempotency is not present in headers" do
73 orig_request_id = "test4"
80 |> Conn.put_resp_header("x-request-id", orig_request_id)
81 |> Conn.put_resp_content_type("application/json")
82 |> IdempotencyPlug.call([])
83 |> Conn.send_resp(status, body)
85 assert [] = Conn.get_resp_header(conn, "idempotency-key")
88 test "doesn't work with GET/DELETE" do
96 |> put_req_header("idempotency-key", key)
97 |> IdempotencyPlug.call([])
98 |> Conn.send_resp(status, body)
100 assert [] = Conn.get_resp_header(conn, "idempotency-key")
105 |> put_req_header("idempotency-key", key)
106 |> IdempotencyPlug.call([])
107 |> Conn.send_resp(status, body)
109 assert [] = Conn.get_resp_header(conn, "idempotency-key")