-
Notifications
You must be signed in to change notification settings - Fork 7
/
errors_test.go
44 lines (36 loc) · 1.12 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
package appoptics
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"strings"
"testing"
)
var (
ErrorRequestBodyUnauthorized = `{
"errors":{
"request":["This token is not permitted to perform this action on this resource"]
}
}
`
)
func TestErrorResponse_Error(t *testing.T) {
errResp := &ErrorResponse{}
body := strings.NewReader(ErrorRequestBodyUnauthorized)
decoder := json.NewDecoder(body)
decodeErr := decoder.Decode(errResp)
t.Run("it decodes without error", func(t *testing.T) {
require.NoError(t, decodeErr)
})
t.Run("it holds detailed error information", func(t *testing.T) {
v := errResp.Errors.(map[string]interface{})
mapV := v["request"].([]interface{})
sVal := mapV[0].(string)
assert.Equal(t, "This token is not permitted to perform this action on this resource", sVal)
})
t.Run("it places error information in Error() output", func(t *testing.T) {
errResp.Status = "403 Forbidden"
actual := `403 Forbidden - {"request":["This token is not permitted to perform this action on this resource"]}`
assert.Equal(t, errResp.Error(), actual)
} )
}