forked from ionos-enterprise/ionos-enterprise-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
123 lines (101 loc) · 4.03 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
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
package profitbricks
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/suite"
"github.com/stretchr/testify/assert"
)
func newApiError(code int) error {
return ApiError{
HTTPStatus: code,
}
}
func TestIsStatusOk(t *testing.T) {
assert.True(t, IsStatusOK(newApiError(http.StatusOK)))
assert.False(t, IsStatusOK(newApiError(http.StatusTeapot)))
}
func TestIsStatusAccepted(t *testing.T) {
assert.True(t, IsStatusAccepted(newApiError(http.StatusAccepted)))
assert.False(t, IsStatusAccepted(newApiError(http.StatusTeapot)))
}
func TestIsStatusNotModified(t *testing.T) {
assert.True(t, IsStatusNotModified(newApiError(http.StatusNotModified)))
assert.False(t, IsStatusNotModified(newApiError(http.StatusTeapot)))
}
func TestIsStatusBadRequest(t *testing.T) {
assert.True(t, IsStatusBadRequest(newApiError(http.StatusBadRequest)))
assert.False(t, IsStatusBadRequest(newApiError(http.StatusTeapot)))
}
func TestIsStatusUnauthorized(t *testing.T) {
assert.True(t, IsStatusUnauthorized(newApiError(http.StatusUnauthorized)))
assert.False(t, IsStatusUnauthorized(newApiError(http.StatusTeapot)))
}
func TestIsStatusForbidden(t *testing.T) {
assert.True(t, IsStatusForbidden(newApiError(http.StatusForbidden)))
assert.False(t, IsStatusForbidden(newApiError(http.StatusTeapot)))
}
func TestIsStatusNotFoundError(t *testing.T) {
assert.True(t, IsStatusNotFound(newApiError(http.StatusNotFound)))
assert.False(t, IsStatusNotFound(newApiError(http.StatusTeapot)))
}
func TestIsStatusMethodNotAllowed(t *testing.T) {
assert.True(t, IsStatusMethodNotAllowed(newApiError(http.StatusMethodNotAllowed)))
assert.False(t, IsStatusMethodNotAllowed(newApiError(http.StatusTeapot)))
}
func TestIsStatusUnsupportedMediaType(t *testing.T) {
assert.True(t, IsStatusUnsupportedMediaType(newApiError(http.StatusUnsupportedMediaType)))
assert.False(t, IsStatusUnsupportedMediaType(newApiError(http.StatusTeapot)))
}
func TestIsStatusUnprocessableEntity(t *testing.T) {
assert.True(t, IsStatusUnprocessableEntity(newApiError(http.StatusUnprocessableEntity)))
assert.False(t, IsStatusUnprocessableEntity(newApiError(http.StatusTeapot)))
}
func TestIsStatusTooManyRequests(t *testing.T) {
assert.True(t, IsStatusTooManyRequests(newApiError(http.StatusTooManyRequests)))
assert.False(t, IsStatusTooManyRequests(newApiError(http.StatusTeapot)))
}
func TestIsRequestFailed(t *testing.T) {
assert.True(t, IsRequestFailed(ClientError{errType: RequestFailed, msg: "fail"}))
assert.False(t, IsRequestFailed(ClientError{errType: ClientErrorType(-1), msg: "fail"}))
}
type ErrorSuite struct {
ClientBaseSuite
}
func Test_ClientError(t *testing.T) {
suite.Run(t, new(ErrorSuite))
}
func (s *ErrorSuite) Test_ApiError() {
body := []byte(`{"httpStatus" : 401, "messages" : [ {"errorCode" : "315", "message" : "Unauthorized" } ] }`)
rsp := makeJsonResponse(http.StatusUnauthorized, body)
httpmock.RegisterResponder(http.MethodGet, "=~/datacenters", httpmock.ResponderFromResponse(rsp))
_, err := s.c.ListDatacenters()
s.Error(err)
s.True(IsStatusUnauthorized(err))
s.Equal(1, httpmock.GetTotalCallCount())
}
func (s *ErrorSuite) Test_BadGatewayError() {
body := []byte("<html><body>Service temporarily not available</body></html>")
mRsp := &http.Response{
Header: http.Header{},
StatusCode: http.StatusBadGateway,
Body: ioutil.NopCloser(bytes.NewReader(body)),
Status: http.StatusText(http.StatusBadGateway),
}
mRsp.Header.Set("Content-Type", "text/html")
s.c.SetRetryCount(0)
httpmock.RegisterResponder(http.MethodGet, "=~/datacenters", httpmock.ResponderFromResponse(mRsp))
_, err := s.c.ListDatacenters()
s.Error(err)
s.Equal(body, err.(ApiError).Body())
s.Equal(1, httpmock.GetTotalCallCount())
}
func TestIsHttpStatusWrapped(t *testing.T) {
assert.True(t, IsHttpStatus(fmt.Errorf("wrapped: %w", newApiError(http.StatusOK)), http.StatusOK))
}
func TestIsClientErrorTypeWrapped(t *testing.T) {
assert.True(t, IsClientErrorType(fmt.Errorf("wrapped: %w", NewClientError(InvalidInput, "")), InvalidInput))
}