You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

98 lines
3.1 KiB

  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package clientcredentials
  5. import (
  6. "context"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "net/url"
  11. "testing"
  12. )
  13. func newConf(serverURL string) *Config {
  14. return &Config{
  15. ClientID: "CLIENT_ID",
  16. ClientSecret: "CLIENT_SECRET",
  17. Scopes: []string{"scope1", "scope2"},
  18. TokenURL: serverURL + "/token",
  19. EndpointParams: url.Values{"audience": {"audience1"}},
  20. }
  21. }
  22. type mockTransport struct {
  23. rt func(req *http.Request) (resp *http.Response, err error)
  24. }
  25. func (t *mockTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
  26. return t.rt(req)
  27. }
  28. func TestTokenRequest(t *testing.T) {
  29. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  30. if r.URL.String() != "/token" {
  31. t.Errorf("authenticate client request URL = %q; want %q", r.URL, "/token")
  32. }
  33. headerAuth := r.Header.Get("Authorization")
  34. if headerAuth != "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ=" {
  35. t.Errorf("Unexpected authorization header, %v is found.", headerAuth)
  36. }
  37. if got, want := r.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; got != want {
  38. t.Errorf("Content-Type header = %q; want %q", got, want)
  39. }
  40. body, err := ioutil.ReadAll(r.Body)
  41. if err != nil {
  42. r.Body.Close()
  43. }
  44. if err != nil {
  45. t.Errorf("failed reading request body: %s.", err)
  46. }
  47. if string(body) != "audience=audience1&grant_type=client_credentials&scope=scope1+scope2" {
  48. t.Errorf("payload = %q; want %q", string(body), "grant_type=client_credentials&scope=scope1+scope2")
  49. }
  50. w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
  51. w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&token_type=bearer"))
  52. }))
  53. defer ts.Close()
  54. conf := newConf(ts.URL)
  55. tok, err := conf.Token(context.Background())
  56. if err != nil {
  57. t.Error(err)
  58. }
  59. if !tok.Valid() {
  60. t.Fatalf("token invalid. got: %#v", tok)
  61. }
  62. if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
  63. t.Errorf("Access token = %q; want %q", tok.AccessToken, "90d64460d14870c08c81352a05dedd3465940a7c")
  64. }
  65. if tok.TokenType != "bearer" {
  66. t.Errorf("token type = %q; want %q", tok.TokenType, "bearer")
  67. }
  68. }
  69. func TestTokenRefreshRequest(t *testing.T) {
  70. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  71. if r.URL.String() == "/somethingelse" {
  72. return
  73. }
  74. if r.URL.String() != "/token" {
  75. t.Errorf("Unexpected token refresh request URL, %v is found.", r.URL)
  76. }
  77. headerContentType := r.Header.Get("Content-Type")
  78. if headerContentType != "application/x-www-form-urlencoded" {
  79. t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
  80. }
  81. body, _ := ioutil.ReadAll(r.Body)
  82. if string(body) != "audience=audience1&grant_type=client_credentials&scope=scope1+scope2" {
  83. t.Errorf("Unexpected refresh token payload, %v is found.", string(body))
  84. }
  85. }))
  86. defer ts.Close()
  87. conf := newConf(ts.URL)
  88. c := conf.Client(context.Background())
  89. c.Get(ts.URL + "/somethingelse")
  90. }