Merge pull request #50 from snikch/fix-json-expiry

Handle expiry correctly in json response
This commit is contained in:
Burcu Dogan 2014-11-25 19:48:01 -08:00
commit 5fd31d511c
2 changed files with 7 additions and 4 deletions

View File

@ -343,14 +343,14 @@ func retrieveToken(o *Options, v url.Values) (*Token, error) {
token.TokenType, _ = b["token_type"].(string) token.TokenType, _ = b["token_type"].(string)
token.RefreshToken, _ = b["refresh_token"].(string) token.RefreshToken, _ = b["refresh_token"].(string)
token.raw = b token.raw = b
e, ok := b["expires_in"].(int) e, ok := b["expires_in"].(float64)
if !ok { if !ok {
// TODO(jbd): Facebook's OAuth2 implementation is broken and // TODO(jbd): Facebook's OAuth2 implementation is broken and
// returns expires_in field in expires. Remove the fallback to expires, // returns expires_in field in expires. Remove the fallback to expires,
// when Facebook fixes their implementation. // when Facebook fixes their implementation.
e, _ = b["expires"].(int) e, _ = b["expires"].(float64)
} }
expires = e expires = int(e)
} }
// Don't overwrite `RefreshToken` with an empty value // Don't overwrite `RefreshToken` with an empty value
// if this was a token refreshing request. // if this was a token refreshing request.

View File

@ -128,7 +128,7 @@ func TestExchangeRequest_JSONResponse(t *testing.T) {
t.Errorf("Unexpected exchange payload, %v is found.", string(body)) t.Errorf("Unexpected exchange payload, %v is found.", string(body))
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer"}`)) w.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer", "expires_in": 86400}`))
})) }))
defer ts.Close() defer ts.Close()
opts := newOpts(ts.URL) opts := newOpts(ts.URL)
@ -137,6 +137,9 @@ func TestExchangeRequest_JSONResponse(t *testing.T) {
t.Error(err) t.Error(err)
} }
tok := tr.Token() tok := tr.Token()
if tok.Expiry.IsZero() {
t.Errorf("Token expiry should not be zero.")
}
if tok.Expired() { if tok.Expired() {
t.Errorf("Token shouldn't be expired.") t.Errorf("Token shouldn't be expired.")
} }