middleware_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package middleware
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/Unknwon/macaron"
  7. "github.com/macaron-contrib/session"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. type scenarioContext struct {
  11. m *macaron.Macaron
  12. context *Context
  13. resp *httptest.ResponseRecorder
  14. }
  15. func (sc *scenarioContext) PerformGet(url string) {
  16. req, err := http.NewRequest("GET", "/", nil)
  17. So(err, ShouldBeNil)
  18. sc.m.ServeHTTP(sc.resp, req)
  19. }
  20. type scenarioFunc func(c *scenarioContext)
  21. func middlewareScenario(desc string, fn scenarioFunc) {
  22. sc := &scenarioContext{}
  23. sc.m = macaron.New()
  24. sc.m.Use(GetContextHandler())
  25. // mock out gc goroutine
  26. startSessionGC = func() {}
  27. sc.m.Use(Sessioner(&session.Options{}))
  28. sc.m.Get("/", func(c *Context) {
  29. sc.context = c
  30. })
  31. sc.resp = httptest.NewRecorder()
  32. fn(sc)
  33. }
  34. func TestMiddlewareContext(t *testing.T) {
  35. Convey("Given grafana context", t, func() {
  36. middlewareScenario("middleware should add context to injector", func(sc *scenarioContext) {
  37. sc.PerformGet("/")
  38. So(sc.context, ShouldNotBeNil)
  39. })
  40. middlewareScenario("Default middleware should allow get request", func(sc *scenarioContext) {
  41. sc.PerformGet("/")
  42. So(sc.resp.Code, ShouldEqual, 200)
  43. })
  44. })
  45. }