This commit is contained in:
Aliaksandr Pliutau 2016-09-19 11:39:05 +07:00
parent 959619c327
commit f65c5dc88e
8 changed files with 32 additions and 15 deletions

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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())
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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))
}
}

View File

@ -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())
}

View File

@ -13,6 +13,9 @@ const (
// APIBaseLive points to the live version of the API
APIBaseLive = "https://api.paypal.com"
// RequestNewTokenBeforeExpiresIn is used by SendWithAuth and try to get new Token when it's about to expire
RequestNewTokenBeforeExpiresIn = 60
)
type (