Skip to content

Commit c10ba27

Browse files
committed
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 <[email protected]>
1 parent 54f42ed commit c10ba27

File tree

6 files changed

+27
-23
lines changed

6 files changed

+27
-23
lines changed

clientcredentials/clientcredentials_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
package clientcredentials
66

77
import (
8+
"context"
89
"io/ioutil"
910
"net/http"
1011
"net/http/httptest"
1112
"testing"
12-
13-
"golang.org/x/oauth2"
1413
)
1514

1615
func newConf(url string) *Config {
@@ -57,7 +56,7 @@ func TestTokenRequest(t *testing.T) {
5756
}))
5857
defer ts.Close()
5958
conf := newConf(ts.URL)
60-
tok, err := conf.Token(oauth2.NoContext)
59+
tok, err := conf.Token(context.Background())
6160
if err != nil {
6261
t.Error(err)
6362
}
@@ -91,6 +90,6 @@ func TestTokenRefreshRequest(t *testing.T) {
9190
}))
9291
defer ts.Close()
9392
conf := newConf(ts.URL)
94-
c := conf.Client(oauth2.NoContext)
93+
c := conf.Client(context.Background())
9594
c.Get(ts.URL + "/somethingelse")
9695
}

example_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
package oauth2_test
66

77
import (
8+
"context"
89
"fmt"
910
"log"
1011

1112
"golang.org/x/oauth2"
1213
)
1314

