forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
36 lines (29 loc) · 959 Bytes
/
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
package stripe
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
assert "github.com/stretchr/testify/require"
)
func TestErrorError(t *testing.T) {
err := &Error{Type: "foo", Msg: "bar"}
assert.Equal(t, `{"message":"bar","type":"foo"}`, err.Error())
}
func TestErrorResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Request-Id", "req_123")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, `{"error":{"message":"bar","type":"`+ErrorTypeInvalidRequest+`"}}`)
}))
defer ts.Close()
backend := GetBackendWithConfig(APIBackend, &BackendConfig{
URL: ts.URL,
})
err := backend.Call(http.MethodGet, "/v1/account", "sk_test_badKey", nil, nil)
assert.Error(t, err)
stripeErr := err.(*Error)
assert.Equal(t, ErrorTypeInvalidRequest, stripeErr.Type)
assert.Equal(t, "req_123", stripeErr.RequestID)
assert.Equal(t, 401, stripeErr.HTTPStatusCode)
}