App Plugins: Allow resource handle to define Cache-Control Header (#92559)

This commit is contained in:
Jan-Otto Kröpke 2024-09-17 10:28:38 +02:00 committed by GitHub
parent 4f21ecf982
commit e2bce38a79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -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.

View File

@ -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()