From 825077a5b0dc0c90d93bc94ae83398c4f68d0003 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 26 Jun 2019 18:36:42 +0700 Subject: Add Idempotency plug --- test/plugs/idempotency_plug_test.exs | 110 +++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 test/plugs/idempotency_plug_test.exs (limited to 'test') diff --git a/test/plugs/idempotency_plug_test.exs b/test/plugs/idempotency_plug_test.exs new file mode 100644 index 000000000..aebc463e9 --- /dev/null +++ b/test/plugs/idempotency_plug_test.exs @@ -0,0 +1,110 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Plugs.IdempotencyPlugTest do + use ExUnit.Case, async: true + use Plug.Test + + alias Pleroma.Plugs.IdempotencyPlug + alias Plug.Conn + + test "returns result from cache" do + key = "test1" + orig_request_id = "test1" + second_request_id = "test2" + body = "testing" + status = 200 + + :post + |> conn("/cofe") + |> put_req_header("idempotency-key", key) + |> Conn.put_resp_header("x-request-id", orig_request_id) + |> Conn.put_resp_content_type("application/json") + |> IdempotencyPlug.call([]) + |> Conn.send_resp(status, body) + + conn2 = + :post + |> conn("/cofe") + |> put_req_header("idempotency-key", key) + |> Conn.put_resp_header("x-request-id", second_request_id) + |> Conn.put_resp_content_type("application/json") + |> IdempotencyPlug.call([]) + + assert_raise Conn.AlreadySentError, fn -> + Conn.send_resp(conn2, :im_a_teapot, "no cofe") + end + + assert conn2.resp_body == body + assert conn2.status == status + + assert [^second_request_id] = Conn.get_resp_header(conn2, "x-request-id") + assert [^orig_request_id] = Conn.get_resp_header(conn2, "x-original-request-id") + assert [^key] = Conn.get_resp_header(conn2, "idempotency-key") + assert ["true"] = Conn.get_resp_header(conn2, "idempotent-replayed") + assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn2, "content-type") + end + + test "pass conn downstream if the cache not found" do + key = "test2" + orig_request_id = "test3" + body = "testing" + status = 200 + + conn = + :post + |> conn("/cofe") + |> put_req_header("idempotency-key", key) + |> Conn.put_resp_header("x-request-id", orig_request_id) + |> Conn.put_resp_content_type("application/json") + |> IdempotencyPlug.call([]) + |> Conn.send_resp(status, body) + + assert conn.resp_body == body + assert conn.status == status + + assert [] = Conn.get_resp_header(conn, "idempotent-replayed") + assert [^key] = Conn.get_resp_header(conn, "idempotency-key") + end + + test "passes conn downstream if idempotency is not present in headers" do + orig_request_id = "test4" + body = "testing" + status = 200 + + conn = + :post + |> conn("/cofe") + |> Conn.put_resp_header("x-request-id", orig_request_id) + |> Conn.put_resp_content_type("application/json") + |> IdempotencyPlug.call([]) + |> Conn.send_resp(status, body) + + assert [] = Conn.get_resp_header(conn, "idempotency-key") + end + + test "doesn't work with GET/DELETE" do + key = "test3" + body = "testing" + status = 200 + + conn = + :get + |> conn("/cofe") + |> put_req_header("idempotency-key", key) + |> IdempotencyPlug.call([]) + |> Conn.send_resp(status, body) + + assert [] = Conn.get_resp_header(conn, "idempotency-key") + + conn = + :delete + |> conn("/cofe") + |> put_req_header("idempotency-key", key) + |> IdempotencyPlug.call([]) + |> Conn.send_resp(status, body) + + assert [] = Conn.get_resp_header(conn, "idempotency-key") + end +end -- cgit v1.2.3 From 889a9c3a3f427f5fdf2708fd281c1218d08b8fd7 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Thu, 27 Jun 2019 01:53:36 +0700 Subject: Polish IdempotencyPlug --- test/plugs/idempotency_plug_test.exs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'test') diff --git a/test/plugs/idempotency_plug_test.exs b/test/plugs/idempotency_plug_test.exs index aebc463e9..ac1735f13 100644 --- a/test/plugs/idempotency_plug_test.exs +++ b/test/plugs/idempotency_plug_test.exs @@ -24,7 +24,7 @@ defmodule Pleroma.Plugs.IdempotencyPlugTest do |> IdempotencyPlug.call([]) |> Conn.send_resp(status, body) - conn2 = + conn = :post |> conn("/cofe") |> put_req_header("idempotency-key", key) @@ -33,17 +33,17 @@ defmodule Pleroma.Plugs.IdempotencyPlugTest do |> IdempotencyPlug.call([]) assert_raise Conn.AlreadySentError, fn -> - Conn.send_resp(conn2, :im_a_teapot, "no cofe") + Conn.send_resp(conn, :im_a_teapot, "no cofe") end - assert conn2.resp_body == body - assert conn2.status == status + assert conn.resp_body == body + assert conn.status == status - assert [^second_request_id] = Conn.get_resp_header(conn2, "x-request-id") - assert [^orig_request_id] = Conn.get_resp_header(conn2, "x-original-request-id") - assert [^key] = Conn.get_resp_header(conn2, "idempotency-key") - assert ["true"] = Conn.get_resp_header(conn2, "idempotent-replayed") - assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn2, "content-type") + assert [^second_request_id] = Conn.get_resp_header(conn, "x-request-id") + assert [^orig_request_id] = Conn.get_resp_header(conn, "x-original-request-id") + assert [^key] = Conn.get_resp_header(conn, "idempotency-key") + assert ["true"] = Conn.get_resp_header(conn, "idempotent-replayed") + assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn, "content-type") end test "pass conn downstream if the cache not found" do -- cgit v1.2.3