-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
75 lines (64 loc) · 1.52 KB
/
error_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
package rack_test
import (
"errors"
"net/http"
"testing"
"github.com/stevecallear/rack"
)
func TestStatusCode(t *testing.T) {
err := errors.New("error")
tests := []struct {
name string
err error
exp int
}{
{
name: "should return 500 if the error is not a status error",
err: err,
exp: http.StatusInternalServerError,
},
{
name: "should return status error codes",
err: rack.WrapError(http.StatusBadRequest, err),
exp: http.StatusBadRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
act := rack.StatusCode(tt.err)
if act != tt.exp {
t.Errorf("got %d, expected %d", act, tt.exp)
}
})
}
}
func TestStatusError_Code(t *testing.T) {
t.Run("should return the status code", func(t *testing.T) {
const exp = http.StatusConflict
err := rack.WrapError(exp, errors.New("error"))
act := err.Code()
if act != exp {
t.Errorf("got %d, expected %d", act, exp)
}
})
}
func TestStatusError_Error(t *testing.T) {
t.Run("should return the wrapped error message", func(t *testing.T) {
const exp = "error"
sut := rack.WrapError(http.StatusBadRequest, errors.New(exp))
act := sut.Error()
if act != exp {
t.Errorf("got %s, expected %s", act, exp)
}
})
}
func TestStatusError_Unwrap(t *testing.T) {
t.Run("should return the wrapped error", func(t *testing.T) {
exp := errors.New("error")
sut := rack.WrapError(http.StatusBadRequest, exp)
act := errors.Unwrap(sut)
if act != exp {
t.Errorf("got %v, expected %v", act, exp)
}
})
}