Use http package instead of gorequest
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func TestHandle497Error(t *testing.T) {
|
||||
t.Parallel()
|
||||
var expected, actual error
|
||||
client := New("developerSecret")
|
||||
|
||||
@@ -33,6 +34,7 @@ func TestHandle497Error(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHandle400Error(t *testing.T) {
|
||||
t.Parallel()
|
||||
var expected, actual error
|
||||
client := New("developerSecret")
|
||||
|
||||
@@ -54,6 +56,7 @@ func TestHandle400Error(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
t.Parallel()
|
||||
expected := Client{
|
||||
URL: SandboxURL,
|
||||
TimeOut: time.Second * 5,
|
||||
@@ -67,6 +70,7 @@ func TestNew(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewWithEnvironment(t *testing.T) {
|
||||
t.Parallel()
|
||||
expected := Client{
|
||||
URL: ProductionURL,
|
||||
TimeOut: time.Second * 5,
|
||||
@@ -83,6 +87,7 @@ func TestNewWithEnvironment(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewWithConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
config := Config{
|
||||
IsProduction: true,
|
||||
Secret: "developerSecret",
|
||||
@@ -102,6 +107,7 @@ func TestNewWithConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewWithConfigTimeout(t *testing.T) {
|
||||
t.Parallel()
|
||||
config := Config{
|
||||
IsProduction: true,
|
||||
Secret: "developerSecret",
|
||||
@@ -120,6 +126,7 @@ func TestNewWithConfigTimeout(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVerify(t *testing.T) {
|
||||
t.Parallel()
|
||||
server, client := testTools(
|
||||
200,
|
||||
"{\"purchaseDate\":1402008634018,\"receiptId\":\"q1YqVrJSSs7P1UvMTazKz9PLTCwoTswtyEktM9JLrShIzCvOzM-LL04tiTdW0lFKASo2NDEwMjCwMDM2MTC0AIqVAsUsLd1c4l18jIxdfTOK_N1d8kqLLHVLc8oK83OLgtPNCit9AoJdjJ3dXG2BGkqUrAxrAQ\",\"productId\":\"com.amazon.iapsamplev2.expansion_set_3\",\"parentProductId\":null,\"productType\":\"ENTITLED\",\"cancelDate\":null,\"quantity\":1,\"betaProduct\":false,\"testTransaction\":true}",
|
||||
@@ -145,6 +152,7 @@ func TestVerify(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVerifyTimeout(t *testing.T) {
|
||||
t.Parallel()
|
||||
// HTTP 100 is "continue" so it will time out
|
||||
server, client := testTools(100, "timeout response")
|
||||
defer server.Close()
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/parnurzeal/gorequest"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -99,17 +97,20 @@ func NewWithConfig(config Config) Client {
|
||||
|
||||
// Verify sends receipts and gets validation result
|
||||
func (c *Client) Verify(req IAPRequest, result interface{}) error {
|
||||
_, body, errs := gorequest.New().
|
||||
Post(c.URL).
|
||||
Send(req).
|
||||
Timeout(c.TimeOut).
|
||||
End()
|
||||
|
||||
if errs != nil {
|
||||
return fmt.Errorf("%v", errs)
|
||||
client := http.Client{
|
||||
Timeout: c.TimeOut,
|
||||
}
|
||||
|
||||
err := json.NewDecoder(strings.NewReader(body)).Decode(result)
|
||||
b := new(bytes.Buffer)
|
||||
json.NewEncoder(b).Encode(req)
|
||||
|
||||
resp, err := client.Post(c.URL, "application/json; charset=utf-8", b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(result)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,9 +2,6 @@ package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -133,45 +130,23 @@ func TestNewWithConfigTimeout(t *testing.T) {
|
||||
|
||||
func TestVerify(t *testing.T) {
|
||||
client := New()
|
||||
client.TimeOut = time.Millisecond * 100
|
||||
|
||||
expected := &IAPResponse{
|
||||
Status: 21002,
|
||||
}
|
||||
req := IAPRequest{
|
||||
ReceiptData: "dummy data",
|
||||
}
|
||||
result := &IAPResponse{}
|
||||
err := client.Verify(req, result)
|
||||
if err == nil {
|
||||
t.Errorf("error should be occurred because of timeout")
|
||||
}
|
||||
|
||||
client = New()
|
||||
expected := &IAPResponse{
|
||||
Status: 21002,
|
||||
}
|
||||
client.Verify(req, result)
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("got %v\nwant %v", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyTimeout(t *testing.T) {
|
||||
// HTTP 100 is "continue" so it will time out
|
||||
server, client := testTools(100, "dummy response")
|
||||
defer server.Close()
|
||||
|
||||
req := IAPRequest{
|
||||
ReceiptData: "dummy data",
|
||||
}
|
||||
|
||||
expected := errors.New("")
|
||||
result := &IAPResponse{}
|
||||
actual := client.Verify(req, result)
|
||||
if !reflect.DeepEqual(reflect.TypeOf(actual), reflect.TypeOf(expected)) {
|
||||
t.Errorf("got %v\nwant %v", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func testTools(code int, body string) (*httptest.Server, *Client) {
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(code)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprintln(w, body)
|
||||
}))
|
||||
|
||||
client := &Client{URL: server.URL, TimeOut: time.Second * 2}
|
||||
return server, client
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ func init() {
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Exception scenario
|
||||
expected := "oauth2: cannot fetch token: 401 Unauthorized\nResponse: {\n \"error\" : \"invalid_client\",\n \"error_description\" : \"The OAuth client was invalid.\"\n}"
|
||||
|
||||
@@ -54,6 +55,7 @@ func TestNew(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSetTimeout(t *testing.T) {
|
||||
t.Parallel()
|
||||
_timeout := time.Second * 3
|
||||
SetTimeout(_timeout)
|
||||
|
||||
@@ -63,6 +65,7 @@ func TestSetTimeout(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVerifySubscription(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Exception scenario
|
||||
expected := "googleapi: Error 404: No application was found for the given package name., applicationNotFound"
|
||||
|
||||
@@ -77,6 +80,7 @@ func TestVerifySubscription(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVerifySubscriptionAndroidPublisherError(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := Client{nil}
|
||||
expected := errors.New("client is nil")
|
||||
_, actual := client.VerifySubscription("package", "subscriptionID", "purchaseToken")
|
||||
@@ -87,6 +91,7 @@ func TestVerifySubscriptionAndroidPublisherError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVerifyProduct(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Exception scenario
|
||||
expected := "googleapi: Error 404: No application was found for the given package name., applicationNotFound"
|
||||
|
||||
@@ -101,6 +106,7 @@ func TestVerifyProduct(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVerifyProductAndroidPublisherError(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := Client{nil}
|
||||
expected := errors.New("client is nil")
|
||||
_, actual := client.VerifyProduct("package", "productID", "purchaseToken")
|
||||
@@ -111,6 +117,7 @@ func TestVerifyProductAndroidPublisherError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCancelSubscription(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Exception scenario
|
||||
client := Client{nil}
|
||||
expected := errors.New("client is nil")
|
||||
@@ -132,6 +139,7 @@ func TestCancelSubscription(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRefundSubscription(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Exception scenario
|
||||
client := Client{nil}
|
||||
expected := errors.New("client is nil")
|
||||
@@ -153,6 +161,7 @@ func TestRefundSubscription(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRevokeSubscription(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Exception scenario
|
||||
client := Client{nil}
|
||||
expected := errors.New("client is nil")
|
||||
@@ -174,6 +183,7 @@ func TestRevokeSubscription(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVerifySignature(t *testing.T) {
|
||||
t.Parallel()
|
||||
receipt := `{"orderId":"GPA.xxxx-xxxx-xxxx-xxxxx","packageName":"my.package","productId":"myproduct","purchaseTime":1437564796303,"purchaseState":0,"developerPayload":"user001","purchaseToken":"some-token"}`
|
||||
|
||||
// when public key format is invalid base64
|
||||
|
||||
Reference in New Issue
Block a user