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