mirror of
https://github.com/grafana/grafana.git
synced 2025-01-10 08:03:58 -06:00
Plugins: Fix Cache-Control header response for public/plugins/* assets API (#64051)
* fix caching * fixeroo * simplify * undo changes * fix test
This commit is contained in:
parent
b9ebd5757a
commit
88666f9bbd
@ -37,7 +37,8 @@ func AddDefaultResponseHeaders(cfg *setting.Cfg) web.Handler {
|
||||
|
||||
_, _, resourceURLMatch := t.Match(c.Req.URL.Path)
|
||||
resourceCachable := resourceURLMatch && allowCacheControl(c.Resp)
|
||||
if !strings.HasPrefix(c.Req.URL.Path, "/api/datasources/proxy/") && !resourceCachable {
|
||||
if !strings.HasPrefix(c.Req.URL.Path, "/public/plugins/") &&
|
||||
!strings.HasPrefix(c.Req.URL.Path, "/api/datasources/proxy/") && !resourceCachable {
|
||||
addNoCacheHeaders(c.Resp)
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/user/userimpl"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tests/testinfra"
|
||||
)
|
||||
|
||||
@ -117,6 +118,57 @@ func TestIntegrationPlugins(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegrationPluginAssets(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
|
||||
type testCase struct {
|
||||
desc string
|
||||
url string
|
||||
env string
|
||||
expStatus int
|
||||
expCacheControl string
|
||||
}
|
||||
t.Run("Assets", func(t *testing.T) {
|
||||
testCases := []testCase{
|
||||
{
|
||||
desc: "should return no-cache settings for Dev env",
|
||||
env: setting.Dev,
|
||||
url: "http://%s/public/plugins/testdata/img/testdata.svg",
|
||||
expStatus: http.StatusOK,
|
||||
expCacheControl: "max-age=0, must-revalidate, no-cache",
|
||||
},
|
||||
{
|
||||
desc: "should return cache settings for Prod env",
|
||||
env: setting.Prod,
|
||||
url: "http://%s/public/plugins/testdata/img/testdata.svg",
|
||||
expStatus: http.StatusOK,
|
||||
expCacheControl: "public, max-age=3600",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
dir, cfgPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
|
||||
AppModeProduction: tc.env == setting.Prod,
|
||||
})
|
||||
|
||||
grafanaListedAddr, _ := testinfra.StartGrafana(t, dir, cfgPath)
|
||||
url := fmt.Sprintf(tc.url, grafanaListedAddr)
|
||||
// nolint:gosec
|
||||
resp, err := http.Get(url)
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, resp.Body.Close())
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expStatus, resp.StatusCode)
|
||||
require.Equal(t, tc.expCacheControl, resp.Header.Get("Cache-Control"))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserCommand) {
|
||||
t.Helper()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user