github.com/ManabuSeki/goa-v1@v1.4.3/mux_test.go (about) 1 package goa_test 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "net/url" 8 9 "github.com/goadesign/goa" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Mux", func() { 15 var mux goa.ServeMux 16 17 var req *http.Request 18 var rw *TestResponseWriter 19 20 BeforeEach(func() { 21 mux = goa.NewMux() 22 }) 23 24 JustBeforeEach(func() { 25 rw = &TestResponseWriter{ParentHeader: http.Header{}} 26 mux.ServeHTTP(rw, req) 27 }) 28 29 Context("with no handler", func() { 30 BeforeEach(func() { 31 var err error 32 req, err = http.NewRequest("GET", "/", nil) 33 Ω(err).ShouldNot(HaveOccurred()) 34 }) 35 It("returns 404 to all requests", func() { 36 Ω(rw.Status).Should(Equal(404)) 37 }) 38 }) 39 40 Context("with registered handlers", func() { 41 const reqMeth = "POST" 42 const reqPath = "/foo" 43 const reqBody = "some body" 44 45 var readMeth, readPath, readBody string 46 47 BeforeEach(func() { 48 var body bytes.Buffer 49 body.WriteString(reqBody) 50 var err error 51 req, err = http.NewRequest(reqMeth, reqPath, &body) 52 Ω(err).ShouldNot(HaveOccurred()) 53 mux.Handle(reqMeth, reqPath, func(rw http.ResponseWriter, req *http.Request, vals url.Values) { 54 b, err := ioutil.ReadAll(req.Body) 55 Ω(err).ShouldNot(HaveOccurred()) 56 readPath = req.URL.Path 57 readMeth = req.Method 58 readBody = string(b) 59 }) 60 }) 61 62 It("handles requests", func() { 63 Ω(readMeth).Should(Equal(reqMeth)) 64 Ω(readPath).Should(Equal(reqPath)) 65 Ω(readBody).Should(Equal(reqBody)) 66 }) 67 }) 68 69 Context("with registered handlers and wrong method", func() { 70 const handlerMeth = "POST" 71 const reqMeth = "GET" 72 const reqPath = "/foo" 73 74 BeforeEach(func() { 75 var err error 76 req, err = http.NewRequest(reqMeth, reqPath, nil) 77 Ω(err).ShouldNot(HaveOccurred()) 78 mux.Handle(handlerMeth, reqPath, func(rw http.ResponseWriter, req *http.Request, vals url.Values) {}) 79 }) 80 81 It("returns 405 to not allowed method", func() { 82 Ω(rw.Status).Should(Equal(405)) 83 }) 84 }) 85 86 })