github.com/shogo82148/goa-v1@v1.6.2/middleware_test.go (about)

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