-
Notifications
You must be signed in to change notification settings - Fork 70
/
errors_test.go
191 lines (182 loc) · 5.29 KB
/
errors_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package papi
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/ptr"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
"github.com/stretchr/testify/require"
"github.com/tj/assert"
)
func TestNewError(t *testing.T) {
sess, err := session.New()
require.NoError(t, err)
req, err := http.NewRequestWithContext(
context.TODO(),
http.MethodHead,
"/",
nil)
require.NoError(t, err)
tests := map[string]struct {
response *http.Response
expected *Error
}{
"valid response, status code 500": {
response: &http.Response{
Status: "Internal Server Error",
StatusCode: http.StatusInternalServerError,
Body: ioutil.NopCloser(strings.NewReader(
`{"type":"a","title":"b","detail":"c"}`),
),
Request: req,
},
expected: &Error{
Type: "a",
Title: "b",
Detail: "c",
StatusCode: http.StatusInternalServerError,
},
},
"invalid response body, assign status code": {
response: &http.Response{
Status: "Internal Server Error",
StatusCode: http.StatusInternalServerError,
Body: ioutil.NopCloser(strings.NewReader(
`test`),
),
Request: req,
},
expected: &Error{
Title: "Failed to unmarshal error body. PAPI API failed. Check details for more information.",
Detail: "test",
StatusCode: http.StatusInternalServerError,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := Client(sess).(*papi).Error(test.response)
assert.Equal(t, test.expected, res)
})
}
}
func TestErrorIs(t *testing.T) {
tests := map[string]struct {
err Error
given error
expected bool
}{
"is ErrSBDNotEnabled": {
err: Error{
StatusCode: http.StatusForbidden,
Type: "https://problems.luna.akamaiapis.net/papi/v0/property-version-hostname/default-cert-provisioning-unavailable",
},
given: ErrSBDNotEnabled,
expected: true,
},
"is wrapped ErrSBDNotEnabled": {
err: Error{
StatusCode: http.StatusForbidden,
Type: "https://problems.luna.akamaiapis.net/papi/v0/property-version-hostname/default-cert-provisioning-unavailable",
},
given: fmt.Errorf("oops: %w", ErrSBDNotEnabled),
expected: true,
},
"is ErrDefaultCertLimitReached": {
err: Error{
StatusCode: http.StatusTooManyRequests,
LimitKey: "DEFAULT_CERTS_PER_CONTRACT",
Remaining: ptr.To(0),
},
given: ErrDefaultCertLimitReached,
expected: true,
},
"is not ErrSBDNotEnabled": {
err: Error{
StatusCode: http.StatusTooManyRequests,
LimitKey: "DEFAULT_CERTS_PER_CONTRACT",
Remaining: ptr.To(0),
},
given: ErrSBDNotEnabled,
expected: false,
},
"is not ErrDefaultCertLimitReached": {
err: Error{
StatusCode: http.StatusForbidden,
Type: "https://problems.luna.akamaiapis.net/papi/v0/property-version-hostname/default-cert-provisioning-unavailable",
},
given: ErrDefaultCertLimitReached,
expected: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := test.err.Is(test.given)
assert.Equal(t, test.expected, res)
})
}
}
func TestJsonErrorUnmarshalling(t *testing.T) {
req, err := http.NewRequestWithContext(
context.TODO(),
http.MethodHead,
"/",
nil)
require.NoError(t, err)
tests := map[string]struct {
input *http.Response
expected *Error
}{
"API failure with HTML response": {
input: &http.Response{
Request: req,
Status: "OK",
StatusCode: http.StatusServiceUnavailable,
Body: ioutil.NopCloser(strings.NewReader(`<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>`))},
expected: &Error{
Type: "",
Title: "Failed to unmarshal error body. PAPI API failed. Check details for more information.",
Detail: "<HTML><HEAD>...</HEAD><BODY>...</BODY></HTML>",
StatusCode: http.StatusServiceUnavailable,
},
},
"API failure with plain text response": {
input: &http.Response{
Request: req,
Status: "OK",
StatusCode: http.StatusServiceUnavailable,
Body: ioutil.NopCloser(strings.NewReader("Your request did not succeed as this operation has reached the limit for your account. Please try after 2024-01-16T15:20:55.945Z"))},
expected: &Error{
Type: "",
StatusCode: http.StatusServiceUnavailable,
Title: "Failed to unmarshal error body. PAPI API failed. Check details for more information.",
Detail: "Your request did not succeed as this operation has reached the limit for your account. Please try after 2024-01-16T15:20:55.945Z",
},
},
"API failure with XML response": {
input: &http.Response{
Request: req,
Status: "OK",
StatusCode: http.StatusServiceUnavailable,
Body: ioutil.NopCloser(strings.NewReader(`<Root><Item id="1" name="Example" /></Root>`))},
expected: &Error{
Type: "",
StatusCode: http.StatusServiceUnavailable,
Title: "Failed to unmarshal error body. PAPI API failed. Check details for more information.",
Detail: "<Root><Item id=\"1\" name=\"Example\" /></Root>",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
sess, _ := session.New()
p := papi{
Session: sess,
}
assert.Equal(t, test.expected, p.Error(test.input))
})
}
}