-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
96 lines (90 loc) · 2.19 KB
/
errors_test.go
File metadata and controls
96 lines (90 loc) · 2.19 KB
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
package fetch
import (
"errors"
"net/http"
"testing"
"github.com/code-gorilla-au/odize"
)
func TestAPIError_Error(t *testing.T) {
type fields struct {
StatusCode int
StatusText string
Message string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "should return 4xx error",
fields: fields{
StatusCode: http.StatusBadRequest,
StatusText: http.StatusText(http.StatusBadRequest),
Message: "there was an issue with the request",
},
want: "Bad Request: [400]: there was an issue with the request",
},
{
name: "should return 5xx error",
fields: fields{
StatusCode: http.StatusInternalServerError,
StatusText: http.StatusText(http.StatusInternalServerError),
Message: "there was an issue with the request",
},
want: "Internal Server Error: [500]: there was an issue with the request",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := APIError{
StatusCode: tt.fields.StatusCode,
StatusText: tt.fields.StatusText,
Message: tt.fields.Message,
}
if got := e.Error(); got != tt.want {
t.Errorf("APIError.Error() = %v, want %v", got, tt.want)
}
})
}
}
func TestAPIError_Unwrap_should_match(t *testing.T) {
var err *APIError
expectedErr := APIError{
StatusCode: http.StatusBadRequest,
StatusText: http.StatusText(http.StatusBadRequest),
Message: "some issue",
}
var testErr error = &expectedErr
if errors.As(testErr, &err) {
odize.AssertEqual(t, err.Message, expectedErr.Message)
} else {
t.Error("non matching error")
}
}
func TestAPIError_errors_is(t *testing.T) {
fn := func() error {
return &APIError{
StatusCode: http.StatusOK,
}
}
expected := APIError{
StatusCode: http.StatusOK,
}
err := fn()
odize.AssertEqual(t, err.Error(), expected.Error())
}
func TestAPIError_Unwrap(t *testing.T) {
var err *APIError
expectedErr := APIError{
StatusCode: http.StatusBadRequest,
StatusText: http.StatusText(http.StatusBadRequest),
Message: "some issue",
}
var testErr error = &expectedErr
if errors.As(testErr, &err) {
odize.AssertEqual(t, err.Unwrap().Error(), expectedErr.Error())
} else {
t.Error("non matching error")
}
}