-
Notifications
You must be signed in to change notification settings - Fork 44
/
client_test.go
178 lines (146 loc) · 4.53 KB
/
client_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package omise_test
import (
"context"
"encoding/json"
"fmt"
"log"
"testing"
. "github.com/omise/omise-go"
"github.com/omise/omise-go/internal"
"github.com/omise/omise-go/internal/testutil"
"github.com/omise/omise-go/operations"
r "github.com/stretchr/testify/require"
)
func TestNewClient(t *testing.T) {
pkey, skey := testutil.Keys()
_, err := NewClient(pkey, skey)
r.NoError(t, err)
_, err = NewClient("", skey)
r.NoError(t, err)
_, err = NewClient(pkey, "")
r.NoError(t, err)
_, err = NewClient("", "")
r.Error(t, err)
r.Equal(t, ErrInvalidKey, err)
_, err = NewClient("123", "123")
r.Error(t, err)
r.Equal(t, ErrInvalidKey, err)
}
func TestClient_Request(t *testing.T) {
pkey, skey := testutil.Keys()
client, err := NewClient(pkey, skey)
client.WithCustomHeaders(map[string]string{
"X-Header-ABC": "ABC",
})
r.NoError(t, err)
// use skey for api.omise.co endpoint
req, err := client.Request(&operations.RetrieveAccount{})
r.NoError(t, err)
user, _, ok := req.BasicAuth()
r.True(t, ok)
r.Equal(t, user, skey)
// use pkey for vault.omise.co endopint
req, err = client.Request(&operations.CreateToken{})
r.NoError(t, err)
user, _, ok = req.BasicAuth()
r.True(t, ok)
r.Equal(t, user, pkey)
// use overridden endopint, if specified
client.Endpoints[internal.API] = "http://api.omise.dev:3000"
client.Endpoints[internal.Vault] = "http://vault.omise.dev:4500"
req, err = client.Request(&operations.RetrieveAccount{})
r.NoError(t, err)
r.Equal(t, "http://api.omise.dev:3000/account", req.URL.String())
req, err = client.Request(&operations.CreateToken{})
r.NoError(t, err)
r.Equal(t, "http://vault.omise.dev:4500/tokens", req.URL.String())
// general request properties
desc := &operations.RetrieveAccount{}
req, err = client.Request(desc)
r.NoError(t, err)
r.Contains(t, req.Header.Get("User-Agent"), "OmiseGo/")
r.Contains(t, req.Header.Get("User-Agent"), "Go/go")
r.Contains(t, req.Header.Get("X-Header-ABC"), "ABC")
r.Equal(t, req.Header.Get("Omise-Version"), internal.Version)
client.GoVersion = "RANDOMXXXVERSION"
req, err = client.Request(desc)
r.NoError(t, err)
r.Contains(t, req.Header.Get("User-Agent"), "Go/RANDOMXXXVERSION")
client.WithUserAgent("OmiseShopify/2.0.0 CheckoutPage/1.0.0")
req, err = client.Request(desc)
r.NoError(t, err)
r.Contains(t, req.Header.Get("User-Agent"), "OmiseShopify/2.0.0 CheckoutPage/1.0.0")
}
func TestClient_Error(t *testing.T) {
client, err := NewClient(testutil.Keys())
r.NoError(t, err)
err = client.Do(nil, &internal.Description{
Endpoint: internal.API,
Method: "GET",
Path: "/definitely_never_found",
})
r.NotNil(t, err)
apiErr, ok := err.(*Error)
r.True(t, ok, "error returned is not *omise.Error.")
r.Equal(t, apiErr.Code, "not_found")
err = client.Do(nil, &internal.Description{
Endpoint: internal.Endpoint("virus_endpoint"),
Method: "GET",
Path: "/",
})
r.NotNil(t, err)
r.IsType(t, ErrInternal(""), err)
}
func TestClient_TransportError(t *testing.T) {
client := testutil.NewFixedClient(t)
err := client.Do(&struct{}{}, &internal.Description{
Endpoint: internal.API,
Method: "GET",
Path: "/malformed",
})
r.NotNil(t, err)
apiErr, ok := err.(*ErrTransport)
r.True(t, ok, "error returned in not *omise.ErrTransport: ")
_, ok = apiErr.Err.(*json.SyntaxError)
r.True(t, ok, "error does not wrap *json.SyntaxError")
r.Contains(t, string(apiErr.Buffer), "not a valid JSON")
}
func TestClient_WithContext(t *testing.T) {
client := testutil.NewFixedClient(t)
client.WithContext(context.Background())
account := &Account{}
client.MustDo(account, &operations.RetrieveAccount{})
r.Equal(t, account.ID, "acct_4yq6tcsyoged5c0ocxd")
}
func TestClient_SetDebug(t *testing.T) {
client := testutil.NewFixedClient(t)
client.SetDebug(true)
account := &Account{}
client.MustDo(account, &operations.RetrieveAccount{})
r.Equal(t, account.ID, "acct_4yq6tcsyoged5c0ocxd")
}
func ExampleClient_Do() {
// gets your API keys
pkey, skey := "pkey_test_4yq6tct0llin5nyyi5l", "skey_test_4yq6tct0lblmed2yp5t"
// creates a client
client, err := NewClient(pkey, skey)
if err != nil {
log.Fatal(err)
}
// creates a charge
charge, create := &Charge{}, &operations.CreateCharge{
Amount: 100000, // ¥10,000
Currency: "jpy",
Card: "tok_1234",
}
// checks for error
if err := client.Do(charge, create); err != nil {
if omiseErr, ok := err.(*Error); ok {
log.Fatal(omiseErr.Code + " " + omiseErr.Message)
} else {
log.Fatal("transport error: " + err.Error())
}
}
// verify
fmt.Printf("authorized charge: %#v\n", charge)
}