Describe("Request", func() { It("returns nil if not initialized", func() { Ω(ctx.Request()).Should(BeNil()) }) }) Describe("Header", func() { It("returns nil if not initialized", func() { Ω(ctx.Header()).Should(BeNil()) }) }) Describe("ResponseStatus", func() { It("returns 0 if not initialized", func() { Ω(ctx.ResponseStatus()).Should(Equal(0)) }) }) Describe("ResponseLength", func() { It("returns 0 if not initialized", func() { Ω(ctx.ResponseLength()).Should(Equal(0)) }) }) Describe("Get", func() { It(`returns "", false if not initialized`, func() { p := ctx.Get("foo") Ω(p).Should(Equal("")) }) })
Ω(mErr).ShouldNot(HaveOccurred()) }) }) Context("with a context", func() { var service goa.Service var ctx *goa.Context BeforeEach(func() { service = goa.New("test") req, err := http.NewRequest("GET", "/goo", nil) Ω(err).ShouldNot(HaveOccurred()) rw := new(testResponseWriter) params := url.Values{"foo": []string{"bar"}} ctx = goa.NewContext(nil, service, req, rw, params) Ω(ctx.ResponseStatus()).Should(Equal(0)) }) Context("using a goa handler", func() { BeforeEach(func() { var goaHandler goa.Handler = func(ctx *goa.Context) error { ctx.Respond(200, "ok") return nil } input = goaHandler }) It("wraps it in a middleware", func() { Ω(mErr).ShouldNot(HaveOccurred()) h := func(ctx *goa.Context) error { return nil } Ω(middleware(h)(ctx)).ShouldNot(HaveOccurred())
s := goa.New("test") s.SetEncoder(goa.JSONEncoderFactory(), true, "*/*") ctx = goa.NewContext(nil, s, req, rw, nil) spec = &httpauth.Specification{ ValidationProvider: validFunc, } }) It("handles valid credentials", func() { req.Header.Add("Authorization", authString) auth := httpauth.BasicMiddleware(spec)(handler) Ω(auth(ctx)).ShouldNot(HaveOccurred()) Ω(ctx.ResponseStatus()).Should(Equal(http.StatusOK)) Ω(rw.Body).Should(Equal([]byte("\"ok\"\n"))) }) It("handles invalid credentials", func() { auth := httpauth.BasicMiddleware(spec)(handler) Ω(auth(ctx)).ShouldNot(HaveOccurred()) Ω(ctx.ResponseStatus()).Should(Equal(http.StatusUnauthorized)) Ω(ctx.Header()).Should(HaveKey("Www-Authenticate")) Ω(ctx.Header().Get("Www-Authenticate")).Should(Equal(`Basic realm="Restricted"`)) Ω(rw.Body).Should(ContainSubstring("Unauthorized")) }) It("sets a custom realm", func() { spec.Realm = "Custom"
handler = new(testHandler) logger := log15.New("test", "test") logger.SetHandler(handler) ctx.Logger = logger }) It("encodes response using gzip", func() { h := func(ctx *goa.Context) error { ctx.Write([]byte("gzip me!")) ctx.WriteHeader(http.StatusOK) return nil } t := gzm.Middleware(gzip.BestCompression)(h) err := t(ctx) Ω(err).ShouldNot(HaveOccurred()) Ω(ctx.ResponseStatus()).Should(Equal(http.StatusOK)) gzr, err := gzip.NewReader(bytes.NewReader(rw.Body)) Ω(err).ShouldNot(HaveOccurred()) buf := bytes.NewBuffer(nil) io.Copy(buf, gzr) Ω(err).ShouldNot(HaveOccurred()) Ω(buf.String()).Should(Equal("gzip me!")) }) }) type testHandler struct { Records []*log15.Record }
Ω(mErr).ShouldNot(HaveOccurred()) }) }) Context("with a context", func() { var service goa.Service var ctx *goa.Context BeforeEach(func() { service = goa.New("test") req, err := http.NewRequest("GET", "/goo", nil) Ω(err).ShouldNot(HaveOccurred()) rw := new(TestResponseWriter) params := url.Values{"foo": []string{"bar"}} ctx = goa.NewContext(nil, service, req, rw, params) Ω(ctx.ResponseStatus()).Should(Equal(0)) }) Context("using a goa handler", func() { BeforeEach(func() { var goaHandler goa.Handler = func(ctx *goa.Context) error { ctx.Respond(200, "ok") return nil } input = goaHandler }) It("wraps it in a middleware", func() { Ω(mErr).ShouldNot(HaveOccurred()) h := func(ctx *goa.Context) error { return nil } Ω(middleware(h)(ctx)).ShouldNot(HaveOccurred())