forked from ohookins/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherrors_test.go
72 lines (60 loc) · 2.1 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
package contentful
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNotFoundErrorResponse(t *testing.T) {
var err error
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = fmt.Fprintln(w, readTestData(t, "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(context.TODO(), "unknown-space-id")
require.Error(t, err)
var notFoundError NotFoundError
ok := errors.As(err, ¬FoundError)
assert.True(t, ok)
assert.Equal(t, 404, notFoundError.APIError.res.StatusCode)
assert.Equal(t, "request-id", notFoundError.APIError.err.RequestID)
assert.Equal(t, "The resource could not be found.", notFoundError.APIError.err.Message)
assert.Equal(t, "Error", notFoundError.APIError.err.Sys.Type)
assert.Equal(t, "NotFound", notFoundError.APIError.err.Sys.ID)
}
func TestRateLimitExceededResponse(t *testing.T) {
var err error
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, _ = fmt.Fprintln(w, readTestData(t, "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(context.TODO(), space)
require.Error(t, err)
var rateLimitExceededError RateLimitExceededError
ok := errors.As(err, &rateLimitExceededError)
assert.True(t, ok)
assert.Equal(t, 403, rateLimitExceededError.APIError.res.StatusCode)
assert.Equal(t, "request-id", rateLimitExceededError.APIError.err.RequestID)
assert.Equal(t, "You are creating too many Spaces.", rateLimitExceededError.APIError.err.Message)
assert.Equal(t, "Error", rateLimitExceededError.APIError.err.Sys.Type)
assert.Equal(t, "RateLimitExceeded", rateLimitExceededError.APIError.err.Sys.ID)
}