Add Pleroma.Plugs.Cache
This commit is contained in:
parent
bce16f4557
commit
11e12b5761
11 changed files with 457 additions and 7 deletions
|
|
@ -53,9 +53,12 @@ defmodule Pleroma.ObjectTest do
|
|||
|
||||
assert object == cached_object
|
||||
|
||||
Cachex.put(:web_resp_cache, URI.parse(object.data["id"]).path, "cofe")
|
||||
|
||||
Object.delete(cached_object)
|
||||
|
||||
{:ok, nil} = Cachex.get(:object_cache, "object:#{object.data["id"]}")
|
||||
{:ok, nil} = Cachex.get(:web_resp_cache, URI.parse(object.data["id"]).path)
|
||||
|
||||
cached_object = Object.get_cached_by_ap_id(object.data["id"])
|
||||
|
||||
|
|
|
|||
186
test/plugs/cache_test.exs
Normal file
186
test/plugs/cache_test.exs
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Plugs.CacheTest do
|
||||
use ExUnit.Case, async: true
|
||||
use Plug.Test
|
||||
|
||||
alias Pleroma.Plugs.Cache
|
||||
|
||||
@miss_resp {200,
|
||||
[
|
||||
{"cache-control", "max-age=0, private, must-revalidate"},
|
||||
{"content-type", "cofe/hot; charset=utf-8"},
|
||||
{"x-cache", "MISS from Pleroma"}
|
||||
], "cofe"}
|
||||
|
||||
@hit_resp {200,
|
||||
[
|
||||
{"cache-control", "max-age=0, private, must-revalidate"},
|
||||
{"content-type", "cofe/hot; charset=utf-8"},
|
||||
{"x-cache", "HIT from Pleroma"}
|
||||
], "cofe"}
|
||||
|
||||
@ttl 5
|
||||
|
||||
setup do
|
||||
Cachex.clear(:web_resp_cache)
|
||||
:ok
|
||||
end
|
||||
|
||||
test "caches a response" do
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
|
||||
assert_raise(Plug.Conn.AlreadySentError, fn ->
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
end)
|
||||
|
||||
assert @hit_resp ==
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: nil})
|
||||
|> sent_resp()
|
||||
end
|
||||
|
||||
test "ttl is set" do
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: @ttl})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
|
||||
assert @hit_resp ==
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: @ttl})
|
||||
|> sent_resp()
|
||||
|
||||
:timer.sleep(@ttl + 1)
|
||||
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: @ttl})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
end
|
||||
|
||||
test "set ttl via conn.assigns" do
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> assign(:cache_ttl, @ttl)
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
|
||||
assert @hit_resp ==
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: nil})
|
||||
|> sent_resp()
|
||||
|
||||
:timer.sleep(@ttl + 1)
|
||||
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/")
|
||||
|> Cache.call(%{query_params: false, ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
end
|
||||
|
||||
test "ignore query string when `query_params` is false" do
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/?cofe")
|
||||
|> Cache.call(%{query_params: false, ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
|
||||
assert @hit_resp ==
|
||||
conn(:get, "/?cofefe")
|
||||
|> Cache.call(%{query_params: false, ttl: nil})
|
||||
|> sent_resp()
|
||||
end
|
||||
|
||||
test "take query string into account when `query_params` is true" do
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/?cofe")
|
||||
|> Cache.call(%{query_params: true, ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/?cofefe")
|
||||
|> Cache.call(%{query_params: true, ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
end
|
||||
|
||||
test "take specific query params into account when `query_params` is list" do
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/?a=1&b=2&c=3&foo=bar")
|
||||
|> fetch_query_params()
|
||||
|> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
|
||||
assert @hit_resp ==
|
||||
conn(:get, "/?bar=foo&c=3&b=2&a=1")
|
||||
|> fetch_query_params()
|
||||
|> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
|
||||
|> sent_resp()
|
||||
|
||||
assert @miss_resp ==
|
||||
conn(:get, "/?bar=foo&c=3&b=2&a=2")
|
||||
|> fetch_query_params()
|
||||
|> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
end
|
||||
|
||||
test "ignore not GET requests" do
|
||||
expected =
|
||||
{200,
|
||||
[
|
||||
{"cache-control", "max-age=0, private, must-revalidate"},
|
||||
{"content-type", "cofe/hot; charset=utf-8"}
|
||||
], "cofe"}
|
||||
|
||||
assert expected ==
|
||||
conn(:post, "/")
|
||||
|> Cache.call(%{query_params: true, ttl: nil})
|
||||
|> put_resp_content_type("cofe/hot")
|
||||
|> send_resp(:ok, "cofe")
|
||||
|> sent_resp()
|
||||
end
|
||||
|
||||
test "ignore non-successful responses" do
|
||||
expected =
|
||||
{418,
|
||||
[
|
||||
{"cache-control", "max-age=0, private, must-revalidate"},
|
||||
{"content-type", "tea/iced; charset=utf-8"}
|
||||
], "🥤"}
|
||||
|
||||
assert expected ==
|
||||
conn(:get, "/cofe")
|
||||
|> Cache.call(%{query_params: true, ttl: nil})
|
||||
|> put_resp_content_type("tea/iced")
|
||||
|> send_resp(:im_a_teapot, "🥤")
|
||||
|> sent_resp()
|
||||
end
|
||||
end
|
||||
|
|
@ -175,6 +175,49 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
|
|||
|
||||
assert json_response(conn, 404)
|
||||
end
|
||||
|
||||
test "it caches a response", %{conn: conn} do
|
||||
note = insert(:note)
|
||||
uuid = String.split(note.data["id"], "/") |> List.last()
|
||||
|
||||
conn1 =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/objects/#{uuid}")
|
||||
|
||||
assert json_response(conn1, :ok)
|
||||
assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"}))
|
||||
|
||||
conn2 =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/objects/#{uuid}")
|
||||
|
||||
assert json_response(conn1, :ok) == json_response(conn2, :ok)
|
||||
assert Enum.any?(conn2.resp_headers, &(&1 == {"x-cache", "HIT from Pleroma"}))
|
||||
end
|
||||
|
||||
test "cached purged after object deletion", %{conn: conn} do
|
||||
note = insert(:note)
|
||||
uuid = String.split(note.data["id"], "/") |> List.last()
|
||||
|
||||
conn1 =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/objects/#{uuid}")
|
||||
|
||||
assert json_response(conn1, :ok)
|
||||
assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"}))
|
||||
|
||||
Object.delete(note)
|
||||
|
||||
conn2 =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/objects/#{uuid}")
|
||||
|
||||
assert "Not found" == json_response(conn2, :not_found)
|
||||
end
|
||||
end
|
||||
|
||||
describe "/object/:uuid/likes" do
|
||||
|
|
@ -264,6 +307,51 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
|
|||
|
||||
assert json_response(conn, 404)
|
||||
end
|
||||
|
||||
test "it caches a response", %{conn: conn} do
|
||||
activity = insert(:note_activity)
|
||||
uuid = String.split(activity.data["id"], "/") |> List.last()
|
||||
|
||||
conn1 =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/activities/#{uuid}")
|
||||
|
||||
assert json_response(conn1, :ok)
|
||||
assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"}))
|
||||
|
||||
conn2 =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/activities/#{uuid}")
|
||||
|
||||
assert json_response(conn1, :ok) == json_response(conn2, :ok)
|
||||
assert Enum.any?(conn2.resp_headers, &(&1 == {"x-cache", "HIT from Pleroma"}))
|
||||
end
|
||||
|
||||
test "cached purged after activity deletion", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
{:ok, activity} = CommonAPI.post(user, %{"status" => "cofe"})
|
||||
|
||||
uuid = String.split(activity.data["id"], "/") |> List.last()
|
||||
|
||||
conn1 =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/activities/#{uuid}")
|
||||
|
||||
assert json_response(conn1, :ok)
|
||||
assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"}))
|
||||
|
||||
Activity.delete_by_ap_id(activity.object.data["id"])
|
||||
|
||||
conn2 =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/activities/#{uuid}")
|
||||
|
||||
assert "Not found" == json_response(conn2, :not_found)
|
||||
end
|
||||
end
|
||||
|
||||
describe "/inbox" do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue