This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
transport_test.go
200 lines (159 loc) · 4.87 KB
/
transport_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
198
199
200
package httpset
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/strava/go.serversets/fixedset"
)
type StubRoundTripper struct {
PrevRequest *http.Request
}
func (rt *StubRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
rt.PrevRequest = req
return nil, nil
}
func TestNewTransport(t *testing.T) {
fs := fixedset.New([]string{"localhost:2181"})
transport := NewTransport(fs)
if l := len(transport.Endpoints()); l != 1 {
t.Errorf("should have one endpoint but got %v", l)
}
fs.SetEndpoints([]string{"localhost:2181", "localhost:2182"})
<-transport.Event()
if len(transport.Endpoints()) != 2 {
t.Errorf("should have two endpoint but got %v", transport.Endpoints())
}
}
func TestTransportRoundTrip(t *testing.T) {
transport := NewTransport(nil)
stub := &StubRoundTripper{}
transport.BaseTransport = stub
r, _ := http.NewRequest("GET", "http://localhost", nil)
_, err := transport.RoundTrip(r)
if err != ErrNoServers {
t.Errorf("should get error if no servers")
}
transport.SetEndpoints([]string{"a:80", "b:80"})
<-transport.Event()
r, _ = http.NewRequest("GET", "http://localhost", nil)
transport.RoundTrip(r)
if v := stub.PrevRequest.URL.String(); v != "http://b:80" {
t.Errorf("incorrect url, got %v", v)
}
transport.RoundTrip(r)
if v := stub.PrevRequest.URL.String(); v != "http://a:80" {
t.Errorf("incorrect url, got %v", v)
}
transport.RoundTrip(r)
if v := stub.PrevRequest.URL.String(); v != "http://b:80" {
t.Errorf("incorrect url, got %v", v)
}
}
func TestTransportCloseWatch(t *testing.T) {
count := 0
event := make(chan struct{}, 1)
watcherClosed = func() {
count++
event <- struct{}{}
}
fs := fixedset.New(nil)
NewTransport(fs)
fs.Close()
// little event channel to allow the other goroutine to run and quit as it should
<-event
if count != 1 {
t.Error("should close watching goroutine on watcher channel close")
}
}
func TestNewTransportWithoutWatcher(t *testing.T) {
NewTransport(nil)
// should not panic or anything
}
func TestTransportRotateServer(t *testing.T) {
transport := NewTransport(nil)
transport.SetEndpoints([]string{"localhost:2181", "localhost:2182"})
if ep, _ := transport.RotateEndpoint(); ep != "localhost:2182" {
t.Errorf("incorrect server, got %v", ep)
}
if ep, _ := transport.RotateEndpoint(); ep != "localhost:2181" {
t.Errorf("incorrect server, got %v", ep)
}
if ep, _ := transport.RotateEndpoint(); ep != "localhost:2182" {
t.Errorf("incorrect server, got %v", ep)
}
}
func TestTransportTriggerEvent(t *testing.T) {
transport := NewTransport(nil)
transport.triggerEvent()
transport.triggerEvent()
transport.triggerEvent()
if transport.EventCount != 3 {
t.Errorf("event count not right, got %v", transport.EventCount)
}
}
func TestTransportReplaceHost(t *testing.T) {
transport := NewTransport(nil)
r, _ := http.NewRequest("GET", "http://hostname/path/path2", nil)
err := transport.replaceHost(r)
if err != ErrNoServers {
t.Errorf("should get error, but got %v", err)
}
transport.SetEndpoints([]string{"host:123"})
tests := [][2]string{
[2]string{"http://hostname/path/path2", "http://host:123/path/path2"},
[2]string{"http://hostname:321/path/path2", "http://host:123/path/path2"},
[2]string{"https://hostname:321/path/path2", "https://host:123/path/path2"},
[2]string{"https://hostname:321/path/path2?key=value", "https://host:123/path/path2?key=value"},
[2]string{"/path/path2?key=value", "http://host:123/path/path2?key=value"},
}
for _, test := range tests {
r, _ := http.NewRequest("GET", test[0], nil)
transport.replaceHost(r)
if v := r.URL.String(); v != test[1] {
t.Errorf("host not replaced, expected %s, got %s", test[1], v)
}
}
// UseHTTPS
transport.UseHTTPS = true
r, _ = http.NewRequest("GET", "/path/path2?key=value", nil)
transport.replaceHost(r)
if v := r.URL.String(); v != "https://host:123/path/path2?key=value" {
t.Errorf("host not replaced, got %s", v)
}
}
func TestTransport(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
count1 := 0
server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count1++
}))
defer server1.Close()
count2 := 0
server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count2++
}))
defer server2.Close()
transport := NewTransport(nil)
u1, _ := url.Parse(server1.URL)
u2, _ := url.Parse(server2.URL)
transport.SetEndpoints([]string{u1.Host, u2.Host})
<-transport.Event()
r, _ := http.NewRequest("GET", "http://somehost/", nil)
_, err := transport.RoundTrip(r)
if err != nil {
t.Errorf("request error: %v", err)
}
if count2 != 1 {
t.Errorf("should hit the second server, got %v", count2)
}
_, err = transport.RoundTrip(r)
if err != nil {
t.Errorf("request error: %v", err)
}
if count1 != 1 {
t.Errorf("should hit the first server, got %v", count1)
}
}