all: deprecate NoContext

There is no good reason why we suggest NoContext rather than
context.Background(). When the oauth2 library first came around, the
community was not familiar with the x/net/context package. For
documentation reasons, we decided to add NoContext to the oauth2
package. It was not a good idea even back then. And given that context
package is fairly popular, there is no good reason why we are
depending on this.

Updating all the references of NoContext with context.Background
and documenting it as deprecated.

Change-Id: I18e390f1351023a29b567777a3f963dd550cf657
Reviewed-on: https://go-review.googlesource.com/27690
Reviewed-by: Chris Broadfoot <cbro@golang.org>
This commit is contained in:
Jaana Burcu Dogan 2016-08-24 15:40:36 -07:00
parent 54f42edba4
commit c10ba270aa
6 changed files with 27 additions and 23 deletions

View File

@ -5,12 +5,11 @@
package clientcredentials package clientcredentials
import ( import (
"context"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"golang.org/x/oauth2"
) )
func newConf(url string) *Config { func newConf(url string) *Config {
@ -57,7 +56,7 @@ func TestTokenRequest(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
tok, err := conf.Token(oauth2.NoContext) tok, err := conf.Token(context.Background())
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -91,6 +90,6 @@ func TestTokenRefreshRequest(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
c := conf.Client(oauth2.NoContext) c := conf.Client(context.Background())
c.Get(ts.URL + "/somethingelse") c.Get(ts.URL + "/somethingelse")
} }

View File

@ -5,6 +5,7 @@
package oauth2_test package oauth2_test
import ( import (
"context"
"fmt" "fmt"
"log" "log"
@ -12,6 +13,7 @@ import (
) )
func ExampleConfig() { func ExampleConfig() {
ctx := context.Background()
conf := &oauth2.Config{ conf := &oauth2.Config{
ClientID: "YOUR_CLIENT_ID", ClientID: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET", ClientSecret: "YOUR_CLIENT_SECRET",
@ -35,11 +37,11 @@ func ExampleConfig() {
if _, err := fmt.Scan(&code); err != nil { if _, err := fmt.Scan(&code); err != nil {
log.Fatal(err) log.Fatal(err)
} }
tok, err := conf.Exchange(oauth2.NoContext, code) tok, err := conf.Exchange(ctx, code)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
client := conf.Client(oauth2.NoContext, tok) client := conf.Client(ctx, tok)
client.Get("...") client.Get("...")
} }

View File

@ -5,11 +5,13 @@
package jwt_test package jwt_test
import ( import (
"golang.org/x/oauth2" "context"
"golang.org/x/oauth2/jwt" "golang.org/x/oauth2/jwt"
) )
func ExampleJWTConfig() { func ExampleJWTConfig() {
ctx := context.Background()
conf := &jwt.Config{ conf := &jwt.Config{
Email: "xxx@developer.com", Email: "xxx@developer.com",
// The contents of your RSA private key or your PEM file // The contents of your RSA private key or your PEM file
@ -26,6 +28,6 @@ func ExampleJWTConfig() {
} }
// Initiate an http.Client, the following GET request will be // Initiate an http.Client, the following GET request will be
// authorized and authenticated on the behalf of user@example.com. // authorized and authenticated on the behalf of user@example.com.
client := conf.Client(oauth2.NoContext) client := conf.Client(ctx)
client.Get("...") client.Get("...")
} }

View File

@ -5,11 +5,10 @@
package jwt package jwt
import ( import (
"context"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"golang.org/x/oauth2"
) )
var dummyPrivateKey = []byte(`-----BEGIN RSA PRIVATE KEY----- var dummyPrivateKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
@ -57,7 +56,7 @@ func TestJWTFetch_JSONResponse(t *testing.T) {
PrivateKey: dummyPrivateKey, PrivateKey: dummyPrivateKey,
TokenURL: ts.URL, TokenURL: ts.URL,
} }
tok, err := conf.TokenSource(oauth2.NoContext).Token() tok, err := conf.TokenSource(context.Background()).Token()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -91,7 +90,7 @@ func TestJWTFetch_BadResponse(t *testing.T) {
PrivateKey: dummyPrivateKey, PrivateKey: dummyPrivateKey,
TokenURL: ts.URL, TokenURL: ts.URL,
} }
tok, err := conf.TokenSource(oauth2.NoContext).Token() tok, err := conf.TokenSource(context.Background()).Token()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -124,7 +123,7 @@ func TestJWTFetch_BadResponseType(t *testing.T) {
PrivateKey: dummyPrivateKey, PrivateKey: dummyPrivateKey,
TokenURL: ts.URL, TokenURL: ts.URL,
} }
tok, err := conf.TokenSource(oauth2.NoContext).Token() tok, err := conf.TokenSource(context.Background()).Token()
if err == nil { if err == nil {
t.Error("got a token; expected error") t.Error("got a token; expected error")
if tok.AccessToken != "" { if tok.AccessToken != "" {

View File

@ -21,6 +21,8 @@ import (
// NoContext is the default context you should supply if not using // NoContext is the default context you should supply if not using
// your own context.Context (see https://golang.org/x/net/context). // your own context.Context (see https://golang.org/x/net/context).
//
// Deprecated: Use context.Background() or context.TODO() instead.
var NoContext = context.TODO() var NoContext = context.TODO()
// RegisterBrokenAuthHeaderProvider registers an OAuth2 server // RegisterBrokenAuthHeaderProvider registers an OAuth2 server

View File

@ -97,7 +97,7 @@ func TestExchangeRequest(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
tok, err := conf.Exchange(NoContext, "exchange-code") tok, err := conf.Exchange(context.Background(), "exchange-code")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -141,7 +141,7 @@ func TestExchangeRequest_JSONResponse(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
tok, err := conf.Exchange(NoContext, "exchange-code") tok, err := conf.Exchange(context.Background(), "exchange-code")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -236,7 +236,7 @@ func testExchangeRequest_JSONResponse_expiry(t *testing.T, exp string, expect er
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
t1 := time.Now().Add(day) t1 := time.Now().Add(day)
tok, err := conf.Exchange(NoContext, "exchange-code") tok, err := conf.Exchange(context.Background(), "exchange-code")
t2 := time.Now().Add(day) t2 := time.Now().Add(day)
// Do a fmt.Sprint comparison so either side can be // Do a fmt.Sprint comparison so either side can be
// nil. fmt.Sprint just stringifies them to "<nil>", and no // nil. fmt.Sprint just stringifies them to "<nil>", and no
@ -269,7 +269,7 @@ func TestExchangeRequest_BadResponse(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
tok, err := conf.Exchange(NoContext, "code") tok, err := conf.Exchange(context.Background(), "code")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -285,7 +285,7 @@ func TestExchangeRequest_BadResponseType(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
_, err := conf.Exchange(NoContext, "exchange-code") _, err := conf.Exchange(context.Background(), "exchange-code")
if err == nil { if err == nil {
t.Error("expected error from invalid access_token type") t.Error("expected error from invalid access_token type")
} }
@ -344,7 +344,7 @@ func TestPasswordCredentialsTokenRequest(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
tok, err := conf.PasswordCredentialsToken(NoContext, "user1", "password1") tok, err := conf.PasswordCredentialsToken(context.Background(), "user1", "password1")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -380,7 +380,7 @@ func TestTokenRefreshRequest(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
c := conf.Client(NoContext, &Token{RefreshToken: "REFRESH_TOKEN"}) c := conf.Client(context.Background(), &Token{RefreshToken: "REFRESH_TOKEN"})
c.Get(ts.URL + "/somethingelse") c.Get(ts.URL + "/somethingelse")
} }
@ -403,7 +403,7 @@ func TestFetchWithNoRefreshToken(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
c := conf.Client(NoContext, nil) c := conf.Client(context.Background(), nil)
_, err := c.Get(ts.URL + "/somethingelse") _, err := c.Get(ts.URL + "/somethingelse")
if err == nil { if err == nil {
t.Errorf("Fetch should return an error if no refresh token is set") t.Errorf("Fetch should return an error if no refresh token is set")
@ -420,7 +420,7 @@ func TestRefreshToken_RefreshTokenReplacement(t *testing.T) {
conf := newConf(ts.URL) conf := newConf(ts.URL)
tkr := tokenRefresher{ tkr := tokenRefresher{
conf: conf, conf: conf,
ctx: NoContext, ctx: context.Background(),
refreshToken: "OLD REFRESH TOKEN", refreshToken: "OLD REFRESH TOKEN",
} }
tk, err := tkr.Token() tk, err := tkr.Token()
@ -446,7 +446,7 @@ func TestConfigClientWithToken(t *testing.T) {
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
c := conf.Client(NoContext, tok) c := conf.Client(context.Background(), tok)
req, err := http.NewRequest("GET", ts.URL, nil) req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)