forked from go-packages/paypal
Fixed #15
This commit is contained in:
parent
959619c327
commit
f65c5dc88e
|
@ -19,12 +19,12 @@ func TestGetAuthorization(t *testing.T) {
|
|||
|
||||
a, err := c.GetAuthorization(testAuthID)
|
||||
if err != nil || a.ID != testAuthID {
|
||||
t.Errorf("GetAuthorization failed for ID=" + testAuthID)
|
||||
t.Errorf("GetAuthorization failed for ID=%s", testAuthID)
|
||||
}
|
||||
|
||||
a, err = c.GetAuthorization(testFakeAuthID)
|
||||
if err == nil {
|
||||
t.Errorf("GetAuthorization must return error for ID=" + testFakeAuthID)
|
||||
t.Errorf("GetAuthorization must return error for ID=%s", testFakeAuthID)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
21
client.go
21
client.go
|
@ -47,6 +47,12 @@ func (c *Client) SetAccessToken(token string) error {
|
|||
// unmarshaled into v, or if v is an io.Writer, the response will
|
||||
// be written to it without decoding
|
||||
func (c *Client) Send(req *http.Request, v interface{}) error {
|
||||
var (
|
||||
err error
|
||||
resp *http.Response
|
||||
data []byte
|
||||
)
|
||||
|
||||
// Set default headers
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Accept-Language", "en_US")
|
||||
|
@ -56,7 +62,7 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
|||
req.Header.Set("Content-type", "application/json")
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
resp, err = c.client.Do(req)
|
||||
c.log(req, resp)
|
||||
|
||||
if err != nil {
|
||||
|
@ -66,7 +72,7 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
|||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
errResp := &ErrorResponse{Response: resp}
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
data, err = ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err == nil && len(data) > 0 {
|
||||
json.Unmarshal(data, errResp)
|
||||
|
@ -90,10 +96,19 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
|
|||
}
|
||||
|
||||
// SendWithAuth makes a request to the API and apply OAuth2 header automatically.
|
||||
// If the access token soon to be expired, it will try to get a new one before
|
||||
// If the access token soon to be expired or already expired, it will try to get a new one before
|
||||
// making the main request
|
||||
// client.Token will be updated when changed
|
||||
func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
|
||||
if c.Token != nil {
|
||||
if c.Token.ExpiresIn < RequestNewTokenBeforeExpiresIn {
|
||||
// c.Token willbe updated in GetAccessToken call
|
||||
_, err := c.GetAccessToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,6 @@ func TestNewClient(t *testing.T) {
|
|||
|
||||
_, err = NewClient(testClientID, testSecret, APIBaseSandBox)
|
||||
if err != nil {
|
||||
t.Errorf("NewClient() must not return error for valid creds: " + err.Error())
|
||||
t.Errorf("NewClient() must not return error for valid creds: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,6 @@ func TestGetUserInfo(t *testing.T) {
|
|||
|
||||
u, err := c.GetUserInfo("openid")
|
||||
if u.ID != testUserID || err != nil {
|
||||
t.Errorf("GetUserInfo must return valid test ID=" + testUserID)
|
||||
t.Errorf("GetUserInfo must return valid test ID=%s", testUserID)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,12 +11,12 @@ func TestGetOrder(t *testing.T) {
|
|||
|
||||
o, err := c.GetOrder(testOrderID)
|
||||
if err != nil || o.ID != testOrderID {
|
||||
t.Errorf("GetOrder failed for ID=" + testOrderID)
|
||||
t.Errorf("GetOrder failed for ID=%s", testOrderID)
|
||||
}
|
||||
|
||||
o, err = c.GetOrder(testFakeOrderID)
|
||||
if err == nil {
|
||||
t.Errorf("GetOrder must return error for ID=" + testFakeOrderID)
|
||||
t.Errorf("GetOrder must return error for ID=%s", testFakeOrderID)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ package paypalsdk
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -42,7 +41,7 @@ func TestGetPayments(t *testing.T) {
|
|||
payments, _ := c.GetPayments()
|
||||
|
||||
if len(payments) != 2 {
|
||||
t.Errorf("2 payments must be returned for GetPayments. Returned: " + strconv.Itoa(len(payments)))
|
||||
t.Errorf("2 payments must be returned for GetPayments. Returned: %d", len(payments))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ func TestGetSale(t *testing.T) {
|
|||
|
||||
_, err := c.GetSale(testSaleID)
|
||||
if err == nil {
|
||||
t.Errorf("404 must be returned for ID=" + testSaleID)
|
||||
t.Errorf("404 must be returned for ID=%s", testSaleID)
|
||||
} else {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
@ -23,14 +23,14 @@ func TestRefundSale(t *testing.T) {
|
|||
|
||||
_, err := c.RefundSale(testSaleID, nil)
|
||||
if err == nil {
|
||||
t.Errorf("404 must be returned for ID=" + testSaleID)
|
||||
t.Errorf("404 must be returned for ID=%s", testSaleID)
|
||||
} else {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
_, err = c.RefundSale(testSaleID, &Amount{Total: "7.00", Currency: "USD"})
|
||||
if err == nil {
|
||||
t.Errorf("404 must be returned for ID=" + testSaleID)
|
||||
t.Errorf("404 must be returned for ID=%s", testSaleID)
|
||||
} else {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ func TestGetRefund(t *testing.T) {
|
|||
|
||||
_, err := c.GetRefund("1")
|
||||
if err == nil {
|
||||
t.Errorf("404 must be returned for ID=" + testSaleID)
|
||||
t.Errorf("404 must be returned for ID=%s", testSaleID)
|
||||
} else {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user