/
dial_test.go
95 lines (87 loc) · 2.21 KB
/
dial_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2016 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package grpc
import (
"context"
"errors"
"net"
"testing"
"time"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"google.golang.org/grpc"
)
// Check that user optioned grpc.WithDialer option overwrites App Engine dialer
func TestGRPCHook(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
expected := false
appengineDialerHook = (func(ctx context.Context) grpc.DialOption {
return grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
t.Error("did not expect a call to appengine dialer, got one")
cancel()
return nil, errors.New("not expected")
})
})
defer func() {
appengineDialerHook = nil
}()
expectedDialer := grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
expected = true
cancel()
return nil, errors.New("expected")
})
conn, err := Dial(ctx,
option.WithTokenSource(oauth2.StaticTokenSource(nil)), // No creds.
option.WithGRPCDialOption(expectedDialer),
option.WithGRPCDialOption(grpc.WithBlock()))
if err != context.Canceled {
t.Errorf("got %v, want %v", err, context.Canceled)
}
if conn != nil {
conn.Close()
t.Error("got valid conn, want nil")
}
if !expected {
t.Error("expected a call to expected dialer, didn't get one")
}
}
func TestCheckDirectPathEndPoint(t *testing.T) {
for _, testcase := range []struct {
name string
endpoint string
want bool
}{
{
name: "empty endpoint are disallowed",
endpoint: "",
want: false,
},
{
name: "dns schemes are allowed",
endpoint: "dns:///foo",
want: true,
},
{
name: "host without no prefix are allowed",
endpoint: "foo",
want: true,
},
{
name: "host with port are allowed",
endpoint: "foo:1234",
want: true,
},
{
name: "non-dns schemes are disallowed",
endpoint: "https://foo",
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
if got := checkDirectPathEndPoint(testcase.endpoint); got != testcase.want {
t.Fatalf("got %v, want %v", got, testcase.want)
}
})
}
}