Added client timeout because http.DefaultClient doesn't have timeout

This commit is contained in:
Junpei Tsuji
2018-05-18 12:18:09 +09:00
parent cdb4cd7c54
commit 8e67f122c0
6 changed files with 34 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"time"
) )
const ( const (
@@ -55,9 +56,11 @@ type Client struct {
// New creates a client object // New creates a client object
func New(secret string) *Client { func New(secret string) *Client {
client := &Client{ client := &Client{
URL: getSandboxURL(), URL: getSandboxURL(),
Secret: secret, Secret: secret,
httpCli: http.DefaultClient, httpCli: &http.Client{
Timeout: 10 * time.Second,
},
} }
if os.Getenv("IAP_ENVIRONMENT") == "production" { if os.Getenv("IAP_ENVIRONMENT") == "production" {
client.URL = ProductionURL client.URL = ProductionURL

View File

@@ -60,9 +60,11 @@ func TestHandle400Error(t *testing.T) {
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
expected := &Client{ expected := &Client{
URL: SandboxURL, URL: SandboxURL,
Secret: "developerSecret", Secret: "developerSecret",
httpCli: http.DefaultClient, httpCli: &http.Client{
Timeout: 10 * time.Second,
},
} }
actual := New("developerSecret") actual := New("developerSecret")
@@ -73,9 +75,11 @@ func TestNew(t *testing.T) {
func TestNewWithEnvironment(t *testing.T) { func TestNewWithEnvironment(t *testing.T) {
expected := &Client{ expected := &Client{
URL: ProductionURL, URL: ProductionURL,
Secret: "developerSecret", Secret: "developerSecret",
httpCli: http.DefaultClient, httpCli: &http.Client{
Timeout: 10 * time.Second,
},
} }
os.Setenv("IAP_ENVIRONMENT", "production") os.Setenv("IAP_ENVIRONMENT", "production")

View File

@@ -7,6 +7,7 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time"
) )
const ( const (
@@ -78,7 +79,9 @@ func New() *Client {
client := &Client{ client := &Client{
ProductionURL: ProductionURL, ProductionURL: ProductionURL,
SandboxURL: SandboxURL, SandboxURL: SandboxURL,
httpCli: http.DefaultClient, httpCli: &http.Client{
Timeout: 10 * time.Second,
},
} }
return client return client
} }

View File

@@ -90,7 +90,9 @@ func TestNew(t *testing.T) {
expected := &Client{ expected := &Client{
ProductionURL: ProductionURL, ProductionURL: ProductionURL,
SandboxURL: SandboxURL, SandboxURL: SandboxURL,
httpCli: http.DefaultClient, httpCli: &http.Client{
Timeout: 10 * time.Second,
},
} }
actual := New() actual := New()

View File

@@ -9,6 +9,7 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net/http" "net/http"
"time"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
@@ -26,14 +27,14 @@ type IABClient interface {
// The Client type implements VerifySubscription method // The Client type implements VerifySubscription method
type Client struct { type Client struct {
httpClient *http.Client httpCli *http.Client
} }
// New returns http client which includes the credentials to access androidpublisher API. // New returns http client which includes the credentials to access androidpublisher API.
// You should create a service account for your project at // You should create a service account for your project at
// https://console.developers.google.com and download a JSON key file to set this argument. // https://console.developers.google.com and download a JSON key file to set this argument.
func New(jsonKey []byte) (*Client, error) { func New(jsonKey []byte) (*Client, error) {
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, http.DefaultClient) ctx := context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{Timeout: 10 * time.Second})
conf, err := google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope) conf, err := google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope)
@@ -59,7 +60,7 @@ func (c *Client) VerifySubscription(
subscriptionID string, subscriptionID string,
token string, token string,
) (*androidpublisher.SubscriptionPurchase, error) { ) (*androidpublisher.SubscriptionPurchase, error) {
service, err := androidpublisher.New(c.httpClient) service, err := androidpublisher.New(c.httpCli)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -77,7 +78,7 @@ func (c *Client) VerifyProduct(
productID string, productID string,
token string, token string,
) (*androidpublisher.ProductPurchase, error) { ) (*androidpublisher.ProductPurchase, error) {
service, err := androidpublisher.New(c.httpClient) service, err := androidpublisher.New(c.httpCli)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -90,7 +91,7 @@ func (c *Client) VerifyProduct(
// CancelSubscription cancels a user's subscription purchase. // CancelSubscription cancels a user's subscription purchase.
func (c *Client) CancelSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error { func (c *Client) CancelSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error {
service, err := androidpublisher.New(c.httpClient) service, err := androidpublisher.New(c.httpCli)
if err != nil { if err != nil {
return err return err
} }
@@ -104,7 +105,7 @@ func (c *Client) CancelSubscription(ctx context.Context, packageName string, sub
// RefundSubscription refunds a user's subscription purchase, but the subscription remains valid // RefundSubscription refunds a user's subscription purchase, but the subscription remains valid
// until its expiration time and it will continue to recur. // until its expiration time and it will continue to recur.
func (c *Client) RefundSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error { func (c *Client) RefundSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error {
service, err := androidpublisher.New(c.httpClient) service, err := androidpublisher.New(c.httpCli)
if err != nil { if err != nil {
return err return err
} }
@@ -118,7 +119,7 @@ func (c *Client) RefundSubscription(ctx context.Context, packageName string, sub
// RevokeSubscription refunds and immediately revokes a user's subscription purchase. // RevokeSubscription refunds and immediately revokes a user's subscription purchase.
// Access to the subscription will be terminated immediately and it will stop recurring. // Access to the subscription will be terminated immediately and it will stop recurring.
func (c *Client) RevokeSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error { func (c *Client) RevokeSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error {
service, err := androidpublisher.New(c.httpClient) service, err := androidpublisher.New(c.httpCli)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -37,7 +37,7 @@ func TestNew(t *testing.T) {
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}"
actual, _ := New(dummyKey) actual, _ := New(dummyKey)
val := actual.httpClient.Transport.(*oauth2.Transport) val := actual.httpCli.Transport.(*oauth2.Transport)
token, err := val.Source.Token() token, err := val.Source.Token()
if token != nil { if token != nil {
t.Errorf("got %#v", token) t.Errorf("got %#v", token)
@@ -48,7 +48,7 @@ func TestNew(t *testing.T) {
// TODO Normal scenario // TODO Normal scenario
actual, _ = New(jsonKey) actual, _ = New(jsonKey)
val = actual.httpClient.Transport.(*oauth2.Transport) val = actual.httpCli.Transport.(*oauth2.Transport)
token, err = val.Source.Token() token, err = val.Source.Token()
if err != nil { if err != nil {
t.Errorf("got %#v", err) t.Errorf("got %#v", err)
@@ -62,7 +62,7 @@ func TestNewWithClient(t *testing.T) {
httpClient := urlfetch.Client(ctx) httpClient := urlfetch.Client(ctx)
cli, _ := NewWithClient(dummyKey, httpClient) cli, _ := NewWithClient(dummyKey, httpClient)
tr, _ := cli.httpClient.Transport.(*oauth2.Transport) tr, _ := cli.httpCli.Transport.(*oauth2.Transport)
if !reflect.DeepEqual(tr.Base, httpClient.Transport) { if !reflect.DeepEqual(tr.Base, httpClient.Transport) {
t.Errorf("transport should be urlfetch's one") t.Errorf("transport should be urlfetch's one")