forked from contentful-labs/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
71 lines (59 loc) · 1.99 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
package contentful
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNotFoundErrorResponse(t *testing.T) {
var err error
assert := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
fmt.Fprintln(w, string(readTestData("error-notfound.json")))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
// test space
_, err = cma.Spaces.Get("unknown-space-id")
assert.NotNil(err)
_, ok := err.(NotFoundError)
assert.Equal(true, ok)
notFoundError := err.(NotFoundError)
assert.Equal(404, notFoundError.APIError.res.StatusCode)
assert.Equal("request-id", notFoundError.APIError.err.RequestID)
assert.Equal("The resource could not be found.", notFoundError.APIError.err.Message)
assert.Equal("Error", notFoundError.APIError.err.Sys.Type)
assert.Equal("NotFound", notFoundError.APIError.err.Sys.ID)
}
func TestRateLimitExceededResponse(t *testing.T) {
var err error
assert := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(403)
fmt.Fprintln(w, string(readTestData("error-ratelimit.json")))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
// test space
space := &Space{Name: "test-space"}
err = cma.Spaces.Upsert(space)
assert.NotNil(err)
_, ok := err.(RateLimitExceededError)
assert.Equal(true, ok)
rateLimitExceededError := err.(RateLimitExceededError)
assert.Equal(403, rateLimitExceededError.APIError.res.StatusCode)
assert.Equal("request-id", rateLimitExceededError.APIError.err.RequestID)
assert.Equal("You are creating too many Spaces.", rateLimitExceededError.APIError.err.Message)
assert.Equal("Error", rateLimitExceededError.APIError.err.Sys.Type)
assert.Equal("RateLimitExceeded", rateLimitExceededError.APIError.err.Sys.ID)
}