This repository was archived by the owner on Mar 29, 2025. It is now read-only.
forked from contentful-labs/contentful-go
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patherrors_test.go
More file actions
149 lines (121 loc) · 5.06 KB
/
Copy patherrors_test.go
File metadata and controls
149 lines (121 loc) · 5.06 KB
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
package contentful
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/labd/contentful-go/pkgs/common"
"github.com/stretchr/testify/assert"
)
func TestNotFoundError_Error(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, readTestData("error_notfound.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cmaClient client
cmaClient = NewCMA(CMAToken)
cmaClient.BaseURL = server.URL
// test space
_, err = cmaClient.Spaces.Get("unknown-space-id")
assertions.NotNil(err)
_, ok := err.(common.NotFoundError)
assertions.Equal(true, ok)
notFoundError := err.(common.NotFoundError)
assertions.Equal("the requested resource can not be found", notFoundError.Error())
refVal := reflect.ValueOf(¬FoundError.APIError).Elem()
assertions.Equal(int64(404), refVal.FieldByName("res").Elem().FieldByName("StatusCode").Int())
assertions.Equal("request-id", notFoundError.APIError.Err.RequestID)
assertions.Equal("The resource could not be found.", notFoundError.APIError.Err.Message)
assertions.Equal("Error", notFoundError.APIError.Err.Sys.Type)
assertions.Equal("NotFound", notFoundError.APIError.Err.Sys.ID)
}
func TestRateLimitExceededError_Error(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(403)
_, _ = fmt.Fprintln(w, readTestData("error_ratelimit.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cmaClient client
cmaClient = NewCMA(CMAToken)
cmaClient.BaseURL = server.URL
// test space
space := &Space{Name: "test-space"}
err = cmaClient.Spaces.Upsert(space)
assertions.NotNil(err)
_, ok := err.(common.RateLimitExceededError)
assertions.Equal(true, ok)
rateLimitExceededError := err.(common.RateLimitExceededError)
assertions.Equal("You are creating too many Spaces.", rateLimitExceededError.Error())
refVal := reflect.ValueOf(&rateLimitExceededError.APIError).Elem()
assertions.Equal(int64(403), refVal.FieldByName("res").Elem().FieldByName("StatusCode").Int())
assertions.Equal("request-id", rateLimitExceededError.APIError.Err.RequestID)
assertions.Equal("You are creating too many Spaces.", rateLimitExceededError.APIError.Err.Message)
assertions.Equal("Error", rateLimitExceededError.APIError.Err.Sys.Type)
assertions.Equal("RateLimitExceeded", rateLimitExceededError.APIError.Err.Sys.ID)
}
func TestAccessTokenInvalidError_Error(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(401)
_, _ = fmt.Fprintln(w, readTestData("error_accesstoken_invalid.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cmaClient client
cmaClient = NewCMA(CMAToken)
cmaClient.BaseURL = server.URL
// test space
space := &Space{Name: "test-space"}
err = cmaClient.Spaces.Upsert(space)
assertions.NotNil(err)
_, ok := err.(common.AccessTokenInvalidError)
assertions.Equal(true, ok)
accessTokenInvalidError := err.(common.AccessTokenInvalidError)
assertions.Equal("The access token you sent could not be found or is invalid.", accessTokenInvalidError.Error())
refVal := reflect.ValueOf(&accessTokenInvalidError.APIError).Elem()
assertions.Equal(int64(401), refVal.FieldByName("res").Elem().FieldByName("StatusCode").Int())
assertions.Equal("64adff93598dff78d8494c9d520990", accessTokenInvalidError.APIError.Err.RequestID)
assertions.Equal("The access token you sent could not be found or is invalid.", accessTokenInvalidError.APIError.Err.Message)
assertions.Equal("Error", accessTokenInvalidError.APIError.Err.Sys.Type)
assertions.Equal("AccessTokenInvalid", accessTokenInvalidError.APIError.Err.Sys.ID)
}
func TestInvalidEntry422_Error(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(422)
_, _ = fmt.Fprintln(w, readTestData("error_unique_422.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cmaClient client
cmaClient = NewCMA(CMAToken)
cmaClient.BaseURL = server.URL
// test space
space := &Space{Name: "test-space"}
err = cmaClient.Spaces.Upsert(space)
assertions.NotNil(err)
_, ok := err.(common.InvalidEntryError)
assertions.Equal(true, ok)
invalidEntryError := err.(common.InvalidEntryError)
assertions.Equal("Same field value present in other entry\n", invalidEntryError.Error())
refVal := reflect.ValueOf(&invalidEntryError.APIError).Elem()
assertions.Equal(int64(422), refVal.FieldByName("res").Elem().FieldByName("StatusCode").Int())
assertions.Equal("23e4333f-8fea-4f56-ac4d-4adeb8159185", invalidEntryError.APIError.Err.RequestID)
assertions.Equal("Validation error", invalidEntryError.APIError.Err.Message)
assertions.Equal("Error", invalidEntryError.APIError.Err.Sys.Type)
assertions.Equal("InvalidEntry", invalidEntryError.APIError.Err.Sys.ID)
}