-
Notifications
You must be signed in to change notification settings - Fork 1
/
address_test.go
197 lines (189 loc) · 3.77 KB
/
address_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package fb_test
import (
"fmt"
"testing"
"github.com/featurebasedb/fb"
"github.com/stretchr/testify/assert"
)
func TestAddress(t *testing.T) {
t.Run("Address", func(t *testing.T) {
tests := []struct {
addr fb.Address
expScheme string
expHostPort string
expHost string
expPort uint16
expPath string
}{
{
// blank address
addr: "",
expScheme: "",
expHostPort: "",
expHost: "",
expPort: 0,
},
{
// schema://
addr: "http://",
expScheme: "http",
expHostPort: "",
expHost: "",
expPort: 0,
},
{
// host
addr: "foo",
expScheme: "",
expHostPort: "foo",
expHost: "foo",
expPort: 0,
},
{
// :port
addr: ":8080",
expScheme: "",
expHostPort: ":8080",
expHost: "",
expPort: 8080,
},
{
// host:port
addr: "foo:8080",
expScheme: "",
expHostPort: "foo:8080",
expHost: "foo",
expPort: 8080,
},
{
// schema://host:port
addr: "http://foo:8080",
expScheme: "http",
expHostPort: "foo:8080",
expHost: "foo",
expPort: 8080,
},
{
// schema://host
addr: "http://foo",
expScheme: "http",
expHostPort: "foo",
expHost: "foo",
expPort: 0,
},
{
// schema://:port
addr: "http://:8080",
expScheme: "http",
expHostPort: ":8080",
expHost: "",
expPort: 8080,
},
{
// invalid port
addr: "http://foo:bar",
expScheme: "http",
expHostPort: "foo",
expHost: "foo",
expPort: 0,
},
{
// :port outside of int16 range
addr: ":53308",
expScheme: "",
expHostPort: ":53308",
expHost: "",
expPort: 53308,
},
{
// with path:
addr: "localhost:8080/foo/bar",
expScheme: "",
expHostPort: "localhost:8080",
expHost: "localhost",
expPort: 8080,
expPath: "foo/bar",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
assert.Equal(t, test.expScheme, test.addr.Scheme())
assert.Equal(t, test.expHostPort, test.addr.HostPort())
assert.Equal(t, test.expHost, test.addr.Host())
assert.Equal(t, test.expPort, test.addr.Port())
assert.Equal(t, test.expPath, test.addr.Path())
})
}
})
t.Run("OverrideScheme", func(t *testing.T) {
tests := []struct {
addr fb.Address
scheme string
expAddr string
}{
{
addr: "foo",
scheme: "http",
expAddr: "http://foo",
},
{
addr: "http://foo",
scheme: "grpc",
expAddr: "grpc://foo",
},
{
addr: "http://foo:8080",
scheme: "",
expAddr: "foo:8080",
},
{
addr: "http://foo:8080/bar",
scheme: "",
expAddr: "foo:8080/bar",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
assert.Equal(t, test.expAddr, test.addr.OverrideScheme(test.scheme))
})
}
})
t.Run("WithScheme", func(t *testing.T) {
tests := []struct {
addr fb.Address
scheme string
expAddr string
}{
{
addr: "foo",
scheme: "",
expAddr: "://foo",
},
{
addr: "http://foo",
scheme: "grpc",
expAddr: "http://foo",
},
{
addr: "http://foo:8080",
scheme: "",
expAddr: "http://foo:8080",
},
{
addr: "foo:8080",
scheme: "grpc",
expAddr: "grpc://foo:8080",
},
{
addr: "foo:8080/bar",
scheme: "grpc",
expAddr: "grpc://foo:8080/bar",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
assert.Equal(t, test.expAddr, test.addr.WithScheme(test.scheme))
})
}
})
}