2017-11-15 06:51:15 -06:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2020-12-03 01:28:54 -06:00
|
|
|
"strings"
|
2017-11-15 06:51:15 -06:00
|
|
|
"testing"
|
|
|
|
|
2019-04-08 06:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
2020-02-28 05:50:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-03-08 08:15:17 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth"
|
2018-11-02 04:49:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2020-12-03 01:28:54 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-11-15 06:51:15 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecoveryMiddleware(t *testing.T) {
|
2020-12-03 01:28:54 -06:00
|
|
|
t.Run("Given an API route that panics", func(t *testing.T) {
|
2018-03-22 06:37:35 -05:00
|
|
|
apiURL := "/api/whatever"
|
2020-12-15 12:09:04 -06:00
|
|
|
recoveryScenario(t, "recovery middleware should return JSON", apiURL, func(t *testing.T, sc *scenarioContext) {
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.handlerFunc = panicHandler
|
2018-03-22 06:37:35 -05:00
|
|
|
sc.fakeReq("GET", apiURL).exec()
|
2020-12-14 08:13:01 -06:00
|
|
|
sc.req.Header.Set("content-type", "application/json")
|
2017-11-15 06:51:15 -06:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
assert.Equal(t, 500, sc.resp.Code)
|
|
|
|
assert.Equal(t, "Internal Server Error - Check the Grafana server logs for the detailed error message.", sc.respJson["message"])
|
|
|
|
assert.True(t, strings.HasPrefix(sc.respJson["error"].(string), "Server Error"))
|
2017-11-15 06:51:15 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
t.Run("Given a non-API route that panics", func(t *testing.T) {
|
2018-03-22 06:37:35 -05:00
|
|
|
apiURL := "/whatever"
|
2020-12-04 04:09:32 -06:00
|
|
|
recoveryScenario(t, "recovery middleware should return html", apiURL, func(t *testing.T, sc *scenarioContext) {
|
2020-12-03 01:28:54 -06:00
|
|
|
sc.handlerFunc = panicHandler
|
2018-03-22 06:37:35 -05:00
|
|
|
sc.fakeReq("GET", apiURL).exec()
|
2017-11-15 06:51:15 -06:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
assert.Equal(t, 500, sc.resp.Code)
|
|
|
|
assert.Equal(t, "text/html; charset=UTF-8", sc.resp.Header().Get("content-type"))
|
2020-12-15 12:09:04 -06:00
|
|
|
assert.Contains(t, sc.resp.Body.String(), "<title>Grafana - Error</title>")
|
2017-11-15 06:51:15 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
func panicHandler(c *models.ReqContext) {
|
2017-11-15 06:51:15 -06:00
|
|
|
panic("Handler has panicked")
|
|
|
|
}
|
|
|
|
|
2019-04-08 06:31:46 -05:00
|
|
|
func recoveryScenario(t *testing.T, desc string, url string, fn scenarioFunc) {
|
2020-12-03 01:28:54 -06:00
|
|
|
t.Run(desc, func(t *testing.T) {
|
2020-12-11 04:44:44 -06:00
|
|
|
cfg := setting.NewCfg()
|
|
|
|
cfg.ErrTemplateName = "error-template"
|
2017-11-15 06:51:15 -06:00
|
|
|
sc := &scenarioContext{
|
2020-12-03 01:28:54 -06:00
|
|
|
t: t,
|
2017-11-15 06:51:15 -06:00
|
|
|
url: url,
|
2020-12-11 04:44:44 -06:00
|
|
|
cfg: cfg,
|
2017-11-15 06:51:15 -06:00
|
|
|
}
|
2018-11-02 04:49:46 -05:00
|
|
|
|
2020-12-03 01:28:54 -06:00
|
|
|
viewsPath, err := filepath.Abs("../../public/views")
|
|
|
|
require.NoError(t, err)
|
2017-11-15 06:51:15 -06:00
|
|
|
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.m = web.New()
|
2020-12-11 04:44:44 -06:00
|
|
|
sc.m.Use(Recovery(cfg))
|
2017-11-15 06:51:15 -06:00
|
|
|
|
2020-12-11 04:44:44 -06:00
|
|
|
sc.m.Use(AddDefaultResponseHeaders(cfg))
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.m.UseMiddleware(web.Renderer(viewsPath, "[[", "]]"))
|
2017-11-15 06:51:15 -06:00
|
|
|
|
2019-03-08 08:15:17 -06:00
|
|
|
sc.userAuthTokenService = auth.NewFakeUserAuthTokenService()
|
2019-04-08 06:31:46 -05:00
|
|
|
sc.remoteCacheService = remotecache.NewFakeStore(t)
|
|
|
|
|
2022-03-30 10:01:24 -05:00
|
|
|
contextHandler := getContextHandler(t, nil, nil, nil)
|
2020-12-11 04:44:44 -06:00
|
|
|
sc.m.Use(contextHandler.Middleware)
|
2017-11-15 06:51:15 -06:00
|
|
|
// mock out gc goroutine
|
2022-04-08 03:33:19 -05:00
|
|
|
sc.m.Use(OrgRedirect(cfg, sc.mockSQLStore))
|
2017-11-15 06:51:15 -06:00
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
sc.defaultHandler = func(c *models.ReqContext) {
|
2017-11-15 06:51:15 -06:00
|
|
|
sc.context = c
|
|
|
|
if sc.handlerFunc != nil {
|
|
|
|
sc.handlerFunc(sc.context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sc.m.Get(url, sc.defaultHandler)
|
|
|
|
|
2020-12-04 04:09:32 -06:00
|
|
|
fn(t, sc)
|
2017-11-15 06:51:15 -06:00
|
|
|
})
|
|
|
|
}
|