Andrew Gerrand | 038cb4a | 2015-08-27 10:42:02 +1000 | [diff] [blame] | 1 | // Copyright 2014 The Go Authors. All rights reserved. |
Aaron Torres | a8c019d | 2015-01-16 14:43:33 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Aaron Torres | a8c019d | 2015-01-16 14:43:33 -0800 | [diff] [blame] | 5 | package internal |
| 6 | |
| 7 | import ( |
Antoine GIRARD | c453e0c | 2018-11-01 15:53:36 +0000 | [diff] [blame] | 8 | "context" |
Brad Fitzpatrick | e64efc7 | 2019-02-12 00:20:02 +0000 | [diff] [blame] | 9 | "fmt" |
Brad Fitzpatrick | ea8c673 | 2017-12-05 22:32:25 +0000 | [diff] [blame] | 10 | "io" |
Brad Fitzpatrick | e64efc7 | 2019-02-12 00:20:02 +0000 | [diff] [blame] | 11 | "math" |
Jaana Burcu Dogan | 1611bb4 | 2017-03-13 12:46:53 -0700 | [diff] [blame] | 12 | "net/http" |
| 13 | "net/http/httptest" |
| 14 | "net/url" |
Aaron Torres | a8c019d | 2015-01-16 14:43:33 -0800 | [diff] [blame] | 15 | "testing" |
| 16 | ) |
| 17 | |
Brad Fitzpatrick | 80673b4 | 2019-01-14 22:52:49 +0000 | [diff] [blame] | 18 | func TestRetrieveToken_InParams(t *testing.T) { |
Brad Fitzpatrick | a835fc4 | 2023-08-03 09:40:32 -0700 | [diff] [blame] | 19 | styleCache := new(AuthStyleCache) |
Jaana Burcu Dogan | 1611bb4 | 2017-03-13 12:46:53 -0700 | [diff] [blame] | 20 | const clientID = "client-id" |
Jaana Burcu Dogan | 1611bb4 | 2017-03-13 12:46:53 -0700 | [diff] [blame] | 21 | 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 Light | ee2bad9 | 2017-12-28 11:07:42 -0800 | [diff] [blame] | 28 | w.Header().Set("Content-Type", "application/json") |
| 29 | io.WriteString(w, `{"access_token": "ACCESS_TOKEN", "token_type": "bearer"}`) |
Jaana Burcu Dogan | 1611bb4 | 2017-03-13 12:46:53 -0700 | [diff] [blame] | 30 | })) |
| 31 | defer ts.Close() |
Brad Fitzpatrick | a835fc4 | 2023-08-03 09:40:32 -0700 | [diff] [blame] | 32 | _, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{}, AuthStyleInParams, styleCache) |
Jaana Burcu Dogan | 1611bb4 | 2017-03-13 12:46:53 -0700 | [diff] [blame] | 33 | if err != nil { |
| 34 | t.Errorf("RetrieveToken = %v; want no error", err) |
| 35 | } |
| 36 | } |
| 37 | |
Bastian Ike | 626d87b | 2017-06-28 10:57:20 +0200 | [diff] [blame] | 38 | func TestRetrieveTokenWithContexts(t *testing.T) { |
Brad Fitzpatrick | a835fc4 | 2023-08-03 09:40:32 -0700 | [diff] [blame] | 39 | styleCache := new(AuthStyleCache) |
Bastian Ike | 626d87b | 2017-06-28 10:57:20 +0200 | [diff] [blame] | 40 | const clientID = "client-id" |
| 41 | |
Brad Fitzpatrick | ea8c673 | 2017-12-05 22:32:25 +0000 | [diff] [blame] | 42 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
Ross Light | ee2bad9 | 2017-12-28 11:07:42 -0800 | [diff] [blame] | 43 | w.Header().Set("Content-Type", "application/json") |
| 44 | io.WriteString(w, `{"access_token": "ACCESS_TOKEN", "token_type": "bearer"}`) |
Brad Fitzpatrick | ea8c673 | 2017-12-05 22:32:25 +0000 | [diff] [blame] | 45 | })) |
Bastian Ike | 626d87b | 2017-06-28 10:57:20 +0200 | [diff] [blame] | 46 | defer ts.Close() |
| 47 | |
Brad Fitzpatrick | a835fc4 | 2023-08-03 09:40:32 -0700 | [diff] [blame] | 48 | _, err := RetrieveToken(context.Background(), clientID, "", ts.URL, url.Values{}, AuthStyleUnknown, styleCache) |
Bastian Ike | 626d87b | 2017-06-28 10:57:20 +0200 | [diff] [blame] | 49 | if err != nil { |
| 50 | t.Errorf("RetrieveToken (with background context) = %v; want no error", err) |
| 51 | } |
| 52 | |
Ross Light | 40a09c6 | 2017-12-21 13:15:27 -0800 | [diff] [blame] | 53 | retrieved := make(chan struct{}) |
Bastian Ike | 626d87b | 2017-06-28 10:57:20 +0200 | [diff] [blame] | 54 | cancellingts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
Ross Light | 40a09c6 | 2017-12-21 13:15:27 -0800 | [diff] [blame] | 55 | <-retrieved |
Bastian Ike | 626d87b | 2017-06-28 10:57:20 +0200 | [diff] [blame] | 56 | })) |
| 57 | defer cancellingts.Close() |
| 58 | |
Ross Light | 40a09c6 | 2017-12-21 13:15:27 -0800 | [diff] [blame] | 59 | ctx, cancel := context.WithCancel(context.Background()) |
| 60 | cancel() |
Brad Fitzpatrick | a835fc4 | 2023-08-03 09:40:32 -0700 | [diff] [blame] | 61 | _, err = RetrieveToken(ctx, clientID, "", cancellingts.URL, url.Values{}, AuthStyleUnknown, styleCache) |
Ross Light | 40a09c6 | 2017-12-21 13:15:27 -0800 | [diff] [blame] | 62 | close(retrieved) |
Bastian Ike | 626d87b | 2017-06-28 10:57:20 +0200 | [diff] [blame] | 63 | if err == nil { |
| 64 | t.Errorf("RetrieveToken (with cancelled context) = nil; want error") |
| 65 | } |
| 66 | } |
Brad Fitzpatrick | e64efc7 | 2019-02-12 00:20:02 +0000 | [diff] [blame] | 67 | |
| 68 | func 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 | } |