/
assert_test.go
170 lines (137 loc) · 3.24 KB
/
assert_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
package assert
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/rkusa/web"
)
func TestOK(t *testing.T) {
// condition evaluates to true
func() {
defer func() {
err := recover()
if err != nil {
t.Errorf("expected err to be nil, got %v", err)
}
}()
OK(true, http.StatusNotFound, "")
}()
// condition evaluates to false - empty message provided
func() {
defer func() {
err := recover().(error)
if err == nil || err.Error() != "Not Found" {
t.Errorf(`expected "Not Found" error, got %s`, err.Error())
}
}()
OK(false, http.StatusNotFound, "")
}()
// condition evaluates to false - with provided message
func() {
defer func() {
err := recover().(error)
if err == nil || err.Error() != "Invalid input" {
t.Errorf(`expected "Invalid input" error, got %s`, err.Error())
}
}()
OK(false, http.StatusBadRequest, "Invalid input")
}()
}
func TestSuccess(t *testing.T) {
// no error
func() {
defer func() {
err := recover()
if err != nil {
t.Errorf("expected err to be nil, got %v", err)
}
}()
Success(nil, http.StatusNotFound, "")
}()
// error - empty message provided
func() {
defer func() {
err := recover().(error)
if err == nil || err.Error() != "Not Found" {
t.Errorf(`expected "Not Found" error, got %s`, err.Error())
}
}()
Success(fmt.Errorf("Fail"), http.StatusNotFound, "")
}()
// error - with provided message
func() {
defer func() {
err := recover().(error)
if err == nil || err.Error() != "Invalid input" {
t.Errorf(`expected "Invalid input" error, got %s`, err.Error())
}
}()
Success(fmt.Errorf("Fail"), http.StatusBadRequest, "Invalid input")
}()
}
func TestError(t *testing.T) {
// no error
func() {
defer func() {
err := recover()
if err != nil {
t.Errorf("expected err to be nil, got %v", err)
}
}()
Error(nil)
}()
// error
func() {
defer func() {
err := recover().(error)
if err == nil || err.Error() != "Fail" {
t.Errorf(`expected "Fail" error, got %s`, err.Error())
}
}()
Error(fmt.Errorf("Fail"))
}()
}
func TestThrow(t *testing.T) {
func() {
defer func() {
err := recover().(error)
if err == nil || err.Error() != "Invalid input" {
t.Errorf(`expected "Invalid input" error, got %s`, err.Error())
}
}()
Throw(http.StatusBadRequest, "Invalid input")
}()
}
func TestOnError(t *testing.T) {
called := false
defer func() {
err := recover().(error)
if err == nil || err.Error() != "Bad Request" {
t.Errorf(`expected "Bad Request" error, got %s`, err.Error())
}
if !called {
t.Errorf("OnError was not called")
}
}()
as := New()
as.OnError(func() {
called = true
})
as.OK(false, http.StatusBadRequest, "")
}
func TestMiddleware(t *testing.T) {
app := web.New()
app.Use(Middleware(nil))
app.Use(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
Error(fmt.Errorf("Fail"))
})
rec := httptest.NewRecorder()
app.ServeHTTP(rec, (*http.Request)(nil))
if rec.Code != http.StatusInternalServerError {
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusInternalServerError), http.StatusText(rec.Code))
}
if rec.Body.String() != "Fail\n" {
t.Errorf(`Expected body of "Fail\n", got %s`, rec.Body.String())
}
}