The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/middleware/middleware_test.go

58 lines
1.2 KiB

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/Unknwon/macaron"
"github.com/macaron-contrib/session"
. "github.com/smartystreets/goconvey/convey"
)
type scenarioContext struct {
m *macaron.Macaron
context *Context
resp *httptest.ResponseRecorder
}
func (sc *scenarioContext) PerformGet(url string) {
req, err := http.NewRequest("GET", "/", nil)
So(err, ShouldBeNil)
sc.m.ServeHTTP(sc.resp, req)
}
type scenarioFunc func(c *scenarioContext)
func middlewareScenario(desc string, fn scenarioFunc) {
sc := &scenarioContext{}
sc.m = macaron.New()
sc.m.Use(GetContextHandler())
// mock out gc goroutine
startSessionGC = func() {}
sc.m.Use(Sessioner(&session.Options{}))
sc.m.Get("/", func(c *Context) {
sc.context = c
})
sc.resp = httptest.NewRecorder()
fn(sc)
}
func TestMiddlewareContext(t *testing.T) {
Convey("Given grafana context", t, func() {
middlewareScenario("middleware should add context to injector", func(sc *scenarioContext) {
sc.PerformGet("/")
So(sc.context, ShouldNotBeNil)
})
middlewareScenario("Default middleware should allow get request", func(sc *scenarioContext) {
sc.PerformGet("/")
So(sc.resp.Code, ShouldEqual, 200)
})
})
}