-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbaa_test.go
177 lines (167 loc) · 4.26 KB
/
baa_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package baa
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var b = New()
var r = b.Router()
var c = NewContext(nil, nil, b)
var f = func(c *Context) {}
type newHandler struct{}
func (t *newHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("m3 http.Handler.ServeHTTP", "true")
}
func TestNew(t *testing.T) {
Convey("new baa app", t, func() {
b2 := New()
So(b2, ShouldNotBeNil)
})
Convey("new instance app", t, func() {
b2 := Default()
b3 := Default()
b4 := Instance("new")
So(b2, ShouldEqual, b3)
So(b2, ShouldNotEqual, b4)
})
}
func TestRun(t *testing.T) {
Convey("run baa app", t, func() {
Convey("run baa app normal", func() {
b3 := New()
go b3.Run(":8011")
go b3.RunServer(b3.Server(":8012"))
// go run $GOROOT/src/crypto/tls/generate_cert.go --host localhost
go b3.RunTLS(":8013", "_fixture/cert/cert.pem", "_fixture/cert/key.pem")
go b3.RunTLSServer(b3.Server(":8014"), "_fixture/cert/cert.pem", "_fixture/cert/key.pem")
})
Convey("run baa app error", func() {
b3 := New()
defer func() {
e := recover()
So(e, ShouldNotBeNil)
}()
b3.run(b3.Server(":8015"), "")
})
})
}
func TestServeHTTP(t *testing.T) {
Convey("ServeHTTP", t, func() {
Convey("normal serve", func() {
b.Get("/ok", func(c *Context) {
c.String(200, "ok")
})
w := request("GET", "/ok")
So(w.Code, ShouldEqual, http.StatusOK)
})
Convey("not found serve", func() {
b.Get("/notfound", func(c *Context) {
c.String(200, "ok")
})
w := request("GET", "/notfound2")
So(w.Code, ShouldEqual, http.StatusNotFound)
})
Convey("error serve", func() {
b.SetError(func(err error, c *Context) {
c.Resp.WriteHeader(500)
c.Resp.Write([]byte(err.Error()))
})
b.Get("/error", func(c *Context) {
c.Error(fmt.Errorf("BOMB"))
})
w := request("GET", "/error")
So(w.Code, ShouldEqual, http.StatusInternalServerError)
})
Convey("http error serve", func() {
b2 := New()
b2.errorHandler = nil
b2.Get("/error", func(c *Context) {
c.Error(fmt.Errorf("BOMB"))
})
req, _ := http.NewRequest("GET", "/error", nil)
w := httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusInternalServerError)
})
Convey("http error serve no debug", func() {
b2 := New()
b2.errorHandler = nil
b2.SetDebug(false)
b2.Get("/error", func(c *Context) {
b2.Debug()
c.Error(fmt.Errorf("BOMB"))
})
req, _ := http.NewRequest("GET", "/error", nil)
w := httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusInternalServerError)
})
Convey("Middleware", func() {
b2 := New()
b2.Use(func(c *Context) {
c.Resp.Header().Set("m1", "true")
c.Set("Middleware", "ok")
c.Next()
So(c.Get("Middleware").(string), ShouldEqual, "ok")
})
b2.Use(HandlerFunc(func(c *Context) {
c.Resp.Header().Set("m2", "true")
c.Next()
}))
b2.Use(new(newHandler))
b2.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("m4", "true")
}))
b2.Use(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("m5", "true")
})
b2.Get("/ok", func(c *Context) {
c.String(200, "ok")
})
req, _ := http.NewRequest("GET", "/ok", nil)
w := httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
})
Convey("Break middleware chain", func() {
b2 := New()
b2.Use(func(c *Context) {
c.String(200, "ok1")
c.Break()
c.Next()
})
b2.Use(func(c *Context) {
c.String(200, "ok2")
c.Break()
c.Next()
})
b2.Get("/ok", func(c *Context) {
c.String(200, "ok")
})
req, _ := http.NewRequest("GET", "/ok", nil)
w := httptest.NewRecorder()
b2.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
body, err := ioutil.ReadAll(w.Body)
So(err, ShouldBeNil)
So(string(body), ShouldEqual, "ok1")
})
Convey("Unknow Middleware", func() {
b2 := New()
defer func() {
e := recover()
So(e, ShouldNotBeNil)
}()
b2.Use(func() {})
})
})
}
func request(method, uri string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, uri, nil)
w := httptest.NewRecorder()
b.ServeHTTP(w, req)
return w
}