diff --git a/lib/request_cache/middleware.ex b/lib/request_cache/middleware.ex index 1638667..6307595 100644 --- a/lib/request_cache/middleware.ex +++ b/lib/request_cache/middleware.ex @@ -22,9 +22,7 @@ if absinthe_loaded? do resolution = resolve_resolver_func_middleware(resolution, opts) if resolution.context[RequestCache.Config.conn_private_key()][:enabled?] do - if RequestCache.Config.verbose?() do - Util.verbose_log("[RequestCache.Middleware] Enabling cache for resolution") - end + Util.verbose_log("[RequestCache.Middleware] Enabling cache for resolution") %{resolution | value: resolution.value || opts[:value], diff --git a/lib/request_cache/util.ex b/lib/request_cache/util.ex index c241251..0b667e5 100644 --- a/lib/request_cache/util.ex +++ b/lib/request_cache/util.ex @@ -5,14 +5,6 @@ defmodule RequestCache.Util do @whitelisted_modules [DateTime, NaiveDateTime, Date, Time, File.Stat, MapSet, Regex, URI, Version] - - # def parse_gql_name(query_string) do - # case Regex.run(~r/^(?:query) ([^\({]+(?=\(|{))/, query_string, capture: :all_but_first) do - # [query_name] -> String.trim(query_name) - # _ -> nil - # end - # end - def create_key(url_path, query_string) do "#{url_path}:#{hash_string(query_string)}" end @@ -22,7 +14,7 @@ defmodule RequestCache.Util do end def log_cache_disabled_message do - Logger.debug("RequestCache requested but hasn't been enabled, ensure query has a name and the RequestCache.Plug is part of your Endpoint") + Logger.warning("RequestCache requested but hasn't been enabled, ensure RequestCache.Plug is part of your endpoint.ex file") end def verbose_log(message) do diff --git a/test/request_cache/metrics_test.exs b/test/request_cache/metrics_test.exs index 8cfd524..e055fcd 100644 --- a/test/request_cache/metrics_test.exs +++ b/test/request_cache/metrics_test.exs @@ -29,6 +29,7 @@ defmodule RequestCache.TelemetryMetricsTest do %{conn: conn} end + @tag capture_log: true test "cache miss", %{parent_pid: parent_pid, test: test, conn: conn} do start_telemetry_listener(parent_pid, test, @expected_graphql_cache_miss_event_name) @@ -38,6 +39,7 @@ defmodule RequestCache.TelemetryMetricsTest do @expected_measurements, _metadata} end + @tag capture_log: true test "cache miss with labels", %{parent_pid: parent_pid, test: test, conn: conn} do start_telemetry_listener(parent_pid, test, @expected_graphql_cache_miss_event_name) @@ -67,6 +69,7 @@ defmodule RequestCache.TelemetryMetricsTest do %{conn: conn} end + @tag capture_log: true test "cache hit", %{parent_pid: parent_pid, test: test, conn: conn} do start_telemetry_listener(parent_pid, test, @expected_graphql_cache_hit_event_name) @@ -76,6 +79,7 @@ defmodule RequestCache.TelemetryMetricsTest do @expected_measurements, _metadata} end + @tag capture_log: true test "cache hit with labels", %{parent_pid: parent_pid, test: test, conn: conn} do start_telemetry_listener(parent_pid, test, @expected_graphql_cache_hit_event_name) @@ -91,6 +95,7 @@ defmodule RequestCache.TelemetryMetricsTest do end describe "REST RequestCache.Plug.call/2" do + @tag capture_log: true test "cache miss", %{parent_pid: parent_pid, test: test} do start_telemetry_listener(parent_pid, test, @expected_rest_cache_miss_event_name) @@ -102,6 +107,7 @@ defmodule RequestCache.TelemetryMetricsTest do @expected_measurements, _metadata} end + @tag capture_log: true test "cache hit", %{parent_pid: parent_pid, test: test} do RequestCache.ConCacheStore.put( nil, @@ -122,6 +128,7 @@ defmodule RequestCache.TelemetryMetricsTest do end describe "metrics/0" do + @tag capture_log: true test "metric definitions are correct" do assert [ %Telemetry.Metrics.Counter{ diff --git a/test/request_cache_absinthe_test.exs b/test/request_cache_absinthe_test.exs index 976e783..668cf4e 100644 --- a/test/request_cache_absinthe_test.exs +++ b/test/request_cache_absinthe_test.exs @@ -125,10 +125,10 @@ defmodule RequestCacheAbsintheTest do assert_raise Plug.Conn.WrapperError, fn -> conn = :get - |> conn(graphql_url(@uncached_query)) - |> RequestCache.Support.Utils.ensure_default_opts() - |> Absinthe.Plug.put_options(context: %{call_pid: pid}) - |> Router.call([]) + |> conn(graphql_url(@uncached_query)) + |> RequestCache.Support.Utils.ensure_default_opts() + |> Absinthe.Plug.put_options(context: %{call_pid: pid}) + |> Router.call([]) assert [] === get_resp_header(conn, RequestCache.Plug.request_cache_header()) end @@ -144,10 +144,10 @@ defmodule RequestCacheAbsintheTest do assert_raise Plug.Conn.WrapperError, fn -> conn = :get - |> conn(graphql_url(@uncached_error_query)) - |> RequestCache.Support.Utils.ensure_default_opts() - |> Absinthe.Plug.put_options(context: %{call_pid: pid}) - |> Router.call([]) + |> conn(graphql_url(@uncached_error_query)) + |> RequestCache.Support.Utils.ensure_default_opts() + |> Absinthe.Plug.put_options(context: %{call_pid: pid}) + |> Router.call([]) assert [] === get_resp_header(conn, RequestCache.Plug.request_cache_header()) end @@ -162,11 +162,11 @@ defmodule RequestCacheAbsintheTest do |> Router.call([]) assert ["HIT"] = :get - |> conn(graphql_url(@cached_all_error_query)) - |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all]) - |> Absinthe.Plug.put_options(context: %{call_pid: pid}) - |> Router.call([]) - |> get_resp_header(RequestCache.Plug.request_cache_header()) + |> conn(graphql_url(@cached_all_error_query)) + |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all]) + |> Absinthe.Plug.put_options(context: %{call_pid: pid}) + |> Router.call([]) + |> get_resp_header(RequestCache.Plug.request_cache_header()) end @tag capture_log: true @@ -178,11 +178,11 @@ defmodule RequestCacheAbsintheTest do |> Router.call([]) assert ["HIT"] = :get - |> conn(graphql_url(@cached_not_found_error_query)) - |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]]) - |> Absinthe.Plug.put_options(context: %{call_pid: pid}) - |> Router.call([]) - |> get_resp_header(RequestCache.Plug.request_cache_header()) + |> conn(graphql_url(@cached_not_found_error_query)) + |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]]) + |> Absinthe.Plug.put_options(context: %{call_pid: pid}) + |> Router.call([]) + |> get_resp_header(RequestCache.Plug.request_cache_header()) end @tag capture_log: true diff --git a/test/request_cache_plug_test.exs b/test/request_cache_plug_test.exs index 9458ca3..28a64a9 100644 --- a/test/request_cache_plug_test.exs +++ b/test/request_cache_plug_test.exs @@ -171,29 +171,27 @@ defmodule RequestCachePlugTest do caller_pid: pid } do route = "/my_route/html" - assert %Plug.Conn{resp_headers: uncached_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts() - |> put_private(:call_pid, pid) - |> Router.call([]) + assert %Plug.Conn{resp_headers: uncached_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts() + |> put_private(:call_pid, pid) + |> Router.call([]) assert uncached_headers === [ {"cache-control", "max-age=0, private, must-revalidate"} ] - assert %Plug.Conn{resp_headers: resp_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts() - |> put_private(:call_pid, pid) - |> Router.call([]) + assert %Plug.Conn{resp_headers: resp_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts() + |> put_private(:call_pid, pid) + |> Router.call([]) assert resp_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"}, - {"rc-cache-status", "HIT"}, - {"content-type", "application/json; charset=utf-8"} - ] + {"cache-control", "max-age=0, private, must-revalidate"}, + {"rc-cache-status", "HIT"}, + {"content-type", "application/json; charset=utf-8"} + ] end @tag capture_log: true @@ -201,113 +199,105 @@ defmodule RequestCachePlugTest do caller_pid: pid } do route = "/my_route/cache" - assert %Plug.Conn{resp_headers: uncached_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts() - |> put_private(:call_pid, pid) - |> Router.call([]) + assert %Plug.Conn{resp_headers: uncached_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts() + |> put_private(:call_pid, pid) + |> Router.call([]) assert uncached_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"} + {"cache-control", "max-age=0, private, must-revalidate"} ] - assert %Plug.Conn{resp_headers: resp_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts() - |> put_private(:call_pid, pid) - |> put_resp_content_type("text/html") - |> Router.call([]) + assert %Plug.Conn{resp_headers: resp_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts() + |> put_private(:call_pid, pid) + |> put_resp_content_type("text/html") + |> Router.call([]) assert resp_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"}, - {"content-type", "text/html; charset=utf-8"}, - {"rc-cache-status", "HIT"} - ] + {"cache-control", "max-age=0, private, must-revalidate"}, + {"content-type", "text/html; charset=utf-8"}, + {"rc-cache-status", "HIT"} + ] end test "doesn't cache errors if error caching not enabled", %{caller_pid: pid} do route = "/error-route/no-error-cache-enabled" - assert %Plug.Conn{resp_headers: uncached_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: []]) - |> put_private(:call_pid, pid) - |> Router.call([]) + assert %Plug.Conn{resp_headers: uncached_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: []]) + |> put_private(:call_pid, pid) + |> Router.call([]) assert uncached_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"} + {"cache-control", "max-age=0, private, must-revalidate"} ] - assert %Plug.Conn{resp_headers: expected_uncached_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts() - |> put_private(:call_pid, pid) - |> Router.call([]) + assert %Plug.Conn{resp_headers: expected_uncached_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts() + |> put_private(:call_pid, pid) + |> Router.call([]) assert expected_uncached_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"} - ] + {"cache-control", "max-age=0, private, must-revalidate"} + ] end test "caches errors if error codes supplied and is error", %{caller_pid: pid} do route = "/error-route/caching-errors-enabled" - assert %Plug.Conn{resp_headers: uncached_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]]) - |> put_private(:call_pid, pid) - |> Router.call([]) + assert %Plug.Conn{resp_headers: uncached_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]]) + |> put_private(:call_pid, pid) + |> Router.call([]) assert uncached_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"} + {"cache-control", "max-age=0, private, must-revalidate"} ] - assert %Plug.Conn{resp_headers: expected_cached_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]]) - |> put_private(:call_pid, pid) - |> put_resp_content_type("text/html") - |> Router.call([]) + assert %Plug.Conn{resp_headers: expected_cached_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: [:not_found]]) + |> put_private(:call_pid, pid) + |> put_resp_content_type("text/html") + |> Router.call([]) assert expected_cached_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"}, - {"content-type", "text/html; charset=utf-8"}, - {"rc-cache-status", "HIT"} - ] + {"cache-control", "max-age=0, private, must-revalidate"}, + {"content-type", "text/html; charset=utf-8"}, + {"rc-cache-status", "HIT"} + ] end test "caches errors if error caching enabled", %{caller_pid: pid} do route = "/error-route/all-errors-enabled" - assert %Plug.Conn{resp_headers: uncached_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all]) - |> put_private(:call_pid, pid) - |> Router.call([]) + assert %Plug.Conn{resp_headers: uncached_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all]) + |> put_private(:call_pid, pid) + |> Router.call([]) assert uncached_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"} + {"cache-control", "max-age=0, private, must-revalidate"} ] - assert %Plug.Conn{resp_headers: expected_cached_headers} = - :get - |> conn(route) - |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all]) - |> put_private(:call_pid, pid) - |> put_resp_content_type("text/html") - |> Router.call([]) + assert %Plug.Conn{resp_headers: expected_cached_headers} = :get + |> conn(route) + |> RequestCache.Support.Utils.ensure_default_opts(request: [cached_errors: :all]) + |> put_private(:call_pid, pid) + |> put_resp_content_type("text/html") + |> Router.call([]) assert expected_cached_headers === [ - {"cache-control", "max-age=0, private, must-revalidate"}, - {"content-type", "text/html; charset=utf-8"}, - {"rc-cache-status", "HIT"} - ] + {"cache-control", "max-age=0, private, must-revalidate"}, + {"content-type", "text/html; charset=utf-8"}, + {"rc-cache-status", "HIT"} + ] end end