forked from YokaiRick/akkoma
Polish IdempotencyPlug
This commit is contained in:
parent
159630b21c
commit
889a9c3a3f
3 changed files with 14 additions and 14 deletions
|
@ -29,16 +29,16 @@ def process_request(conn, key) do
|
||||||
{:ok, nil} ->
|
{:ok, nil} ->
|
||||||
cache_resposnse(conn, key)
|
cache_resposnse(conn, key)
|
||||||
|
|
||||||
{atom, message} when atom in [:ignore, :error] ->
|
|
||||||
render_error(conn, message)
|
|
||||||
|
|
||||||
{:ok, record} ->
|
{:ok, record} ->
|
||||||
send_cached(conn, key, record)
|
send_cached(conn, key, record)
|
||||||
|
|
||||||
|
{atom, message} when atom in [:ignore, :error] ->
|
||||||
|
render_error(conn, message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp cache_resposnse(conn, key) do
|
defp cache_resposnse(conn, key) do
|
||||||
Plug.Conn.register_before_send(conn, fn conn ->
|
register_before_send(conn, fn conn ->
|
||||||
[request_id] = get_resp_header(conn, "x-request-id")
|
[request_id] = get_resp_header(conn, "x-request-id")
|
||||||
content_type = get_content_type(conn)
|
content_type = get_content_type(conn)
|
||||||
|
|
||||||
|
|
|
@ -564,7 +564,7 @@ def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
|
||||||
case CommonAPI.post(user, params) do
|
case CommonAPI.post(user, params) do
|
||||||
{:error, message} ->
|
{:error, message} ->
|
||||||
conn
|
conn
|
||||||
|> put_status(422)
|
|> put_status(:unprocessable_entity)
|
||||||
|> json(%{error: message})
|
|> json(%{error: message})
|
||||||
|
|
||||||
{:ok, activity} ->
|
{:ok, activity} ->
|
||||||
|
|
|
@ -24,7 +24,7 @@ test "returns result from cache" do
|
||||||
|> IdempotencyPlug.call([])
|
|> IdempotencyPlug.call([])
|
||||||
|> Conn.send_resp(status, body)
|
|> Conn.send_resp(status, body)
|
||||||
|
|
||||||
conn2 =
|
conn =
|
||||||
:post
|
:post
|
||||||
|> conn("/cofe")
|
|> conn("/cofe")
|
||||||
|> put_req_header("idempotency-key", key)
|
|> put_req_header("idempotency-key", key)
|
||||||
|
@ -33,17 +33,17 @@ test "returns result from cache" do
|
||||||
|> IdempotencyPlug.call([])
|
|> IdempotencyPlug.call([])
|
||||||
|
|
||||||
assert_raise Conn.AlreadySentError, fn ->
|
assert_raise Conn.AlreadySentError, fn ->
|
||||||
Conn.send_resp(conn2, :im_a_teapot, "no cofe")
|
Conn.send_resp(conn, :im_a_teapot, "no cofe")
|
||||||
end
|
end
|
||||||
|
|
||||||
assert conn2.resp_body == body
|
assert conn.resp_body == body
|
||||||
assert conn2.status == status
|
assert conn.status == status
|
||||||
|
|
||||||
assert [^second_request_id] = Conn.get_resp_header(conn2, "x-request-id")
|
assert [^second_request_id] = Conn.get_resp_header(conn, "x-request-id")
|
||||||
assert [^orig_request_id] = Conn.get_resp_header(conn2, "x-original-request-id")
|
assert [^orig_request_id] = Conn.get_resp_header(conn, "x-original-request-id")
|
||||||
assert [^key] = Conn.get_resp_header(conn2, "idempotency-key")
|
assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
|
||||||
assert ["true"] = Conn.get_resp_header(conn2, "idempotent-replayed")
|
assert ["true"] = Conn.get_resp_header(conn, "idempotent-replayed")
|
||||||
assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn2, "content-type")
|
assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn, "content-type")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "pass conn downstream if the cache not found" do
|
test "pass conn downstream if the cache not found" do
|
||||||
|
|
Loading…
Reference in a new issue