File: option_test.go

package info (click to toggle)
golang-google-api 0.0~git20161128.3cc2e59-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 64,468 kB
  • ctags: 71,262
  • sloc: makefile: 15
file content (52 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (3)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
package option

import (
	"reflect"
	"testing"

	"google.golang.org/api/internal"
	"google.golang.org/grpc"
)

// Check that the slice passed into WithScopes is copied.
func TestCopyScopes(t *testing.T) {
	o := &internal.DialSettings{}

	scopes := []string{"a", "b"}
	WithScopes(scopes...).Apply(o)

	// Modify after using.
	scopes[1] = "c"

	if o.Scopes[0] != "a" || o.Scopes[1] != "b" {
		t.Errorf("want ['a', 'b'], got %+v", o.Scopes)
	}
}

func TestApply(t *testing.T) {
	conn := &grpc.ClientConn{}
	opts := []ClientOption{
		WithEndpoint("https://example.com:443"),
		WithScopes("a"), // the next WithScopes should overwrite this one
		WithScopes("https://example.com/auth/helloworld", "https://example.com/auth/otherthing"),
		WithGRPCConn(conn),
		WithUserAgent("ua"),
		WithServiceAccountFile("service-account.json"),
		WithAPIKey("api-key"),
	}
	var got internal.DialSettings
	for _, opt := range opts {
		opt.Apply(&got)
	}
	want := internal.DialSettings{
		Scopes:                     []string{"https://example.com/auth/helloworld", "https://example.com/auth/otherthing"},
		UserAgent:                  "ua",
		Endpoint:                   "https://example.com:443",
		GRPCConn:                   conn,
		ServiceAccountJSONFilename: "service-account.json",
		APIKey: "api-key",
	}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("\ngot  %#v\nwant %#v", got, want)
	}
}