-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
114 lines (102 loc) · 2.67 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
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
package rhttp
import (
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"testing"
)
func TestError(t *testing.T) {
errors := []*Error{
ErrBadRequest,
ErrUnauthorized,
ErrForbidden,
ErrNotFound,
ErrConflict,
ErrInternalServer,
ErrServiceUnavailable,
ErrNotImplemented,
}
t.Run("PackageErrorsUseGenericMessages", func(_ *testing.T) {
for _, err := range errors {
expected := http.StatusText(err.StatusCode)
actual := err.Message
if actual != expected {
t.Errorf("Expected message '%s' but got message '%s'", expected, actual)
}
}
})
t.Run("NewError", func(_ *testing.T) {
statusCode := http.StatusBadRequest
message := "test message"
expected := &Error{
StatusCode: statusCode,
Message: message,
}
output := NewError(statusCode, message)
if !reflect.DeepEqual(output, expected) {
t.Errorf("Expected error '%v' but got error '%v'", expected, output)
}
})
t.Run("New", func(_ *testing.T) {
message := "test message"
for _, err := range errors {
expected := &Error{
StatusCode: err.StatusCode,
Message: message,
}
output := err.New(message)
if !reflect.DeepEqual(output, expected) {
t.Errorf("Expected error '%v' but got error '%v'", expected, output)
}
}
})
t.Run("Newf", func(_ *testing.T) {
format := "test format %d %s"
args := []interface{}{1, "x"}
for _, err := range errors {
expected := &Error{
StatusCode: err.StatusCode,
Message: fmt.Sprintf(format, args...),
}
output := err.Newf(format, args...)
if !reflect.DeepEqual(output, expected) {
t.Errorf("Expected error '%v' but got error '%v'", expected, output)
}
}
})
t.Run("Error", func(_ *testing.T) {
for _, err := range errors {
errStr := err.Error()
if !strings.Contains(errStr, strconv.Itoa(err.StatusCode)) {
t.Errorf("Expected error string '%s' to contain the status code '%d'", errStr, err.StatusCode)
}
}
})
t.Run("Is", func(_ *testing.T) {
for i, err := range errors {
for j, other := range errors {
output := err.Is(other)
if i == j && !output {
t.Errorf("Expected error '%v' to match error '%v'", other, err)
} else if i != j && output {
t.Errorf("Did not expect error '%v' to match error '%v'", other, err)
}
}
}
})
t.Run("HasStatusCode", func(_ *testing.T) {
for i, err := range errors {
for j, other := range errors {
statusCode := other.StatusCode
output := err.HasStatusCode(statusCode)
if i == j && !output {
t.Errorf("Expected error '%v' to have status code '%d'", err, statusCode)
} else if i != j && output {
t.Errorf("Did not expect error '%v' to have status code '%d'", err, statusCode)
}
}
}
})
}