mirror of
https://github.com/golang/oauth2.git
synced 2025-07-21 00:00:09 +08:00
Token caching is now done whenever you make a Client, and ReuseTokenSource is exported from the oauth2 package and used by the Google TokenSources (Compute and App Engine). Token.Expired is now Token.Valid, and works on nil receivers. Some other wording cleanups in the process. All tests pass. App Engine should pass, but is untested. Change-Id: Ibe1d2599ac3ccfe9b399b1672f74bb24cfc8d311 Reviewed-on: https://go-review.googlesource.com/2195 Reviewed-by: Burcu Dogan <jbd@google.com>
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package oauth2
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type tokenSource struct{ token *Token }
|
|
|
|
func (t *tokenSource) Token() (*Token, error) {
|
|
return t.token, nil
|
|
}
|
|
|
|
func TestTransportTokenSource(t *testing.T) {
|
|
ts := &tokenSource{
|
|
token: &Token{
|
|
AccessToken: "abc",
|
|
},
|
|
}
|
|
tr := &Transport{
|
|
Source: ts,
|
|
}
|
|
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Bearer abc" {
|
|
t.Errorf("Transport doesn't set the Authorization header from the fetched token")
|
|
}
|
|
})
|
|
defer server.Close()
|
|
client := http.Client{Transport: tr}
|
|
client.Get(server.URL)
|
|
}
|
|
|
|
func TestTokenValidNoAccessToken(t *testing.T) {
|
|
token := &Token{}
|
|
if token.Valid() {
|
|
t.Errorf("Token should not be valid with no access token")
|
|
}
|
|
}
|
|
|
|
func TestExpiredWithExpiry(t *testing.T) {
|
|
token := &Token{
|
|
Expiry: time.Now().Add(-5 * time.Hour),
|
|
}
|
|
if token.Valid() {
|
|
t.Errorf("Token should not be valid if it expired in the past")
|
|
}
|
|
}
|
|
|
|
func newMockServer(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
|
|
return httptest.NewServer(http.HandlerFunc(handler))
|
|
}
|