Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

564 wiersze
18 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 oauth2
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "net/http"
  12. "net/http/httptest"
  13. "net/url"
  14. "testing"
  15. "time"
  16. "golang.org/x/oauth2/internal"
  17. )
  18. type mockTransport struct {
  19. rt func(req *http.Request) (resp *http.Response, err error)
  20. }
  21. func (t *mockTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
  22. return t.rt(req)
  23. }
  24. func newConf(url string) *Config {
  25. return &Config{
  26. ClientID: "CLIENT_ID",
  27. ClientSecret: "CLIENT_SECRET",
  28. RedirectURL: "REDIRECT_URL",
  29. Scopes: []string{"scope1", "scope2"},
  30. Endpoint: Endpoint{
  31. AuthURL: url + "/auth",
  32. TokenURL: url + "/token",
  33. },
  34. }
  35. }
  36. func TestAuthCodeURL(t *testing.T) {
  37. conf := newConf("server")
  38. url := conf.AuthCodeURL("foo", AccessTypeOffline, ApprovalForce)
  39. const want = "server/auth?access_type=offline&approval_prompt=force&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=foo"
  40. if got := url; got != want {
  41. t.Errorf("got auth code URL = %q; want %q", got, want)
  42. }
  43. }
  44. func TestAuthCodeURL_CustomParam(t *testing.T) {
  45. conf := newConf("server")
  46. param := SetAuthURLParam("foo", "bar")
  47. url := conf.AuthCodeURL("baz", param)
  48. const want = "server/auth?client_id=CLIENT_ID&foo=bar&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=baz"
  49. if got := url; got != want {
  50. t.Errorf("got auth code = %q; want %q", got, want)
  51. }
  52. }
  53. func TestAuthCodeURL_Optional(t *testing.T) {
  54. conf := &Config{
  55. ClientID: "CLIENT_ID",
  56. Endpoint: Endpoint{
  57. AuthURL: "/auth-url",
  58. TokenURL: "/token-url",
  59. },
  60. }
  61. url := conf.AuthCodeURL("")
  62. const want = "/auth-url?client_id=CLIENT_ID&response_type=code"
  63. if got := url; got != want {
  64. t.Fatalf("got auth code = %q; want %q", got, want)
  65. }
  66. }
  67. func TestURLUnsafeClientConfig(t *testing.T) {
  68. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  69. if got, want := r.Header.Get("Authorization"), "Basic Q0xJRU5UX0lEJTNGJTNGOkNMSUVOVF9TRUNSRVQlM0YlM0Y="; got != want {
  70. t.Errorf("Authorization header = %q; want %q", got, want)
  71. }
  72. w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
  73. w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
  74. }))
  75. defer ts.Close()
  76. conf := newConf(ts.URL)
  77. conf.ClientID = "CLIENT_ID??"
  78. conf.ClientSecret = "CLIENT_SECRET??"
  79. _, err := conf.Exchange(context.Background(), "exchange-code")
  80. if err != nil {
  81. t.Error(err)
  82. }
  83. }
  84. func TestExchangeRequest(t *testing.T) {
  85. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  86. if r.URL.String() != "/token" {
  87. t.Errorf("Unexpected exchange request URL %q", r.URL)
  88. }
  89. headerAuth := r.Header.Get("Authorization")
  90. if want := "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ="; headerAuth != want {
  91. t.Errorf("Unexpected authorization header %q, want %q", headerAuth, want)
  92. }
  93. headerContentType := r.Header.Get("Content-Type")
  94. if headerContentType != "application/x-www-form-urlencoded" {
  95. t.Errorf("Unexpected Content-Type header %q", headerContentType)
  96. }
  97. body, err := ioutil.ReadAll(r.Body)
  98. if err != nil {
  99. t.Errorf("Failed reading request body: %s.", err)
  100. }
  101. if string(body) != "code=exchange-code&grant_type=authorization_code&redirect_uri=REDIRECT_URL" {
  102. t.Errorf("Unexpected exchange payload; got %q", body)
  103. }
  104. w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
  105. w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
  106. }))
  107. defer ts.Close()
  108. conf := newConf(ts.URL)
  109. tok, err := conf.Exchange(context.Background(), "exchange-code")
  110. if err != nil {
  111. t.Error(err)
  112. }
  113. if !tok.Valid() {
  114. t.Fatalf("Token invalid. Got: %#v", tok)
  115. }
  116. if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
  117. t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
  118. }
  119. if tok.TokenType != "bearer" {
  120. t.Errorf("Unexpected token type, %#v.", tok.TokenType)
  121. }
  122. scope := tok.Extra("scope")
  123. if scope != "user" {
  124. t.Errorf("Unexpected value for scope: %v", scope)
  125. }
  126. }
  127. func TestExchangeRequest_CustomParam(t *testing.T) {
  128. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  129. if r.URL.String() != "/token" {
  130. t.Errorf("Unexpected exchange request URL, %v is found.", r.URL)
  131. }
  132. headerAuth := r.Header.Get("Authorization")
  133. if headerAuth != "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ=" {
  134. t.Errorf("Unexpected authorization header, %v is found.", headerAuth)
  135. }
  136. headerContentType := r.Header.Get("Content-Type")
  137. if headerContentType != "application/x-www-form-urlencoded" {
  138. t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
  139. }
  140. body, err := ioutil.ReadAll(r.Body)
  141. if err != nil {
  142. t.Errorf("Failed reading request body: %s.", err)
  143. }
  144. if string(body) != "code=exchange-code&foo=bar&grant_type=authorization_code&redirect_uri=REDIRECT_URL" {
  145. t.Errorf("Unexpected exchange payload, %v is found.", string(body))
  146. }
  147. w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
  148. w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
  149. }))
  150. defer ts.Close()
  151. conf := newConf(ts.URL)
  152. param := SetAuthURLParam("foo", "bar")
  153. tok, err := conf.Exchange(context.Background(), "exchange-code", param)
  154. if err != nil {
  155. t.Error(err)
  156. }
  157. if !tok.Valid() {
  158. t.Fatalf("Token invalid. Got: %#v", tok)
  159. }
  160. if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
  161. t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
  162. }
  163. if tok.TokenType != "bearer" {
  164. t.Errorf("Unexpected token type, %#v.", tok.TokenType)
  165. }
  166. scope := tok.Extra("scope")
  167. if scope != "user" {
  168. t.Errorf("Unexpected value for scope: %v", scope)
  169. }
  170. }
  171. func TestExchangeRequest_JSONResponse(t *testing.T) {
  172. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  173. if r.URL.String() != "/token" {
  174. t.Errorf("Unexpected exchange request URL, %v is found.", r.URL)
  175. }
  176. headerAuth := r.Header.Get("Authorization")
  177. if headerAuth != "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ=" {
  178. t.Errorf("Unexpected authorization header, %v is found.", headerAuth)
  179. }
  180. headerContentType := r.Header.Get("Content-Type")
  181. if headerContentType != "application/x-www-form-urlencoded" {
  182. t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
  183. }
  184. body, err := ioutil.ReadAll(r.Body)
  185. if err != nil {
  186. t.Errorf("Failed reading request body: %s.", err)
  187. }
  188. if string(body) != "code=exchange-code&grant_type=authorization_code&redirect_uri=REDIRECT_URL" {
  189. t.Errorf("Unexpected exchange payload, %v is found.", string(body))
  190. }
  191. w.Header().Set("Content-Type", "application/json")
  192. w.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer", "expires_in": 86400}`))
  193. }))
  194. defer ts.Close()
  195. conf := newConf(ts.URL)
  196. tok, err := conf.Exchange(context.Background(), "exchange-code")
  197. if err != nil {
  198. t.Error(err)
  199. }
  200. if !tok.Valid() {
  201. t.Fatalf("Token invalid. Got: %#v", tok)
  202. }
  203. if tok.AccessToken != "90d64460d14870c08c81352a05dedd3465940a7c" {
  204. t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
  205. }
  206. if tok.TokenType != "bearer" {
  207. t.Errorf("Unexpected token type, %#v.", tok.TokenType)
  208. }
  209. scope := tok.Extra("scope")
  210. if scope != "user" {
  211. t.Errorf("Unexpected value for scope: %v", scope)
  212. }
  213. expiresIn := tok.Extra("expires_in")
  214. if expiresIn != float64(86400) {
  215. t.Errorf("Unexpected non-numeric value for expires_in: %v", expiresIn)
  216. }
  217. }
  218. func TestExtraValueRetrieval(t *testing.T) {
  219. values := url.Values{}
  220. kvmap := map[string]string{
  221. "scope": "user", "token_type": "bearer", "expires_in": "86400.92",
  222. "server_time": "1443571905.5606415", "referer_ip": "10.0.0.1",
  223. "etag": "\"afZYj912P4alikMz_P11982\"", "request_id": "86400",
  224. "untrimmed": " untrimmed ",
  225. }
  226. for key, value := range kvmap {
  227. values.Set(key, value)
  228. }
  229. tok := Token{raw: values}
  230. scope := tok.Extra("scope")
  231. if got, want := scope, "user"; got != want {
  232. t.Errorf("got scope = %q; want %q", got, want)
  233. }
  234. serverTime := tok.Extra("server_time")
  235. if got, want := serverTime, 1443571905.5606415; got != want {
  236. t.Errorf("got server_time value = %v; want %v", got, want)
  237. }
  238. refererIP := tok.Extra("referer_ip")
  239. if got, want := refererIP, "10.0.0.1"; got != want {
  240. t.Errorf("got referer_ip value = %v, want %v", got, want)
  241. }
  242. expiresIn := tok.Extra("expires_in")
  243. if got, want := expiresIn, 86400.92; got != want {
  244. t.Errorf("got expires_in value = %v, want %v", got, want)
  245. }
  246. requestID := tok.Extra("request_id")
  247. if got, want := requestID, int64(86400); got != want {
  248. t.Errorf("got request_id value = %v, want %v", got, want)
  249. }
  250. untrimmed := tok.Extra("untrimmed")
  251. if got, want := untrimmed, " untrimmed "; got != want {
  252. t.Errorf("got untrimmed = %q; want %q", got, want)
  253. }
  254. }
  255. const day = 24 * time.Hour
  256. func TestExchangeRequest_JSONResponse_Expiry(t *testing.T) {
  257. seconds := int32(day.Seconds())
  258. for _, c := range []struct {
  259. name string
  260. expires string
  261. want bool
  262. }{
  263. {"normal", fmt.Sprintf(`"expires_in": %d`, seconds), true},
  264. {"paypal", fmt.Sprintf(`"expires_in": "%d"`, seconds), true},
  265. {"facebook", fmt.Sprintf(`"expires": %d`, seconds), true},
  266. {"issue_239", fmt.Sprintf(`"expires_in": null, "expires": %d`, seconds), true},
  267. {"wrong_type", `"expires": false`, false},
  268. {"wrong_type2", `"expires": {}`, false},
  269. {"wrong_value", `"expires": "zzz"`, false},
  270. } {
  271. t.Run(c.name, func(t *testing.T) {
  272. testExchangeRequest_JSONResponse_expiry(t, c.expires, c.want)
  273. })
  274. }
  275. }
  276. func testExchangeRequest_JSONResponse_expiry(t *testing.T, exp string, want bool) {
  277. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  278. w.Header().Set("Content-Type", "application/json")
  279. w.Write([]byte(fmt.Sprintf(`{"access_token": "90d", "scope": "user", "token_type": "bearer", %s}`, exp)))
  280. }))
  281. defer ts.Close()
  282. conf := newConf(ts.URL)
  283. t1 := time.Now().Add(day)
  284. tok, err := conf.Exchange(context.Background(), "exchange-code")
  285. t2 := time.Now().Add(day)
  286. if got := (err == nil); got != want {
  287. if want {
  288. t.Errorf("unexpected error: got %v", err)
  289. } else {
  290. t.Errorf("unexpected success")
  291. }
  292. }
  293. if !want {
  294. return
  295. }
  296. if !tok.Valid() {
  297. t.Fatalf("Token invalid. Got: %#v", tok)
  298. }
  299. expiry := tok.Expiry
  300. if expiry.Before(t1) || expiry.After(t2) {
  301. t.Errorf("Unexpected value for Expiry: %v (shold be between %v and %v)", expiry, t1, t2)
  302. }
  303. }
  304. func TestExchangeRequest_BadResponse(t *testing.T) {
  305. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  306. w.Header().Set("Content-Type", "application/json")
  307. w.Write([]byte(`{"scope": "user", "token_type": "bearer"}`))
  308. }))
  309. defer ts.Close()
  310. conf := newConf(ts.URL)
  311. _, err := conf.Exchange(context.Background(), "code")
  312. if err == nil {
  313. t.Error("expected error from missing access_token")
  314. }
  315. }
  316. func TestExchangeRequest_BadResponseType(t *testing.T) {
  317. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  318. w.Header().Set("Content-Type", "application/json")
  319. w.Write([]byte(`{"access_token":123, "scope": "user", "token_type": "bearer"}`))
  320. }))
  321. defer ts.Close()
  322. conf := newConf(ts.URL)
  323. _, err := conf.Exchange(context.Background(), "exchange-code")
  324. if err == nil {
  325. t.Error("expected error from non-string access_token")
  326. }
  327. }
  328. func TestExchangeRequest_NonBasicAuth(t *testing.T) {
  329. internal.ResetAuthCache()
  330. tr := &mockTransport{
  331. rt: func(r *http.Request) (w *http.Response, err error) {
  332. headerAuth := r.Header.Get("Authorization")
  333. if headerAuth != "" {
  334. t.Errorf("Unexpected authorization header %q", headerAuth)
  335. }
  336. return nil, errors.New("no response")
  337. },
  338. }
  339. c := &http.Client{Transport: tr}
  340. conf := &Config{
  341. ClientID: "CLIENT_ID",
  342. Endpoint: Endpoint{
  343. AuthURL: "https://accounts.google.com/auth",
  344. TokenURL: "https://accounts.google.com/token",
  345. AuthStyle: AuthStyleInParams,
  346. },
  347. }
  348. ctx := context.WithValue(context.Background(), HTTPClient, c)
  349. conf.Exchange(ctx, "code")
  350. }
  351. func TestPasswordCredentialsTokenRequest(t *testing.T) {
  352. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  353. defer r.Body.Close()
  354. expected := "/token"
  355. if r.URL.String() != expected {
  356. t.Errorf("URL = %q; want %q", r.URL, expected)
  357. }
  358. headerAuth := r.Header.Get("Authorization")
  359. expected = "Basic Q0xJRU5UX0lEOkNMSUVOVF9TRUNSRVQ="
  360. if headerAuth != expected {
  361. t.Errorf("Authorization header = %q; want %q", headerAuth, expected)
  362. }
  363. headerContentType := r.Header.Get("Content-Type")
  364. expected = "application/x-www-form-urlencoded"
  365. if headerContentType != expected {
  366. t.Errorf("Content-Type header = %q; want %q", headerContentType, expected)
  367. }
  368. body, err := ioutil.ReadAll(r.Body)
  369. if err != nil {
  370. t.Errorf("Failed reading request body: %s.", err)
  371. }
  372. expected = "grant_type=password&password=password1&scope=scope1+scope2&username=user1"
  373. if string(body) != expected {
  374. t.Errorf("res.Body = %q; want %q", string(body), expected)
  375. }
  376. w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
  377. w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
  378. }))
  379. defer ts.Close()
  380. conf := newConf(ts.URL)
  381. tok, err := conf.PasswordCredentialsToken(context.Background(), "user1", "password1")
  382. if err != nil {
  383. t.Error(err)
  384. }
  385. if !tok.Valid() {
  386. t.Fatalf("Token invalid. Got: %#v", tok)
  387. }
  388. expected := "90d64460d14870c08c81352a05dedd3465940a7c"
  389. if tok.AccessToken != expected {
  390. t.Errorf("AccessToken = %q; want %q", tok.AccessToken, expected)
  391. }
  392. expected = "bearer"
  393. if tok.TokenType != expected {
  394. t.Errorf("TokenType = %q; want %q", tok.TokenType, expected)
  395. }
  396. }
  397. func TestTokenRefreshRequest(t *testing.T) {
  398. internal.ResetAuthCache()
  399. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  400. if r.URL.String() == "/somethingelse" {
  401. return
  402. }
  403. if r.URL.String() != "/token" {
  404. t.Errorf("Unexpected token refresh request URL %q", r.URL)
  405. }
  406. headerContentType := r.Header.Get("Content-Type")
  407. if headerContentType != "application/x-www-form-urlencoded" {
  408. t.Errorf("Unexpected Content-Type header %q", headerContentType)
  409. }
  410. body, _ := ioutil.ReadAll(r.Body)
  411. if string(body) != "grant_type=refresh_token&refresh_token=REFRESH_TOKEN" {
  412. t.Errorf("Unexpected refresh token payload %q", body)
  413. }
  414. w.Header().Set("Content-Type", "application/json")
  415. io.WriteString(w, `{"access_token": "foo", "refresh_token": "bar"}`)
  416. }))
  417. defer ts.Close()
  418. conf := newConf(ts.URL)
  419. c := conf.Client(context.Background(), &Token{RefreshToken: "REFRESH_TOKEN"})
  420. c.Get(ts.URL + "/somethingelse")
  421. }
  422. func TestFetchWithNoRefreshToken(t *testing.T) {
  423. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  424. if r.URL.String() == "/somethingelse" {
  425. return
  426. }
  427. if r.URL.String() != "/token" {
  428. t.Errorf("Unexpected token refresh request URL, %v is found.", r.URL)
  429. }
  430. headerContentType := r.Header.Get("Content-Type")
  431. if headerContentType != "application/x-www-form-urlencoded" {
  432. t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
  433. }
  434. body, _ := ioutil.ReadAll(r.Body)
  435. if string(body) != "client_id=CLIENT_ID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN" {
  436. t.Errorf("Unexpected refresh token payload, %v is found.", string(body))
  437. }
  438. }))
  439. defer ts.Close()
  440. conf := newConf(ts.URL)
  441. c := conf.Client(context.Background(), nil)
  442. _, err := c.Get(ts.URL + "/somethingelse")
  443. if err == nil {
  444. t.Errorf("Fetch should return an error if no refresh token is set")
  445. }
  446. }
  447. func TestTokenRetrieveError(t *testing.T) {
  448. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  449. if r.URL.String() != "/token" {
  450. t.Errorf("Unexpected token refresh request URL, %v is found.", r.URL)
  451. }
  452. w.Header().Set("Content-type", "application/json")
  453. w.WriteHeader(http.StatusBadRequest)
  454. w.Write([]byte(`{"error": "invalid_grant"}`))
  455. }))
  456. defer ts.Close()
  457. conf := newConf(ts.URL)
  458. _, err := conf.Exchange(context.Background(), "exchange-code")
  459. if err == nil {
  460. t.Fatalf("got no error, expected one")
  461. }
  462. _, ok := err.(*RetrieveError)
  463. if !ok {
  464. t.Fatalf("got %T error, expected *RetrieveError; error was: %v", err, err)
  465. }
  466. // Test error string for backwards compatibility
  467. expected := fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", "400 Bad Request", `{"error": "invalid_grant"}`)
  468. if errStr := err.Error(); errStr != expected {
  469. t.Fatalf("got %#v, expected %#v", errStr, expected)
  470. }
  471. }
  472. func TestRefreshToken_RefreshTokenReplacement(t *testing.T) {
  473. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  474. w.Header().Set("Content-Type", "application/json")
  475. w.Write([]byte(`{"access_token":"ACCESS_TOKEN", "scope": "user", "token_type": "bearer", "refresh_token": "NEW_REFRESH_TOKEN"}`))
  476. return
  477. }))
  478. defer ts.Close()
  479. conf := newConf(ts.URL)
  480. tkr := conf.TokenSource(context.Background(), &Token{RefreshToken: "OLD_REFRESH_TOKEN"})
  481. tk, err := tkr.Token()
  482. if err != nil {
  483. t.Errorf("got err = %v; want none", err)
  484. return
  485. }
  486. if want := "NEW_REFRESH_TOKEN"; tk.RefreshToken != want {
  487. t.Errorf("RefreshToken = %q; want %q", tk.RefreshToken, want)
  488. }
  489. }
  490. func TestRefreshToken_RefreshTokenPreservation(t *testing.T) {
  491. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  492. w.Header().Set("Content-Type", "application/json")
  493. w.Write([]byte(`{"access_token":"ACCESS_TOKEN", "scope": "user", "token_type": "bearer"}`))
  494. return
  495. }))
  496. defer ts.Close()
  497. conf := newConf(ts.URL)
  498. const oldRefreshToken = "OLD_REFRESH_TOKEN"
  499. tkr := conf.TokenSource(context.Background(), &Token{RefreshToken: oldRefreshToken})
  500. tk, err := tkr.Token()
  501. if err != nil {
  502. t.Fatalf("got err = %v; want none", err)
  503. }
  504. if tk.RefreshToken != oldRefreshToken {
  505. t.Errorf("RefreshToken = %q; want %q", tk.RefreshToken, oldRefreshToken)
  506. }
  507. }
  508. func TestConfigClientWithToken(t *testing.T) {
  509. tok := &Token{
  510. AccessToken: "abc123",
  511. }
  512. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  513. if got, want := r.Header.Get("Authorization"), fmt.Sprintf("Bearer %s", tok.AccessToken); got != want {
  514. t.Errorf("Authorization header = %q; want %q", got, want)
  515. }
  516. return
  517. }))
  518. defer ts.Close()
  519. conf := newConf(ts.URL)
  520. c := conf.Client(context.Background(), tok)
  521. req, err := http.NewRequest("GET", ts.URL, nil)
  522. if err != nil {
  523. t.Error(err)
  524. }
  525. _, err = c.Do(req)
  526. if err != nil {
  527. t.Error(err)
  528. }
  529. }