github.com/furusax0621/goa-v1@v1.4.3/middleware_test.go (about) 1 package goa_test 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "context" 8 9 "github.com/goadesign/goa" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("NewMiddleware", func() { 15 var input interface{} 16 var middleware goa.Middleware 17 var mErr error 18 19 JustBeforeEach(func() { 20 middleware, mErr = goa.NewMiddleware(input) 21 }) 22 23 Context("using a goa Middleware", func() { 24 var goaMiddleware goa.Middleware 25 26 BeforeEach(func() { 27 goaMiddleware = func(h goa.Handler) goa.Handler { return h } 28 input = goaMiddleware 29 }) 30 31 It("returns the middleware", func() { 32 Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goaMiddleware))) 33 Ω(mErr).ShouldNot(HaveOccurred()) 34 }) 35 }) 36 37 Context("using a goa middleware func", func() { 38 var goaMiddlewareFunc func(goa.Handler) goa.Handler 39 40 BeforeEach(func() { 41 goaMiddlewareFunc = func(h goa.Handler) goa.Handler { return h } 42 input = goaMiddlewareFunc 43 }) 44 45 It("returns the middleware", func() { 46 Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goa.Middleware(goaMiddlewareFunc)))) 47 Ω(mErr).ShouldNot(HaveOccurred()) 48 }) 49 }) 50 51 Context("with a context", func() { 52 var service *goa.Service 53 var req *http.Request 54 var rw http.ResponseWriter 55 var ctx context.Context 56 57 BeforeEach(func() { 58 service = goa.New("test") 59 ctrl := service.NewController("foo") 60 var err error 61 req, err = http.NewRequest("GET", "/goo", nil) 62 Ω(err).ShouldNot(HaveOccurred()) 63 rw = new(TestResponseWriter) 64 ctx = goa.NewContext(ctrl.Context, rw, req, nil) 65 Ω(goa.ContextResponse(ctx).Status).Should(Equal(0)) 66 }) 67 68 Context("using a goa handler", func() { 69 BeforeEach(func() { 70 var goaHandler goa.Handler = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { 71 service.Send(ctx, 200, "ok") 72 return nil 73 } 74 input = goaHandler 75 }) 76 77 It("wraps it in a middleware", func() { 78 Ω(mErr).ShouldNot(HaveOccurred()) 79 h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { return nil } 80 Ω(middleware(h)(ctx, rw, req)).ShouldNot(HaveOccurred()) 81 Ω(goa.ContextResponse(ctx).Status).Should(Equal(200)) 82 }) 83 }) 84 85 Context("using a goa handler func", func() { 86 BeforeEach(func() { 87 input = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { 88 service.Send(ctx, 200, "ok") 89 return nil 90 } 91 }) 92 93 It("wraps it in a middleware", func() { 94 Ω(mErr).ShouldNot(HaveOccurred()) 95 h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { return nil } 96 Ω(middleware(h)(ctx, rw, req)).ShouldNot(HaveOccurred()) 97 Ω(goa.ContextResponse(ctx).Status).Should(Equal(200)) 98 }) 99 }) 100 101 Context("using a http middleware func", func() { 102 BeforeEach(func() { 103 input = func(h http.Handler) http.Handler { return h } 104 }) 105 106 It("wraps it in a middleware", func() { 107 Ω(mErr).ShouldNot(HaveOccurred()) 108 h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { 109 service.Send(ctx, 200, "ok") 110 return nil 111 } 112 Ω(middleware(h)(ctx, rw, req)).ShouldNot(HaveOccurred()) 113 Ω(goa.ContextResponse(ctx).Status).Should(Equal(200)) 114 }) 115 }) 116 117 Context("using a http handler", func() { 118 BeforeEach(func() { 119 input = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 120 w.WriteHeader(200) 121 w.Write([]byte("ok")) 122 }) 123 }) 124 125 It("wraps it in a middleware", func() { 126 Ω(mErr).ShouldNot(HaveOccurred()) 127 h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { 128 return nil 129 } 130 Ω(middleware(h)(ctx, rw, req)).ShouldNot(HaveOccurred()) 131 Ω(rw.(*TestResponseWriter).Status).Should(Equal(200)) 132 }) 133 }) 134 135 Context("using a http handler func", func() { 136 BeforeEach(func() { 137 input = func(w http.ResponseWriter, r *http.Request) { 138 w.WriteHeader(200) 139 w.Write([]byte("ok")) 140 } 141 }) 142 143 It("wraps it in a middleware", func() { 144 Ω(mErr).ShouldNot(HaveOccurred()) 145 h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { 146 return nil 147 } 148 Ω(middleware(h)(ctx, rw, req)).ShouldNot(HaveOccurred()) 149 Ω(rw.(*TestResponseWriter).Status).Should(Equal(200)) 150 }) 151 }) 152 153 }) 154 })