2014-06-17 11:22:06 -04:00
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
type tokenSource struct{ token *Token }
|
2014-06-17 11:22:06 -04:00
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
func (t *tokenSource) Token() (*Token, error) {
|
|
|
|
return t.token, nil
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
func TestTransportTokenSource(t *testing.T) {
|
|
|
|
ts := &tokenSource{
|
2014-06-17 11:22:06 -04:00
|
|
|
token: &Token{
|
|
|
|
AccessToken: "abc",
|
|
|
|
},
|
|
|
|
}
|
2014-12-11 02:30:13 -05:00
|
|
|
tr := &Transport{
|
|
|
|
Source: ts,
|
|
|
|
}
|
2014-06-17 11:22:06 -04:00
|
|
|
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get("Authorization") != "Bearer abc" {
|
2014-07-09 01:27:34 -04:00
|
|
|
t.Errorf("Transport doesn't set the Authorization header from the fetched token")
|
2014-06-17 11:22:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
defer server.Close()
|
|
|
|
client := http.Client{Transport: tr}
|
|
|
|
client.Get(server.URL)
|
|
|
|
}
|
|
|
|
|
2014-12-30 16:25:01 -05:00
|
|
|
func TestTokenValidNoAccessToken(t *testing.T) {
|
2014-06-17 11:22:06 -04:00
|
|
|
token := &Token{}
|
2014-12-30 16:25:01 -05:00
|
|
|
if token.Valid() {
|
|
|
|
t.Errorf("Token should not be valid with no access token")
|
2014-06-17 11:22:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiredWithExpiry(t *testing.T) {
|
|
|
|
token := &Token{
|
|
|
|
Expiry: time.Now().Add(-5 * time.Hour),
|
|
|
|
}
|
2014-12-30 16:25:01 -05:00
|
|
|
if token.Valid() {
|
|
|
|
t.Errorf("Token should not be valid if it expired in the past")
|
2014-06-17 11:22:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockServer(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
|
|
|
|
return httptest.NewServer(http.HandlerFunc(handler))
|
|
|
|
}
|