clientcredentials_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "io/ioutil"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "golang.org/x/oauth2"
  11. )
  12. func newConf(url string) *Config {
  13. return &Config{
  14. ClientID: "CLIENT_ID",
  15. ClientSecret: "CLIENT_SECRET",
  16. Scopes: []string{"scope1", "scope2"},
  17. TokenURL: url + "/token",
  18. }
  19. }
  20. type mockTransport struct {
  21. rt func(req *http.Request) (resp *http.Response, err error)
  22. }
  23. func (t *mockTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
  24. return t.rt(req)
  25. }
  26. func TestTokenRequest(t *testing.T) {
  27. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  28. if r.URL.String() != "/token" {
  29. t.Errorf("authenticate client request URL = %q; want %q", r.URL, "/token")
  30. }
  31. headerAuth := r.Header.Get("Authorization")
  32. if headerAuth != "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ=" {
  33. t.Errorf("Unexpected authorization header, %v is found.", headerAuth)
  34. }
  35. if got, want := r.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; got != want {
  36. t.Errorf("Content-Type header = %q; want %q", got, want)
  37. }
  38. body, err := ioutil.ReadAll(r.Body)
  39. if err != nil {
  40. r.Body.Close()
  41. }
  42. if err != nil {
  43. t.Errorf("failed reading request body: %s.", err)
  44. }
  45. if string(body) != "client_id=CLIENT_ID&grant_type=client_credentials&scope=scope1+scope2" {
  46. t.Errorf("payload = %q; want %q", string(body), "client_id=CLIENT_ID&grant_type=client_credentials&scope=scope1+scope2")
  47. }
  48. w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
  49. w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&token_type=bearer"))
  50. }))
  51. defer ts.Close()
  52. conf := newConf(ts.URL)
  53. tok, err := conf.Token(oauth2.NoContext)
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. if !tok.Valid() {
  58. t.Fatalf("token invalid. got: %#v", tok)
  59. }
  60. if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
  61. t.Errorf("Access token = %q; want %q", tok.AccessToken, "90d64460d14870c08c81352a05dedd3465940a7c")
  62. }
  63. if tok.TokenType != "bearer" {
  64. t.Errorf("token type = %q; want %q", tok.TokenType, "bearer")
  65. }
  66. }
  67. func TestTokenRefreshRequest(t *testing.T) {
  68. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  69. if r.URL.String() == "/somethingelse" {
  70. return
  71. }
  72. if r.URL.String() != "/token" {
  73. t.Errorf("Unexpected token refresh request URL, %v is found.", r.URL)
  74. }
  75. headerContentType := r.Header.Get("Content-Type")
  76. if headerContentType != "application/x-www-form-urlencoded" {
  77. t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
  78. }
  79. body, _ := ioutil.ReadAll(r.Body)
  80. if string(body) != "client_id=CLIENT_ID&grant_type=client_credentials&scope=scope1+scope2" {
  81. t.Errorf("Unexpected refresh token payload, %v is found.", string(body))
  82. }
  83. }))
  84. defer ts.Close()
  85. conf := newConf(ts.URL)
  86. c := conf.Client(oauth2.NoContext)
  87. c.Get(ts.URL + "/somethingelse")
  88. }