Merge pull request #6 from bogdanconstantinescu/master

Increased code coverage for the unit tests
This commit is contained in:
Junpei Tsuji
2015-02-25 21:07:36 +09:00
3 changed files with 140 additions and 1 deletions

View File

@@ -33,7 +33,7 @@ func main() {
req := appstore.IAPRequest{
ReceiptData: "your receipt data encoded by base64",
}
resp, err := client.Verify(&req)
resp, err := client.Verify(req)
}
```

View File

@@ -2,6 +2,10 @@ package appstore
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"time"
@@ -79,6 +83,21 @@ func TestNew(t *testing.T) {
}
}
func TestNewWithEnvironment(t *testing.T) {
expected := Client{
URL: "https://buy.itunes.apple.com/verifyReceipt",
TimeOut: time.Second * 5,
}
os.Setenv("IAP_ENVIRONMENT", "production")
actual := New()
os.Clearenv()
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}
}
func TestNewWithConfig(t *testing.T) {
config := Config{
IsProduction: true,
@@ -96,6 +115,22 @@ func TestNewWithConfig(t *testing.T) {
}
}
func TestNewWithConfigTimeout(t *testing.T) {
config := Config{
IsProduction: true,
}
expected := Client{
URL: "https://buy.itunes.apple.com/verifyReceipt",
TimeOut: time.Second * 5,
}
actual := NewWithConfig(config)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}
}
func TestVerify(t *testing.T) {
client := New()
@@ -110,3 +145,46 @@ func TestVerify(t *testing.T) {
t.Errorf("got %v\nwant %v", actual, expected)
}
}
func TestVerifyErrors(t *testing.T) {
server, client := testTools(199, "dummy response")
defer server.Close()
req := IAPRequest{
ReceiptData: "dummy data",
}
expected := errors.New("An error occurred in IAP - code:199")
_, actual := client.Verify(req)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, 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("")
_, actual := client.Verify(req)
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

@@ -1,6 +1,7 @@
package playstore
import (
"errors"
"os"
"reflect"
"testing"
@@ -27,6 +28,18 @@ func TestInit(t *testing.T) {
}
}
func TestInitWithoutClientSecret(t *testing.T) {
expected := errors.New("Client Secret Key is required")
os.Setenv("IAB_CLIENT_ID", "dummyId")
actual := Init()
os.Clearenv()
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}
}
func TestInitWithConfig(t *testing.T) {
expected := &oauth.Config{
ClientId: "dummyId",
@@ -50,6 +63,34 @@ func TestInitWithConfig(t *testing.T) {
}
}
func TestInitWithConfigErrors(t *testing.T) {
expected := errors.New("Client ID is required")
config := &oauth.Config{
Scope: "https://www.googleapis.com/auth/androidpublisher",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
actual := InitWithConfig(config)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}
expected = errors.New("Client Secret Key is required")
config = &oauth.Config{
ClientId: "dummyId",
Scope: "https://www.googleapis.com/auth/androidpublisher",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
actual = InitWithConfig(config)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}
}
func TestNew(t *testing.T) {
// Initialize config
_config := &oauth.Config{
@@ -106,6 +147,16 @@ func TestVerifySubscription(t *testing.T) {
// TODO Nomal scenario
}
func TestVerifySubscriptionAndroidPublisherError(t *testing.T) {
client := Client{nil}
expected := errors.New("client is nil")
_, actual := client.VerifySubscription("package", "subscriptionID", "purchaseToken")
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}
}
func TestVerifyProduct(t *testing.T) {
Init()
@@ -126,3 +177,13 @@ func TestVerifyProduct(t *testing.T) {
// TODO Nomal scenario
}
func TestVerifyProductAndroidPublisherError(t *testing.T) {
client := Client{nil}
expected := errors.New("client is nil")
_, actual := client.VerifyProduct("package", "productID", "purchaseToken")
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}
}