Refactoring, worked on middleware unit tests, and began thinking about api unit tests, #1921
This commit is contained in:
@@ -10,31 +10,49 @@ import (
|
||||
. "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() {
|
||||
m := macaron.New()
|
||||
m.Use(GetContextHandler())
|
||||
m.Use(Sessioner(&session.Options{}))
|
||||
|
||||
var context *Context
|
||||
|
||||
m.Get("/", func(c *Context) {
|
||||
context = c
|
||||
middlewareScenario("middleware should add context to injector", func(sc *scenarioContext) {
|
||||
sc.PerformGet("/")
|
||||
So(sc.context, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
m.ServeHTTP(resp, req)
|
||||
|
||||
Convey("Should be able to get grafana context in handlers", func() {
|
||||
So(context, ShouldNotBeNil)
|
||||
middlewareScenario("Default middleware should allow get request", func(sc *scenarioContext) {
|
||||
sc.PerformGet("/")
|
||||
So(sc.resp.Code, ShouldEqual, 200)
|
||||
})
|
||||
|
||||
Convey("should return 200", func() {
|
||||
So(resp.Code, ShouldEqual, 200)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user