1415
func ExampleConfig() {
16+
ctx := context.Background()
1517
conf := &oauth2.Config{
1618
ClientID: "YOUR_CLIENT_ID",
1719
ClientSecret: "YOUR_CLIENT_SECRET",
@@ -35,11 +37,11 @@ func ExampleConfig() {
3537
if _, err := fmt.Scan(&code); err != nil {
3638
log.Fatal(err)
3739
}
38-
tok, err := conf.Exchange(oauth2.NoContext, code)
40+
tok, err := conf.Exchange(ctx, code)
3941
if err != nil {
4042
log.Fatal(err)
4143
}
4244

43-
client := conf.Client(oauth2.NoContext, tok)
45+
client := conf.Client(ctx, tok)
4446
client.Get("...")
4547
}

jwt/example_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
package jwt_test
66

77
import (
8-
"golang.org/x/oauth2"
8+
"context"
9+
910
"golang.org/x/oauth2/jwt"
1011
)
1112

1213
func ExampleJWTConfig() {
14+
ctx := context.Background()
1315
conf := &jwt.Config{
1416
1517
// The contents of your RSA private key or your PEM file
@@ -26,6 +28,6 @@ func ExampleJWTConfig() {
2628
}
2729
// Initiate an http.Client, the following GET request will be
2830
// authorized and authenticated on the behalf of [email protected].
29-
client := conf.Client(oauth2.NoContext)
31+
client := conf.Client(ctx)
3032
client.Get("...")
3133
}

jwt/jwt_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
package jwt
66

77
import (
8+
"context"
89
"net/http"
910
"net/http/httptest"
1011
"testing"
11-
12-
"golang.org/x/oauth2"
1312
)
1413

1514
var dummyPrivateKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
@@ -57,7 +56,7 @@ func TestJWTFetch_JSONResponse(t *testing.T) {
5756
PrivateKey: dummyPrivateKey,
5857
TokenURL: ts.URL,
5958
}
60-
tok, err := conf.TokenSource(oauth2.NoContext).Token()
59+
tok, err := conf.TokenSource(context.Background()).Token()
6160
if err != nil {
6261
t.Fatal(err)
6362
}
@@ -91,7 +90,7 @@ func TestJWTFetch_BadResponse(t *testing.T) {
9190
PrivateKey: dummyPrivateKey,
9291
TokenURL: ts.URL,
9392
}
94-
tok, err := conf.TokenSource(oauth2.NoContext).Token()
93+
tok, err := conf.TokenSource(context.Background()).Token()
9594
if err != nil {
9695
t.Fatal(err)
9796
}
@@ -124,7 +123,7 @@ func TestJWTFetch_BadResponseType(t *testing.T) {
124123
PrivateKey: dummyPrivateKey,
125124
TokenURL: ts.URL,
126125
}
127-
tok, err := conf.TokenSource(oauth2.NoContext).Token()
126+
tok, err := conf.TokenSource(context.Background()).Token()
128127
if err == nil {
129128
t.Error("got a token; expected error")
130129
if tok.AccessToken != "" {

oauth2.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
// NoContext is the default context you should supply if not using
2323
// your own context.Context (see https://golang.org/x/net/context).
24+
//
25+
// Deprecated: Use context.Background() or context.TODO() instead.
2426
var NoContext = context.TODO()
2527

2628
// RegisterBrokenAuthHeaderProvider registers an OAuth2 server

oauth2_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestExchangeRequest(t *testing.T) {
9797
}))
9898
defer ts.Close()
9999
conf := newConf(ts.URL)
100-
tok, err := conf.Exchange(NoContext, "exchange-code")
100+
tok, err := conf.Exchange(context.Background(), "exchange-code")
101101
if err != nil {
102102
t.Error(err)
103103
}
@@ -141,7 +141,7 @@ func TestExchangeRequest_JSONResponse(t *testing.T) {
141141
}))
142142
defer ts.Close()
143143
conf := newConf(ts.URL)
144-
tok, err := conf.Exchange(NoContext, "exchange-code")
144+
tok, err := conf.Exchange(context.Background(), "exchange-code")
145145
if err != nil {
146146
t.Error(err)
147147
}
@@ -236,7 +236,7 @@ func testExchangeRequest_JSONResponse_expiry(t *testing.T, exp string, expect er
236236
defer ts.Close()
237237
conf := newConf(ts.URL)
238238
t1 := time.Now().Add(day)
239-
tok, err := conf.Exchange(NoContext, "exchange-code")
239+
tok, err := conf.Exchange(context.Background(), "exchange-code")
240240
t2 := time.Now().Add(day)
241241
// Do a fmt.Sprint comparison so either side can be
242242
// nil. fmt.Sprint just stringifies them to "<nil>", and no
@@ -269,7 +269,7 @@ func TestExchangeRequest_BadResponse(t *testing.T) {
269269
}))
270270
defer ts.Close()
271271
conf := newConf(ts.URL)
272-
tok, err := conf.Exchange(NoContext, "code")
272+
tok, err := conf.Exchange(context.Background(), "code")
273273
if err != nil {
274274
t.Fatal(err)
275275
}
@@ -285,7 +285,7 @@ func TestExchangeRequest_BadResponseType(t *testing.T) {
285285
}))
286286
defer ts.Close()
287287
conf := newConf(ts.URL)
288-
_, err := conf.Exchange(NoContext, "exchange-code")
288+
_, err := conf.Exchange(context.Background(), "exchange-code")
289289
if err == nil {
290290
t.Error("expected error from invalid access_token type")
291291
}
@@ -344,7 +344,7 @@ func TestPasswordCredentialsTokenRequest(t *testing.T) {
344344
}))
345345
defer ts.Close()
346346
conf := newConf(ts.URL)
347-
tok, err := conf.PasswordCredentialsToken(NoContext, "user1", "password1")
347+
tok, err := conf.PasswordCredentialsToken(context.Background(), "user1", "password1")
348348
if err != nil {
349349
t.Error(err)
350350
}
@@ -380,7 +380,7 @@ func TestTokenRefreshRequest(t *testing.T) {
380380
}))
381381
defer ts.Close()
382382
conf := newConf(ts.URL)
383-
c := conf.Client(NoContext, &Token{RefreshToken: "REFRESH_TOKEN"})
383+
c := conf.Client(context.Background(), &Token{RefreshToken: "REFRESH_TOKEN"})
384384
c.Get(ts.URL + "/somethingelse")
385385
}
386386

@@ -403,7 +403,7 @@ func TestFetchWithNoRefreshToken(t *testing.T) {
403403
}))
404404
defer ts.Close()
405405
conf := newConf(ts.URL)
406-
c := conf.Client(NoContext, nil)
406+
c := conf.Client(context.Background(), nil)
407407
_, err := c.Get(ts.URL + "/somethingelse")
408408
if err == nil {
409409
t.Errorf("Fetch should return an error if no refresh token is set")
@@ -420,7 +420,7 @@ func TestRefreshToken_RefreshTokenReplacement(t *testing.T) {
420420
conf := newConf(ts.URL)
421421
tkr := tokenRefresher{
422422
conf: conf,
423-
ctx: NoContext,
423+
ctx: context.Background(),
424424
refreshToken: "OLD REFRESH TOKEN",
425425
}
426426
tk, err := tkr.Token()
@@ -446,7 +446,7 @@ func TestConfigClientWithToken(t *testing.T) {
446446
defer ts.Close()
447447
conf := newConf(ts.URL)
448448

449-
c := conf.Client(NoContext, tok)
449+
c := conf.Client(context.Background(), tok)
450450
req, err := http.NewRequest("GET", ts.URL, nil)
451451
if err != nil {
452452
t.Error(err)

0 commit comments

Comments
 (0)