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"
|
|
|
|
|
2022-09-27 06:58:49 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-08-09 01:54:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
|
|
|
"github.com/grafana/grafana/pkg/services/authn/authntest"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2024-01-04 01:00:07 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/licensing"
|
2023-08-09 01:54:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user/usertest"
|
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"
|
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)
|
2023-06-16 10:46:47 -05:00
|
|
|
assert.Equal(t, "Internal Server Error - test error", sc.respJson["message"])
|
2020-12-03 01:28:54 -06:00
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func panicHandler(c *contextmodel.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()
|
2023-12-05 01:34:22 -06:00
|
|
|
cfg.ErrTemplateName = "error"
|
2023-06-16 10:46:47 -05:00
|
|
|
cfg.UserFacingDefaultError = "test error"
|
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()
|
2024-01-04 01:00:07 -06:00
|
|
|
sc.m.UseMiddleware(Recovery(cfg, &licensing.OSSLicensingService{}))
|
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
|
|
|
|
2023-08-09 01:54:52 -05:00
|
|
|
contextHandler := getContextHandler(t, setting.NewCfg(), &authntest.FakeService{ExpectedIdentity: &authn.Identity{}})
|
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
|
2023-08-09 01:54:52 -05:00
|
|
|
sc.m.Use(OrgRedirect(cfg, usertest.NewUserServiceFake()))
|
2017-11-15 06:51:15 -06:00
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
sc.defaultHandler = func(c *contextmodel.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
|
|
|
})
|
|
|
|
}
|