mirror of
https://github.com/golang/oauth2.git
synced 2025-07-21 00:00:09 +08:00
Instead of maintaining a global map of which OAuth2 servers do which auth style and/or requiring the user to tell us, just try both ways and remember which way worked. But if users want to tell us in the Endpoint, this CL also add Endpoint.AuthStyle. Fixes golang/oauth2#111 Fixes golang/oauth2#365 Fixes golang/oauth2#362 Fixes golang/oauth2#357 Fixes golang/oauth2#353 Fixes golang/oauth2#345 Fixes golang/oauth2#326 Fixes golang/oauth2#352 Fixes golang/oauth2#268 Fixes https://go-review.googlesource.com/c/oauth2/+/58510 (... and surely many more ...) Change-Id: I7b4d98ba1900ee2d3e11e629316b0bf867f7d237 Reviewed-on: https://go-review.googlesource.com/c/157820 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ross Light <light@google.com>
65 lines
2.0 KiB
Go
65 lines
2.0 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package internal
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestRetrieveToken_InParams(t *testing.T) {
|
|
ResetAuthCache()
|
|
const clientID = "client-id"
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if got, want := r.FormValue("client_id"), clientID; got != want {
|
|
t.Errorf("client_id = %q; want %q", got, want)
|
|
}
|
|
if got, want := r.FormValue("client_secret"), ""; got != want {
|
|
t.Errorf("client_secret = %q; want empty", got)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"access_token": "ACCESS_TOKEN", "token_type": "bearer"}`)
|
|
}))
|
|
defer ts.Close()
|
|
_, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{}, AuthStyleInParams)
|
|
if err != nil {
|
|
t.Errorf("RetrieveToken = %v; want no error", err)
|
|
}
|
|
}
|
|
|
|
func TestRetrieveTokenWithContexts(t *testing.T) {
|
|
ResetAuthCache()
|
|
const clientID = "client-id"
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"access_token": "ACCESS_TOKEN", "token_type": "bearer"}`)
|
|
}))
|
|
defer ts.Close()
|
|
|
|
_, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{}, AuthStyleUnknown)
|
|
if err != nil {
|
|
t.Errorf("RetrieveToken (with background context) = %v; want no error", err)
|
|
}
|
|
|
|
retrieved := make(chan struct{})
|
|
cancellingts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
<-retrieved
|
|
}))
|
|
defer cancellingts.Close()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
_, err = RetrieveToken(ctx, clientID, "", cancellingts.URL, url.Values{}, AuthStyleUnknown)
|
|
close(retrieved)
|
|
if err == nil {
|
|
t.Errorf("RetrieveToken (with cancelled context) = nil; want error")
|
|
}
|
|
}
|