recovery_test.go 2.39 KB
Newer Older
1 2 3 4 5 6 7
package middleware

import (
	"path/filepath"
	"testing"

	"github.com/grafana/grafana/pkg/bus"
8
	"github.com/grafana/grafana/pkg/infra/remotecache"
9
	m "github.com/grafana/grafana/pkg/models"
10
	"github.com/grafana/grafana/pkg/services/auth"
11
	"github.com/grafana/grafana/pkg/setting"
12
	. "github.com/smartystreets/goconvey/convey"
13
	macaron "gopkg.in/macaron.v1"
14 15 16
)

func TestRecoveryMiddleware(t *testing.T) {
17 18
	setting.ERR_TEMPLATE_NAME = "error-template"

19
	Convey("Given an api route that panics", t, func() {
20
		apiURL := "/api/whatever"
21
		recoveryScenario(t, "recovery middleware should return json", apiURL, func(sc *scenarioContext) {
22
			sc.handlerFunc = PanicHandler
23
			sc.fakeReq("GET", apiURL).exec()
24 25 26 27 28 29 30 31 32
			sc.req.Header.Add("content-type", "application/json")

			So(sc.resp.Code, ShouldEqual, 500)
			So(sc.respJson["message"], ShouldStartWith, "Internal Server Error - Check the Grafana server logs for the detailed error message.")
			So(sc.respJson["error"], ShouldStartWith, "Server Error")
		})
	})

	Convey("Given a non-api route that panics", t, func() {
33
		apiURL := "/whatever"
34
		recoveryScenario(t, "recovery middleware should return html", apiURL, func(sc *scenarioContext) {
35
			sc.handlerFunc = PanicHandler
36
			sc.fakeReq("GET", apiURL).exec()
37 38 39 40 41 42 43 44

			So(sc.resp.Code, ShouldEqual, 500)
			So(sc.resp.Header().Get("content-type"), ShouldEqual, "text/html; charset=UTF-8")
			So(sc.resp.Body.String(), ShouldContainSubstring, "<title>Grafana - Error</title>")
		})
	})
}

45
func PanicHandler(c *m.ReqContext) {
46 47 48
	panic("Handler has panicked")
}

49
func recoveryScenario(t *testing.T, desc string, url string, fn scenarioFunc) {
50 51 52 53 54 55
	Convey(desc, func() {
		defer bus.ClearBusHandlers()

		sc := &scenarioContext{
			url: url,
		}
56

57 58 59 60 61
		viewsPath, _ := filepath.Abs("../../public/views")

		sc.m = macaron.New()
		sc.m.Use(Recovery())

62
		sc.m.Use(AddDefaultResponseHeaders())
63 64 65 66 67
		sc.m.Use(macaron.Renderer(macaron.RenderOptions{
			Directory: viewsPath,
			Delims:    macaron.Delims{Left: "[[", Right: "]]"},
		}))

68
		sc.userAuthTokenService = auth.NewFakeUserAuthTokenService()
69 70 71
		sc.remoteCacheService = remotecache.NewFakeStore(t)

		sc.m.Use(GetContextHandler(sc.userAuthTokenService, sc.remoteCacheService))
72 73 74
		// mock out gc goroutine
		sc.m.Use(OrgRedirect())

75
		sc.defaultHandler = func(c *m.ReqContext) {
76 77 78 79 80 81 82 83 84 85 86
			sc.context = c
			if sc.handlerFunc != nil {
				sc.handlerFunc(sc.context)
			}
		}

		sc.m.Get(url, sc.defaultHandler)

		fn(sc)
	})
}