-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_error_test.go
138 lines (126 loc) · 3.88 KB
/
api_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package brightbox
import (
"context"
"encoding/json"
"errors"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestError(t *testing.T) {
ts, client, err := SetupConnection(
&APIMock{
T: t,
ExpectMethod: "POST",
ExpectURL: "/1.0/api_clients",
ExpectBody: `{"name":"new client"}`,
GiveStatus: 403,
GiveBody: readJSON("api_error_forbidden"),
},
)
defer ts.Close()
assert.NilError(t, err)
name := "new client"
instance, err := client.CreateAPIClient(context.Background(), APIClientOptions{Name: &name})
assert.Error(t, err, "Forbidden: Account limit reached, please contact support for more information")
assert.Assert(t, is.Nil(instance))
apierror := new(APIError)
assert.ErrorType(t, err, apierror)
if errors.As(err, &apierror) {
assert.Equal(t, apierror.StatusCode, 403)
assert.Equal(t, apierror.ErrorName, "Forbidden")
assert.DeepEqual(
t, apierror.Errors,
[]string{"Account limit reached, please contact support for more information"},
)
}
}
func TestAuthError(t *testing.T) {
ts, client, err := SetupConnection(
&APIMock{
T: t,
ExpectMethod: "POST",
ExpectURL: "/1.0/api_clients",
ExpectBody: `{"name":"new client"}`,
GiveStatus: 401,
GiveBody: ``,
},
)
defer ts.Close()
assert.NilError(t, err)
name := "new client"
instance, err := client.CreateAPIClient(context.Background(), APIClientOptions{Name: &name})
assert.ErrorContains(t, err, ": 401 Unauthorized")
assert.Assert(t, is.Nil(instance))
apierror := new(APIError)
assert.ErrorType(t, err, apierror)
if errors.As(err, &apierror) {
assert.Equal(t, apierror.StatusCode, 401)
assert.Equal(t, apierror.ErrorName, "")
assert.Assert(t, is.Len(apierror.Errors, 0))
}
}
func TestParseError(t *testing.T) {
ts, client, err := SetupConnection(
&APIMock{
T: t,
ExpectMethod: "POST",
ExpectURL: "/1.0/api_clients",
ExpectBody: `{"name":"new client"}`,
GiveStatus: 200,
GiveBody: `{"foo": 1,}`,
},
)
defer ts.Close()
assert.NilError(t, err)
name := "new client"
instance, err := client.CreateAPIClient(context.Background(), APIClientOptions{Name: &name})
assert.ErrorContains(t, err, ": invalid character '}' looking for beginning of object key string")
assert.Assert(t, is.Nil(instance))
jsonError := new(json.SyntaxError)
if errors.As(err, &jsonError) {
assert.Error(t, jsonError, "invalid character '}' looking for beginning of object key string")
} else {
assert.ErrorType(t, err, jsonError)
}
}
func TestParseShortfall(t *testing.T) {
ts, client, err := SetupConnection(
&APIMock{
T: t,
ExpectMethod: "POST",
ExpectURL: "/1.0/api_clients",
ExpectBody: `{"name":"new client"}`,
GiveStatus: 200,
GiveBody: `{"id": "1"}{"id": "2"}`,
},
)
defer ts.Close()
assert.NilError(t, err)
name := "new client"
instance, err := client.CreateAPIClient(context.Background(), APIClientOptions{Name: &name})
assert.ErrorContains(t, err, ": Response body has additional unparsed data at position 12")
assert.Assert(t, is.Nil(instance))
}
func TestUnmarshalError(t *testing.T) {
ts, client, err := SetupConnection(
&APIMock{
T: t,
ExpectMethod: "GET",
ExpectURL: "/1.0/servers/srv-testy",
ExpectBody: "",
GiveStatus: 200,
GiveBody: `{"status": "available"}`,
},
)
defer ts.Close()
assert.NilError(t, err)
instance, err := client.Server(context.Background(), "srv-testy")
assert.ErrorContains(t, err, ": json: cannot unmarshal available into Go struct field Server.status of type serverstatus.Enum")
assert.Assert(t, is.Nil(instance))
var unmarshalError *json.UnmarshalTypeError
if errors.As(err, &unmarshalError) {
assert.Equal(t, unmarshalError.Offset, int64(23))
assert.Error(t, unmarshalError, "json: cannot unmarshal available into Go struct field Server.status of type serverstatus.Enum")
}
}