-
Notifications
You must be signed in to change notification settings - Fork 70
/
errors_test.go
77 lines (72 loc) · 2.37 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
package botman
import (
"context"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
"github.com/stretchr/testify/require"
"github.com/tj/assert"
)
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. Bot Manager 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: "",
Title: "Failed to unmarshal error body. Bot Manager 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",
StatusCode: http.StatusServiceUnavailable,
},
},
"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: "",
Title: "Failed to unmarshal error body. Bot Manager API failed. Check details for more information.",
Detail: "<Root><Item id=\"1\" name=\"Example\" /></Root>",
StatusCode: http.StatusServiceUnavailable,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
sess, _ := session.New()
b := botman{
Session: sess,
}
assert.Equal(t, test.expected, b.Error(test.input))
})
}
}