forked from golang/oauth2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
credential_test.go
39 lines (36 loc) · 1.16 KB
/
credential_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2018 James F Cote All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package oauth2_test
import (
"context"
"testing"
"github.com/jfcote87/oauth2"
)
func Test_PerRPCCredentials(t *testing.T) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(nil)
creds := oauth2.PerRPCCredentials{}
if _, err := creds.GetRequestMetadata(ctx); err == nil || err.Error() != "nil credential" {
t.Errorf("expected \"nil credential\"; got %v", err)
return
}
creds = oauth2.PerRPCCredentials{ts}
if _, err := creds.GetRequestMetadata(ctx); err == nil || err.Error() != "nil token" {
t.Errorf("expected \"nil token\"; got %v", err)
return
}
creds = oauth2.PerRPCCredentials{oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "ABCDEFG"})}
mx, err := creds.GetRequestMetadata(ctx)
if err != nil {
t.Errorf("expected token; got %v", err)
return
}
if mx["authorization"] != "Bearer ABCDEFG" {
t.Errorf("expected Bearer ABCDEFG; got %s", mx["authorizaztion"])
return
}
if !creds.RequireTransportSecurity() {
t.Errorf("expected RequireTransportSecurity to return true")
}
}