blob: c08862ae636975d90eef6324c59031d057311581 [file] [log] [blame]
Andrew Gerrand038cb4a2015-08-27 10:42:02 +10001// Copyright 2014 The Go Authors. All rights reserved.
Aaron Torresa8c019d2015-01-16 14:43:33 -08002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Aaron Torresa8c019d2015-01-16 14:43:33 -08005package internal
6
7import (
Antoine GIRARDc453e0c2018-11-01 15:53:36 +00008 "context"
Brad Fitzpatricke64efc72019-02-12 00:20:02 +00009 "fmt"
Brad Fitzpatrickea8c6732017-12-05 22:32:25 +000010 "io"
Brad Fitzpatricke64efc72019-02-12 00:20:02 +000011 "math"
Jaana Burcu Dogan1611bb42017-03-13 12:46:53 -070012 "net/http"
13 "net/http/httptest"
14 "net/url"
Aaron Torresa8c019d2015-01-16 14:43:33 -080015 "testing"
16)
17
Brad Fitzpatrick80673b42019-01-14 22:52:49 +000018func TestRetrieveToken_InParams(t *testing.T) {
Brad Fitzpatricka835fc42023-08-03 09:40:32 -070019 styleCache := new(AuthStyleCache)
Jaana Burcu Dogan1611bb42017-03-13 12:46:53 -070020 const clientID = "client-id"
Jaana Burcu Dogan1611bb42017-03-13 12:46:53 -070021 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22 if got, want := r.FormValue("client_id"), clientID; got != want {
23 t.Errorf("client_id = %q; want %q", got, want)
24 }
25 if got, want := r.FormValue("client_secret"), ""; got != want {
26 t.Errorf("client_secret = %q; want empty", got)
27 }
Ross Lightee2bad92017-12-28 11:07:42 -080028 w.Header().Set("Content-Type", "application/json")
29 io.WriteString(w, `{"access_token": "ACCESS_TOKEN", "token_type": "bearer"}`)
Jaana Burcu Dogan1611bb42017-03-13 12:46:53 -070030 }))
31 defer ts.Close()
Brad Fitzpatricka835fc42023-08-03 09:40:32 -070032 _, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{}, AuthStyleInParams, styleCache)
Jaana Burcu Dogan1611bb42017-03-13 12:46:53 -070033 if err != nil {
34 t.Errorf("RetrieveToken = %v; want no error", err)
35 }
36}
37
Bastian Ike626d87b2017-06-28 10:57:20 +020038func TestRetrieveTokenWithContexts(t *testing.T) {
Brad Fitzpatricka835fc42023-08-03 09:40:32 -070039 styleCache := new(AuthStyleCache)
Bastian Ike626d87b2017-06-28 10:57:20 +020040 const clientID = "client-id"
41
Brad Fitzpatrickea8c6732017-12-05 22:32:25 +000042 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Ross Lightee2bad92017-12-28 11:07:42 -080043 w.Header().Set("Content-Type", "application/json")
44 io.WriteString(w, `{"access_token": "ACCESS_TOKEN", "token_type": "bearer"}`)
Brad Fitzpatrickea8c6732017-12-05 22:32:25 +000045 }))
Bastian Ike626d87b2017-06-28 10:57:20 +020046 defer ts.Close()
47
Brad Fitzpatricka835fc42023-08-03 09:40:32 -070048 _, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{}, AuthStyleUnknown, styleCache)
Bastian Ike626d87b2017-06-28 10:57:20 +020049 if err != nil {
50 t.Errorf("RetrieveToken (with background context) = %v; want no error", err)
51 }
52
Ross Light40a09c62017-12-21 13:15:27 -080053 retrieved := make(chan struct{})
Bastian Ike626d87b2017-06-28 10:57:20 +020054 cancellingts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Ross Light40a09c62017-12-21 13:15:27 -080055 <-retrieved
Bastian Ike626d87b2017-06-28 10:57:20 +020056 }))
57 defer cancellingts.Close()
58
Ross Light40a09c62017-12-21 13:15:27 -080059 ctx, cancel := context.WithCancel(context.Background())
60 cancel()
Brad Fitzpatricka835fc42023-08-03 09:40:32 -070061 _, err = RetrieveToken(ctx, clientID, "", cancellingts.URL, url.Values{}, AuthStyleUnknown, styleCache)
Ross Light40a09c62017-12-21 13:15:27 -080062 close(retrieved)
Bastian Ike626d87b2017-06-28 10:57:20 +020063 if err == nil {
64 t.Errorf("RetrieveToken (with cancelled context) = nil; want error")
65 }
66}
Brad Fitzpatricke64efc72019-02-12 00:20:02 +000067
68func TestExpiresInUpperBound(t *testing.T) {
69 var e expirationTime
70 if err := e.UnmarshalJSON([]byte(fmt.Sprint(int64(math.MaxInt32) + 1))); err != nil {
71 t.Fatal(err)
72 }
73 const want = math.MaxInt32
74 if e != want {
75 t.Errorf("expiration time = %v; want %v", e, want)
76 }
77}