From fe51698284efb50437c6554ed4fd409131e485f5 Mon Sep 17 00:00:00 2001 From: Timothy Lock Date: Wed, 17 Jan 2018 16:47:14 -0500 Subject: [PATCH] Fix bug involving HttpStatusResponse the `ok` in `r, ok := result.(*HttpStatusResponse)` would always fail unless the `result` being passed in was also of type `HttpStatusResponse` --- appstore/model.go | 2 +- appstore/validator.go | 17 ++++++++++++++--- appstore/validator_test.go | 6 ++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/appstore/model.go b/appstore/model.go index 9f45d4f..a4c3476 100644 --- a/appstore/model.go +++ b/appstore/model.go @@ -115,7 +115,7 @@ type ( // The HttpStatusResponse struct contains the status code returned by the store // Used as a workaround to detect when to hit the production appstore or sandbox appstore regardless of receipt type - HttpStatusResponse struct { + StatusResponse struct { Status int `json:"status"` } ) diff --git a/appstore/validator.go b/appstore/validator.go index daaacbb..42eeba0 100644 --- a/appstore/validator.go +++ b/appstore/validator.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "io/ioutil" "net/http" "time" ) @@ -115,14 +116,24 @@ func (c *Client) Verify(req IAPRequest, result interface{}) error { } defer resp.Body.Close() - err = json.NewDecoder(resp.Body).Decode(result) + // Read the body now so that we can unmarshal it twice + buf, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + err = json.Unmarshal(buf, &result) if err != nil { return err } // https://developer.apple.com/library/content/technotes/tn2413/_index.html#//apple_ref/doc/uid/DTS40016228-CH1-RECEIPTURL - r, ok := result.(*HttpStatusResponse) - if ok && r.Status == 21007 { + var r StatusResponse + err = json.Unmarshal(buf, &r) + if err != nil { + return err + } + if r.Status == 21007 { b = new(bytes.Buffer) json.NewEncoder(b).Encode(req) resp, err := client.Post(c.SandboxURL, "application/json; charset=utf-8", b) diff --git a/appstore/validator_test.go b/appstore/validator_test.go index 2b2f831..8e21cf0 100644 --- a/appstore/validator_test.go +++ b/appstore/validator_test.go @@ -93,9 +93,9 @@ func TestHandleError(t *testing.T) { func TestNew(t *testing.T) { expected := Client{ - ProductionURL: SandboxURL, - TimeOut: time.Second * 5, + ProductionURL: ProductionURL, SandboxURL: SandboxURL, + TimeOut: time.Second * 5, } actual := New() @@ -127,6 +127,7 @@ func TestNewWithConfig(t *testing.T) { expected := Client{ ProductionURL: ProductionURL, + SandboxURL: SandboxURL, TimeOut: time.Second * 2, } @@ -141,6 +142,7 @@ func TestNewWithConfigTimeout(t *testing.T) { expected := Client{ ProductionURL: ProductionURL, + SandboxURL: SandboxURL, TimeOut: time.Second * 5, }