summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastian Ike <bastian.ike@gmail.com>2017-06-28 10:57:20 +0200
committerChris Broadfoot <cbro@golang.org>2017-06-29 18:09:26 +0000
commit626d87b99350690f6eb2b92c2f717221cdaf6f9a (patch)
treeff683fb10507f0c041a7dead7251ccd05de47c16
parent5432cc9688e6250a0dd8f5a5f4c781d92b398be6 (diff)
downloadgolang-x-oauth2-626d87b99350690f6eb2b92c2f717221cdaf6f9a.tar.gz
internal: Use provided context in subsequent request
Currently the HTTP request does not set the given context. This change sets the context (if not nil) on the request. Change-Id: I4bb21636d05050a68ba70ce92f9bf9ba608fbfad Reviewed-on: https://go-review.googlesource.com/45370 Run-TryBot: Chris Broadfoot <cbro@golang.org> Reviewed-by: Jaana Burcu Dogan <jbd@google.com> Reviewed-by: Chris Broadfoot <cbro@golang.org>
-rw-r--r--internal/token.go3
-rw-r--r--internal/token_test.go24
2 files changed, 26 insertions, 1 deletions
diff --git a/internal/token.go b/internal/token.go
index efe7af5..766cfb6 100644
--- a/internal/token.go
+++ b/internal/token.go
@@ -18,6 +18,7 @@ import (
"time"
"golang.org/x/net/context"
+ "golang.org/x/net/context/ctxhttp"
)
// Token represents the crendentials used to authorize
@@ -189,7 +190,7 @@ func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string,
if !bustedAuth {
req.SetBasicAuth(clientID, clientSecret)
}
- r, err := hc.Do(req)
+ r, err := ctxhttp.Do(ctx, hc, req)
if err != nil {
return nil, err
}
diff --git a/internal/token_test.go b/internal/token_test.go
index 882de11..c19a462 100644
--- a/internal/token_test.go
+++ b/internal/token_test.go
@@ -79,3 +79,27 @@ func TestProviderAuthHeaderWorksDomain(t *testing.T) {
}
}
}
+
+func TestRetrieveTokenWithContexts(t *testing.T) {
+ const clientID = "client-id"
+
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
+ defer ts.Close()
+
+ _, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{})
+ if err != nil {
+ t.Errorf("RetrieveToken (with background context) = %v; want no error", err)
+ }
+
+ ctx, cancelfunc := context.WithCancel(context.Background())
+
+ cancellingts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ cancelfunc()
+ }))
+ defer cancellingts.Close()
+
+ _, err = RetrieveToken(ctx, clientID, "", cancellingts.URL, url.Values{})
+ if err == nil {
+ t.Errorf("RetrieveToken (with cancelled context) = nil; want error")
+ }
+}