-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patherror_test.go
78 lines (70 loc) · 2.16 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
package igdb
import (
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/pkg/errors"
)
const testErrNotFound = `
{
"status": 404,
"message": "status not found"
}
`
func TestCheckResponse(t *testing.T) {
var tests = []struct {
name string
code int
body string
wantErr error
}{
{"Status OK", http.StatusOK, "", nil},
{"Status Bad Request", http.StatusBadRequest, "", ErrBadRequest},
{"Status Unauthorized", http.StatusUnauthorized, "", ErrUnauthorized},
{"Status Forbidden", http.StatusForbidden, "", ErrForbidden},
{"Status Internal Server Error", http.StatusInternalServerError, "", ErrInternalError},
{"Status Too Many Requests", http.StatusTooManyRequests, "", ErrManyRequests},
{"Unexpected Status Not Found", http.StatusNotFound, testErrNotFound, ServerError{Status: 404, Msg: "status not found"}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
resp := &http.Response{StatusCode: test.code,
Body: ioutil.NopCloser(strings.NewReader(test.body)),
}
err := checkResponse(resp)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
})
}
}
func TestIsBracketPair(t *testing.T) {
tests := []struct {
name string
b []byte
wantBool bool
}{
{"Nil slice", nil, false},
{"Empty byte slice", []byte{}, false},
{"Single open bracket", []byte{91}, false},
{"Single closed bracket", []byte{93}, false},
{"Double open bracket", []byte{91, 91}, false},
{"Double closed bracket", []byte{93, 93}, false},
{"Triple open bracket", []byte{91, 91, 91}, false},
{"Triple closed bracket", []byte{93, 93, 93}, false},
{"Reversed bracket pair", []byte{93, 92}, false},
{"Proper bracket pair with leading bracket", []byte{91, 91, 93}, false},
{"Proper bracket pair with trailing bracket", []byte{91, 93, 93}, false},
{"Double proper bracket pair", []byte{91, 93, 91, 93}, false},
{"Proper bracket pair", []byte{91, 93}, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := isBracketPair(test.b)
if got != test.wantBool {
t.Errorf("got: <%v>, want: <%v>", got, test.wantBool)
}
})
}
}