1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.CacheTest do
6 # Relies on Cachex, has to stay synchronous
10 alias Pleroma.Web.Plugs.Cache
14 {"cache-control", "max-age=0, private, must-revalidate"},
15 {"content-type", "cofe/hot; charset=utf-8"},
16 {"x-cache", "MISS from Pleroma"}
21 {"cache-control", "max-age=0, private, must-revalidate"},
22 {"content-type", "cofe/hot; charset=utf-8"},
23 {"x-cache", "HIT from Pleroma"}
28 test "caches a response" do
31 |> Cache.call(%{query_params: false, ttl: nil})
32 |> put_resp_content_type("cofe/hot")
33 |> send_resp(:ok, "cofe")
36 assert_raise(Plug.Conn.AlreadySentError, fn ->
38 |> Cache.call(%{query_params: false, ttl: nil})
39 |> put_resp_content_type("cofe/hot")
40 |> send_resp(:ok, "cofe")
46 |> Cache.call(%{query_params: false, ttl: nil})
53 |> Cache.call(%{query_params: false, ttl: @ttl})
54 |> put_resp_content_type("cofe/hot")
55 |> send_resp(:ok, "cofe")
60 |> Cache.call(%{query_params: false, ttl: @ttl})
63 :timer.sleep(@ttl + 1)
67 |> Cache.call(%{query_params: false, ttl: @ttl})
68 |> put_resp_content_type("cofe/hot")
69 |> send_resp(:ok, "cofe")
73 test "set ttl via conn.assigns" do
76 |> Cache.call(%{query_params: false, ttl: nil})
77 |> put_resp_content_type("cofe/hot")
78 |> assign(:cache_ttl, @ttl)
79 |> send_resp(:ok, "cofe")
84 |> Cache.call(%{query_params: false, ttl: nil})
87 :timer.sleep(@ttl + 1)
91 |> Cache.call(%{query_params: false, ttl: nil})
92 |> put_resp_content_type("cofe/hot")
93 |> send_resp(:ok, "cofe")
97 test "ignore query string when `query_params` is false" do
100 |> Cache.call(%{query_params: false, ttl: nil})
101 |> put_resp_content_type("cofe/hot")
102 |> send_resp(:ok, "cofe")
106 conn(:get, "/?cofefe")
107 |> Cache.call(%{query_params: false, ttl: nil})
111 test "take query string into account when `query_params` is true" do
114 |> Cache.call(%{query_params: true, ttl: nil})
115 |> put_resp_content_type("cofe/hot")
116 |> send_resp(:ok, "cofe")
120 conn(:get, "/?cofefe")
121 |> Cache.call(%{query_params: true, ttl: nil})
122 |> put_resp_content_type("cofe/hot")
123 |> send_resp(:ok, "cofe")
127 test "take specific query params into account when `query_params` is list" do
129 conn(:get, "/?a=1&b=2&c=3&foo=bar")
130 |> fetch_query_params()
131 |> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
132 |> put_resp_content_type("cofe/hot")
133 |> send_resp(:ok, "cofe")
137 conn(:get, "/?bar=foo&c=3&b=2&a=1")
138 |> fetch_query_params()
139 |> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
143 conn(:get, "/?bar=foo&c=3&b=2&a=2")
144 |> fetch_query_params()
145 |> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
146 |> put_resp_content_type("cofe/hot")
147 |> send_resp(:ok, "cofe")
151 test "ignore not GET requests" do
155 {"cache-control", "max-age=0, private, must-revalidate"},
156 {"content-type", "cofe/hot; charset=utf-8"}
161 |> Cache.call(%{query_params: true, ttl: nil})
162 |> put_resp_content_type("cofe/hot")
163 |> send_resp(:ok, "cofe")
167 test "ignore non-successful responses" do
171 {"cache-control", "max-age=0, private, must-revalidate"},
172 {"content-type", "tea/iced; charset=utf-8"}
177 |> Cache.call(%{query_params: true, ttl: nil})
178 |> put_resp_content_type("tea/iced")
179 |> send_resp(:im_a_teapot, "🥤")
183 test "ignores if skip_cache is assigned" do
186 |> assign(:skip_cache, true)
187 |> Cache.call(%{query_params: false, ttl: nil})
188 |> put_resp_content_type("cofe/hot")
189 |> send_resp(:ok, "cofe")
194 |> assign(:skip_cache, true)
195 |> Cache.call(%{query_params: false, ttl: nil})
196 |> put_resp_content_type("cofe/hot")
197 |> send_resp(:ok, "cofe")