2014-06-17 11:22:06 -04:00
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
2018-05-28 17:26:48 -04:00
|
|
|
"errors"
|
|
|
|
"io"
|
2014-06-17 11:22:06 -04:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2015-12-29 23:02:54 -05:00
|
|
|
func TestTransportNilTokenSource(t *testing.T) {
|
|
|
|
tr := &Transport{}
|
|
|
|
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
defer server.Close()
|
|
|
|
client := &http.Client{Transport: tr}
|
2016-08-26 14:51:24 -04:00
|
|
|
resp, err := client.Get(server.URL)
|
2015-12-29 23:02:54 -05:00
|
|
|
if err == nil {
|
2016-08-26 14:51:24 -04:00
|
|
|
t.Errorf("got no errors, want an error with nil token source")
|
2015-12-29 23:02:54 -05:00
|
|
|
}
|
2016-08-26 14:51:24 -04:00
|
|
|
if resp != nil {
|
|
|
|
t.Errorf("Response = %v; want nil", resp)
|
2015-12-29 23:02:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:26:48 -04:00
|
|
|
type readCloseCounter struct {
|
|
|
|
CloseCount int
|
|
|
|
ReadErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readCloseCounter) Read(b []byte) (int, error) {
|
|
|
|
return 0, r.ReadErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readCloseCounter) Close() error {
|
|
|
|
r.CloseCount++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransportCloseRequestBody(t *testing.T) {
|
|
|
|
tr := &Transport{}
|
|
|
|
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
defer server.Close()
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
body := &readCloseCounter{
|
|
|
|
ReadErr: errors.New("readCloseCounter.Read not implemented"),
|
|
|
|
}
|
|
|
|
resp, err := client.Post(server.URL, "application/json", body)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("got no errors, want an error with nil token source")
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Errorf("Response = %v; want nil", resp)
|
|
|
|
}
|
|
|
|
if expected := 1; body.CloseCount != expected {
|
|
|
|
t.Errorf("Body was closed %d times, expected %d", body.CloseCount, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransportCloseRequestBodySuccess(t *testing.T) {
|
|
|
|
tr := &Transport{
|
|
|
|
Source: StaticTokenSource(&Token{
|
|
|
|
AccessToken: "abc",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
defer server.Close()
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
body := &readCloseCounter{
|
|
|
|
ReadErr: io.EOF,
|
|
|
|
}
|
|
|
|
resp, err := client.Post(server.URL, "application/json", body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got error %v; expected none", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Errorf("Response is nil; expected non-nil")
|
|
|
|
}
|
|
|
|
if expected := 1; body.CloseCount != expected {
|
|
|
|
t.Errorf("Body was closed %d times, expected %d", body.CloseCount, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-08-26 14:51:24 -04:00
|
|
|
if got, want := r.Header.Get("Authorization"), "Bearer abc"; got != want {
|
|
|
|
t.Errorf("Authorization header = %q; want %q", got, want)
|
2014-06-17 11:22:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
defer server.Close()
|
2015-12-29 23:02:54 -05:00
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
res, err := client.Get(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res.Body.Close()
|
2014-06-17 11:22:06 -04:00
|
|
|
}
|
|
|
|
|
2015-05-10 16:33:37 -04:00
|
|
|
// Test for case-sensitive token types, per https://github.com/golang/oauth2/issues/113
|
|
|
|
func TestTransportTokenSourceTypes(t *testing.T) {
|
|
|
|
const val = "abc"
|
|
|
|
tests := []struct {
|
|
|
|
key string
|
|
|
|
val string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{key: "bearer", val: val, want: "Bearer abc"},
|
|
|
|
{key: "mac", val: val, want: "MAC abc"},
|
|
|
|
{key: "basic", val: val, want: "Basic abc"},
|
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
|
|
ts := &tokenSource{
|
|
|
|
token: &Token{
|
|
|
|
AccessToken: tc.val,
|
|
|
|
TokenType: tc.key,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tr := &Transport{
|
|
|
|
Source: ts,
|
|
|
|
}
|
|
|
|
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("Authorization"), tc.want; got != want {
|
|
|
|
t.Errorf("Authorization header (%q) = %q; want %q", val, got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer server.Close()
|
2015-12-29 23:02:54 -05:00
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
res, err := client.Get(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res.Body.Close()
|
2015-05-10 16:33:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-08-26 14:51:24 -04:00
|
|
|
t.Errorf("got valid with no access token; want invalid")
|
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() {
|
2016-08-26 14:51:24 -04:00
|
|
|
t.Errorf("got valid with expired token; want invalid")
|
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))
|
|
|
|
}
|