Skip to content

Commit

Permalink
fix #39. use json.Number in oauth result map so that expires can be…
Browse files Browse the repository at this point in the history
… parsed correctly.
  • Loading branch information
huandu committed Jul 29, 2015
1 parent 4a5abfb commit ff27e4b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
48 changes: 46 additions & 2 deletions facebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,8 +1417,8 @@ func TestCamelCaseToUnderScore(t *testing.T) {
func TestMakeSliceResult(t *testing.T) {
jsonStr := `{
"error": {
"message": "Invalid OAuth access token.",
"type": "OAuthException",
"message": "Invalid OAuth access token.",
"type": "OAuthException",
"code": 190
}
}`
Expand Down Expand Up @@ -1467,3 +1467,47 @@ func TestMakeSliceResultWithNilElements(t *testing.T) {
t.Fatalf("decode res is not expected. [res:%v]", res)
}
}

// case for #39.
func TestResultDecodeNumberString(t *testing.T) {
res := Result{
"int": json.Number("1234"),
"string": json.Number("1234"),
"float": json.Number("1234"),
}

var intValue int64
var strValue string
var floatValue float32
var err error

err = res.DecodeField("int", &intValue)

if err != nil {
t.Fatalf("fail to decode field `int`. [e:%v]", err)
}

if intValue != 1234 {
t.Fatalf("unexpected int value. [expect:1234] [actual:%v]", intValue)
}

err = res.DecodeField("string", &strValue)

if err != nil {
t.Fatalf("fail to decode field `string`. [e:%v]", err)
}

if strValue != "1234" {
t.Fatalf("unexpected string value. [expect:1234] [actual:%v]", strValue)
}

err = res.DecodeField("float", &floatValue)

if err != nil {
t.Fatalf("fail to decode field `float`. [e:%v]", err)
}

if floatValue != 1234 {
t.Fatalf("unexpected float value. [expect:1234] [actual:%v]", floatValue)
}
}
5 changes: 4 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -562,7 +563,9 @@ func (session *Session) sendOauthRequest(uri string, params Params) (Result, err
res := Result{}

for k := range query {
res[k] = query.Get(k)
// json.Number is an alias of string and can be decoded as a string or number.
// therefore, it's safe to convert all query values to this type for all purpose.
res[k] = json.Number(query.Get(k))
}

return res, nil
Expand Down

0 comments on commit ff27e4b

Please sign in to comment.