Polish IdempotencyPlug
[akkoma] / test / plugs / idempotency_plug_test.exs
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
5 defmodule Pleroma.Plugs.IdempotencyPlugTest do
6 use ExUnit.Case, async: true
7 use Plug.Test
8
9 alias Pleroma.Plugs.IdempotencyPlug
10 alias Plug.Conn
11
12 test "returns result from cache" do
13 key = "test1"
14 orig_request_id = "test1"
15 second_request_id = "test2"
16 body = "testing"
17 status = 200
18
19 :post
20 |> conn("/cofe")
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)
26
27 conn =
28 :post
29 |> conn("/cofe")
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([])
34
35 assert_raise Conn.AlreadySentError, fn ->
36 Conn.send_resp(conn, :im_a_teapot, "no cofe")
37 end
38
39 assert conn.resp_body == body
40 assert conn.status == status
41
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")
47 end
48
49 test "pass conn downstream if the cache not found" do
50 key = "test2"
51 orig_request_id = "test3"
52 body = "testing"
53 status = 200
54
55 conn =
56 :post
57 |> conn("/cofe")
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)
63
64 assert conn.resp_body == body
65 assert conn.status == status
66
67 assert [] = Conn.get_resp_header(conn, "idempotent-replayed")
68 assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
69 end
70
71 test "passes conn downstream if idempotency is not present in headers" do
72 orig_request_id = "test4"
73 body = "testing"
74 status = 200
75
76 conn =
77 :post
78 |> conn("/cofe")
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)
83
84 assert [] = Conn.get_resp_header(conn, "idempotency-key")
85 end
86
87 test "doesn't work with GET/DELETE" do
88 key = "test3"
89 body = "testing"
90 status = 200
91
92 conn =
93 :get
94 |> conn("/cofe")
95 |> put_req_header("idempotency-key", key)
96 |> IdempotencyPlug.call([])
97 |> Conn.send_resp(status, body)
98
99 assert [] = Conn.get_resp_header(conn, "idempotency-key")
100
101 conn =
102 :delete
103 |> conn("/cofe")
104 |> put_req_header("idempotency-key", key)
105 |> IdempotencyPlug.call([])
106 |> Conn.send_resp(status, body)
107
108 assert [] = Conn.get_resp_header(conn, "idempotency-key")
109 end
110 end