Use http package instead of gorequest

This commit is contained in:
Junpei Tsuji
2016-11-01 23:30:58 +09:00
parent 3890ed82ea
commit ebac4ddf8d
4 changed files with 42 additions and 48 deletions

View File

@@ -12,6 +12,7 @@ import (
) )
func TestHandle497Error(t *testing.T) { func TestHandle497Error(t *testing.T) {
t.Parallel()
var expected, actual error var expected, actual error
client := New("developerSecret") client := New("developerSecret")
@@ -33,6 +34,7 @@ func TestHandle497Error(t *testing.T) {
} }
func TestHandle400Error(t *testing.T) { func TestHandle400Error(t *testing.T) {
t.Parallel()
var expected, actual error var expected, actual error
client := New("developerSecret") client := New("developerSecret")
@@ -54,6 +56,7 @@ func TestHandle400Error(t *testing.T) {
} }
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
t.Parallel()
expected := Client{ expected := Client{
URL: SandboxURL, URL: SandboxURL,
TimeOut: time.Second * 5, TimeOut: time.Second * 5,
@@ -67,6 +70,7 @@ func TestNew(t *testing.T) {
} }
func TestNewWithEnvironment(t *testing.T) { func TestNewWithEnvironment(t *testing.T) {
t.Parallel()
expected := Client{ expected := Client{
URL: ProductionURL, URL: ProductionURL,
TimeOut: time.Second * 5, TimeOut: time.Second * 5,
@@ -83,6 +87,7 @@ func TestNewWithEnvironment(t *testing.T) {
} }
func TestNewWithConfig(t *testing.T) { func TestNewWithConfig(t *testing.T) {
t.Parallel()
config := Config{ config := Config{
IsProduction: true, IsProduction: true,
Secret: "developerSecret", Secret: "developerSecret",
@@ -102,6 +107,7 @@ func TestNewWithConfig(t *testing.T) {
} }
func TestNewWithConfigTimeout(t *testing.T) { func TestNewWithConfigTimeout(t *testing.T) {
t.Parallel()
config := Config{ config := Config{
IsProduction: true, IsProduction: true,
Secret: "developerSecret", Secret: "developerSecret",
@@ -120,6 +126,7 @@ func TestNewWithConfigTimeout(t *testing.T) {
} }
func TestVerify(t *testing.T) { func TestVerify(t *testing.T) {
t.Parallel()
server, client := testTools( server, client := testTools(
200, 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}", "{\"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) { func TestVerifyTimeout(t *testing.T) {
t.Parallel()
// HTTP 100 is "continue" so it will time out // HTTP 100 is "continue" so it will time out
server, client := testTools(100, "timeout response") server, client := testTools(100, "timeout response")
defer server.Close() defer server.Close()

View File

@@ -1,14 +1,12 @@
package appstore package appstore
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "net/http"
"os" "os"
"strings"
"time" "time"
"github.com/parnurzeal/gorequest"
) )
const ( const (
@@ -99,17 +97,20 @@ func NewWithConfig(config Config) Client {
// Verify sends receipts and gets validation result // Verify sends receipts and gets validation result
func (c *Client) Verify(req IAPRequest, result interface{}) error { func (c *Client) Verify(req IAPRequest, result interface{}) error {
_, body, errs := gorequest.New(). client := http.Client{
Post(c.URL). Timeout: c.TimeOut,
Send(req).
Timeout(c.TimeOut).
End()
if errs != nil {
return fmt.Errorf("%v", errs)
} }
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 return err
} }

View File

@@ -2,9 +2,6 @@ package appstore
import ( import (
"errors" "errors"
"fmt"
"net/http"
"net/http/httptest"
"os" "os"
"reflect" "reflect"
"testing" "testing"
@@ -133,45 +130,23 @@ func TestNewWithConfigTimeout(t *testing.T) {
func TestVerify(t *testing.T) { func TestVerify(t *testing.T) {
client := New() client := New()
client.TimeOut = time.Millisecond * 100
expected := &IAPResponse{
Status: 21002,
}
req := IAPRequest{ req := IAPRequest{
ReceiptData: "dummy data", ReceiptData: "dummy data",
} }
result := &IAPResponse{} 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) client.Verify(req, result)
if !reflect.DeepEqual(result, expected) { if !reflect.DeepEqual(result, expected) {
t.Errorf("got %v\nwant %v", 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
}

View File

@@ -31,6 +31,7 @@ func init() {
} }
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
t.Parallel()
// Exception scenario // Exception scenario
expected := "oauth2: cannot fetch token: 401 Unauthorized\nResponse: {\n \"error\" : \"invalid_client\",\n \"error_description\" : \"The OAuth client was invalid.\"\n}" 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) { func TestSetTimeout(t *testing.T) {
t.Parallel()
_timeout := time.Second * 3 _timeout := time.Second * 3
SetTimeout(_timeout) SetTimeout(_timeout)
@@ -63,6 +65,7 @@ func TestSetTimeout(t *testing.T) {
} }
func TestVerifySubscription(t *testing.T) { func TestVerifySubscription(t *testing.T) {
t.Parallel()
// Exception scenario // Exception scenario
expected := "googleapi: Error 404: No application was found for the given package name., applicationNotFound" 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) { func TestVerifySubscriptionAndroidPublisherError(t *testing.T) {
t.Parallel()
client := Client{nil} client := Client{nil}
expected := errors.New("client is nil") expected := errors.New("client is nil")
_, actual := client.VerifySubscription("package", "subscriptionID", "purchaseToken") _, actual := client.VerifySubscription("package", "subscriptionID", "purchaseToken")
@@ -87,6 +91,7 @@ func TestVerifySubscriptionAndroidPublisherError(t *testing.T) {
} }
func TestVerifyProduct(t *testing.T) { func TestVerifyProduct(t *testing.T) {
t.Parallel()
// Exception scenario // Exception scenario
expected := "googleapi: Error 404: No application was found for the given package name., applicationNotFound" 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) { func TestVerifyProductAndroidPublisherError(t *testing.T) {
t.Parallel()
client := Client{nil} client := Client{nil}
expected := errors.New("client is nil") expected := errors.New("client is nil")
_, actual := client.VerifyProduct("package", "productID", "purchaseToken") _, actual := client.VerifyProduct("package", "productID", "purchaseToken")
@@ -111,6 +117,7 @@ func TestVerifyProductAndroidPublisherError(t *testing.T) {
} }
func TestCancelSubscription(t *testing.T) { func TestCancelSubscription(t *testing.T) {
t.Parallel()
// Exception scenario // Exception scenario
client := Client{nil} client := Client{nil}
expected := errors.New("client is nil") expected := errors.New("client is nil")
@@ -132,6 +139,7 @@ func TestCancelSubscription(t *testing.T) {
} }
func TestRefundSubscription(t *testing.T) { func TestRefundSubscription(t *testing.T) {
t.Parallel()
// Exception scenario // Exception scenario
client := Client{nil} client := Client{nil}
expected := errors.New("client is nil") expected := errors.New("client is nil")
@@ -153,6 +161,7 @@ func TestRefundSubscription(t *testing.T) {
} }
func TestRevokeSubscription(t *testing.T) { func TestRevokeSubscription(t *testing.T) {
t.Parallel()
// Exception scenario // Exception scenario
client := Client{nil} client := Client{nil}
expected := errors.New("client is nil") expected := errors.New("client is nil")
@@ -174,6 +183,7 @@ func TestRevokeSubscription(t *testing.T) {
} }
func TestVerifySignature(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"}` 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 // when public key format is invalid base64