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`
This commit is contained in:
Timothy Lock
2018-01-17 16:47:14 -05:00
parent 132768c0fe
commit fe51698284
3 changed files with 19 additions and 6 deletions

View File

@@ -115,7 +115,7 @@ type (
// The HttpStatusResponse struct contains the status code returned by the store // 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 // 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"` Status int `json:"status"`
} }
) )

View File

@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil"
"net/http" "net/http"
"time" "time"
) )
@@ -115,14 +116,24 @@ func (c *Client) Verify(req IAPRequest, result interface{}) error {
} }
defer resp.Body.Close() 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 { if err != nil {
return err return err
} }
// https://developer.apple.com/library/content/technotes/tn2413/_index.html#//apple_ref/doc/uid/DTS40016228-CH1-RECEIPTURL // https://developer.apple.com/library/content/technotes/tn2413/_index.html#//apple_ref/doc/uid/DTS40016228-CH1-RECEIPTURL
r, ok := result.(*HttpStatusResponse) var r StatusResponse
if ok && r.Status == 21007 { err = json.Unmarshal(buf, &r)
if err != nil {
return err
}
if r.Status == 21007 {
b = new(bytes.Buffer) b = new(bytes.Buffer)
json.NewEncoder(b).Encode(req) json.NewEncoder(b).Encode(req)
resp, err := client.Post(c.SandboxURL, "application/json; charset=utf-8", b) resp, err := client.Post(c.SandboxURL, "application/json; charset=utf-8", b)

View File

@@ -93,9 +93,9 @@ func TestHandleError(t *testing.T) {
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
expected := Client{ expected := Client{
ProductionURL: SandboxURL, ProductionURL: ProductionURL,
TimeOut: time.Second * 5,
SandboxURL: SandboxURL, SandboxURL: SandboxURL,
TimeOut: time.Second * 5,
} }
actual := New() actual := New()
@@ -127,6 +127,7 @@ func TestNewWithConfig(t *testing.T) {
expected := Client{ expected := Client{
ProductionURL: ProductionURL, ProductionURL: ProductionURL,
SandboxURL: SandboxURL,
TimeOut: time.Second * 2, TimeOut: time.Second * 2,
} }
@@ -141,6 +142,7 @@ func TestNewWithConfigTimeout(t *testing.T) {
expected := Client{ expected := Client{
ProductionURL: ProductionURL, ProductionURL: ProductionURL,
SandboxURL: SandboxURL,
TimeOut: time.Second * 5, TimeOut: time.Second * 5,
} }