From e2bce38a7990176733c01ee9e9cc2311ffbf2a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Tue, 17 Sep 2024 10:28:38 +0200 Subject: [PATCH] App Plugins: Allow resource handle to define Cache-Control Header (#92559) --- pkg/middleware/middleware.go | 1 + pkg/middleware/middleware_test.go | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index cefecc50e06..2b32ba7a5fe 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -33,6 +33,7 @@ func AddDefaultResponseHeaders(cfg *setting.Cfg) web.Handler { t := web.NewTree() t.Add("/api/datasources/uid/:uid/resources/*", nil) t.Add("/api/datasources/:id/resources/*", nil) + t.Add("/api/plugins/:id/resources/*", nil) return func(c *web.Context) { c.Resp.Before(func(w web.ResponseWriter) { // if response has already been written, skip. diff --git a/pkg/middleware/middleware_test.go b/pkg/middleware/middleware_test.go index 0dd9533a729..1510e6caab1 100644 --- a/pkg/middleware/middleware_test.go +++ b/pkg/middleware/middleware_test.go @@ -116,7 +116,7 @@ func TestMiddlewareContext(t *testing.T) { assert.Empty(t, sc.resp.Header().Get("Expires")) }) - middlewareScenario(t, "middleware should pass cache-control on resources with private cache control", func(t *testing.T, sc *scenarioContext) { + middlewareScenario(t, "middleware should pass cache-control on datasource resources with private cache control", func(t *testing.T, sc *scenarioContext) { sc = sc.fakeReq("GET", "/api/datasources/1/resources/foo") sc.resp.Header().Add("Cache-Control", "private, max-age=86400") sc.resp.Header().Add("X-Grafana-Cache", "true") @@ -124,7 +124,7 @@ func TestMiddlewareContext(t *testing.T) { assert.Equal(t, "private, max-age=86400", sc.resp.Header().Get("Cache-Control")) }) - middlewareScenario(t, "middleware should not pass cache-control on resources with public cache control", func(t *testing.T, sc *scenarioContext) { + middlewareScenario(t, "middleware should not pass cache-control on datasource resources with public cache control", func(t *testing.T, sc *scenarioContext) { sc = sc.fakeReq("GET", "/api/datasources/1/resources/foo") sc.resp.Header().Add("Cache-Control", "public, max-age=86400, private") sc.resp.Header().Add("X-Grafana-Cache", "true") @@ -132,6 +132,22 @@ func TestMiddlewareContext(t *testing.T) { assert.Equal(t, noStore, sc.resp.Header().Get("Cache-Control")) }) + middlewareScenario(t, "middleware should pass cache-control on plugins resources with private cache control", func(t *testing.T, sc *scenarioContext) { + sc = sc.fakeReq("GET", "/api/plugins/1/resources/foo") + sc.resp.Header().Add("Cache-Control", "private, max-age=86400") + sc.resp.Header().Add("X-Grafana-Cache", "true") + sc.exec() + assert.Equal(t, "private, max-age=86400", sc.resp.Header().Get("Cache-Control")) + }) + + middlewareScenario(t, "middleware should not pass cache-control on plugins resources with public cache control", func(t *testing.T, sc *scenarioContext) { + sc = sc.fakeReq("GET", "/api/plugins/1/resources/foo") + sc.resp.Header().Add("Cache-Control", "public, max-age=86400, private") + sc.resp.Header().Add("X-Grafana-Cache", "true") + sc.exec() + assert.Equal(t, noStore, sc.resp.Header().Get("Cache-Control")) + }) + middlewareScenario(t, "middleware should not add Cache-Control header for requests to datasource proxy API", func( t *testing.T, sc *scenarioContext) { sc.fakeReq("GET", "/api/datasources/proxy/1/test").exec()