logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

cache_test.exs (5684B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Plugs.CacheTest do
  5. use ExUnit.Case, async: true
  6. use Plug.Test
  7. alias Pleroma.Plugs.Cache
  8. @miss_resp {200,
  9. [
  10. {"cache-control", "max-age=0, private, must-revalidate"},
  11. {"content-type", "cofe/hot; charset=utf-8"},
  12. {"x-cache", "MISS from Pleroma"}
  13. ], "cofe"}
  14. @hit_resp {200,
  15. [
  16. {"cache-control", "max-age=0, private, must-revalidate"},
  17. {"content-type", "cofe/hot; charset=utf-8"},
  18. {"x-cache", "HIT from Pleroma"}
  19. ], "cofe"}
  20. @ttl 5
  21. setup do
  22. Cachex.clear(:web_resp_cache)
  23. :ok
  24. end
  25. test "caches a response" do
  26. assert @miss_resp ==
  27. conn(:get, "/")
  28. |> Cache.call(%{query_params: false, ttl: nil})
  29. |> put_resp_content_type("cofe/hot")
  30. |> send_resp(:ok, "cofe")
  31. |> sent_resp()
  32. assert_raise(Plug.Conn.AlreadySentError, fn ->
  33. conn(:get, "/")
  34. |> Cache.call(%{query_params: false, ttl: nil})
  35. |> put_resp_content_type("cofe/hot")
  36. |> send_resp(:ok, "cofe")
  37. |> sent_resp()
  38. end)
  39. assert @hit_resp ==
  40. conn(:get, "/")
  41. |> Cache.call(%{query_params: false, ttl: nil})
  42. |> sent_resp()
  43. end
  44. test "ttl is set" do
  45. assert @miss_resp ==
  46. conn(:get, "/")
  47. |> Cache.call(%{query_params: false, ttl: @ttl})
  48. |> put_resp_content_type("cofe/hot")
  49. |> send_resp(:ok, "cofe")
  50. |> sent_resp()
  51. assert @hit_resp ==
  52. conn(:get, "/")
  53. |> Cache.call(%{query_params: false, ttl: @ttl})
  54. |> sent_resp()
  55. :timer.sleep(@ttl + 1)
  56. assert @miss_resp ==
  57. conn(:get, "/")
  58. |> Cache.call(%{query_params: false, ttl: @ttl})
  59. |> put_resp_content_type("cofe/hot")
  60. |> send_resp(:ok, "cofe")
  61. |> sent_resp()
  62. end
  63. test "set ttl via conn.assigns" do
  64. assert @miss_resp ==
  65. conn(:get, "/")
  66. |> Cache.call(%{query_params: false, ttl: nil})
  67. |> put_resp_content_type("cofe/hot")
  68. |> assign(:cache_ttl, @ttl)
  69. |> send_resp(:ok, "cofe")
  70. |> sent_resp()
  71. assert @hit_resp ==
  72. conn(:get, "/")
  73. |> Cache.call(%{query_params: false, ttl: nil})
  74. |> sent_resp()
  75. :timer.sleep(@ttl + 1)
  76. assert @miss_resp ==
  77. conn(:get, "/")
  78. |> Cache.call(%{query_params: false, ttl: nil})
  79. |> put_resp_content_type("cofe/hot")
  80. |> send_resp(:ok, "cofe")
  81. |> sent_resp()
  82. end
  83. test "ignore query string when `query_params` is false" do
  84. assert @miss_resp ==
  85. conn(:get, "/?cofe")
  86. |> Cache.call(%{query_params: false, ttl: nil})
  87. |> put_resp_content_type("cofe/hot")
  88. |> send_resp(:ok, "cofe")
  89. |> sent_resp()
  90. assert @hit_resp ==
  91. conn(:get, "/?cofefe")
  92. |> Cache.call(%{query_params: false, ttl: nil})
  93. |> sent_resp()
  94. end
  95. test "take query string into account when `query_params` is true" do
  96. assert @miss_resp ==
  97. conn(:get, "/?cofe")
  98. |> Cache.call(%{query_params: true, ttl: nil})
  99. |> put_resp_content_type("cofe/hot")
  100. |> send_resp(:ok, "cofe")
  101. |> sent_resp()
  102. assert @miss_resp ==
  103. conn(:get, "/?cofefe")
  104. |> Cache.call(%{query_params: true, ttl: nil})
  105. |> put_resp_content_type("cofe/hot")
  106. |> send_resp(:ok, "cofe")
  107. |> sent_resp()
  108. end
  109. test "take specific query params into account when `query_params` is list" do
  110. assert @miss_resp ==
  111. conn(:get, "/?a=1&b=2&c=3&foo=bar")
  112. |> fetch_query_params()
  113. |> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
  114. |> put_resp_content_type("cofe/hot")
  115. |> send_resp(:ok, "cofe")
  116. |> sent_resp()
  117. assert @hit_resp ==
  118. conn(:get, "/?bar=foo&c=3&b=2&a=1")
  119. |> fetch_query_params()
  120. |> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
  121. |> sent_resp()
  122. assert @miss_resp ==
  123. conn(:get, "/?bar=foo&c=3&b=2&a=2")
  124. |> fetch_query_params()
  125. |> Cache.call(%{query_params: ["a", "b", "c"], ttl: nil})
  126. |> put_resp_content_type("cofe/hot")
  127. |> send_resp(:ok, "cofe")
  128. |> sent_resp()
  129. end
  130. test "ignore not GET requests" do
  131. expected =
  132. {200,
  133. [
  134. {"cache-control", "max-age=0, private, must-revalidate"},
  135. {"content-type", "cofe/hot; charset=utf-8"}
  136. ], "cofe"}
  137. assert expected ==
  138. conn(:post, "/")
  139. |> Cache.call(%{query_params: true, ttl: nil})
  140. |> put_resp_content_type("cofe/hot")
  141. |> send_resp(:ok, "cofe")
  142. |> sent_resp()
  143. end
  144. test "ignore non-successful responses" do
  145. expected =
  146. {418,
  147. [
  148. {"cache-control", "max-age=0, private, must-revalidate"},
  149. {"content-type", "tea/iced; charset=utf-8"}
  150. ], "🥤"}
  151. assert expected ==
  152. conn(:get, "/cofe")
  153. |> Cache.call(%{query_params: true, ttl: nil})
  154. |> put_resp_content_type("tea/iced")
  155. |> send_resp(:im_a_teapot, "🥤")
  156. |> sent_resp()
  157. end
  158. end