This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathproxy_test.go
167 lines (133 loc) · 5.46 KB
/
proxy_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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var proxyTestVersion = &version{
name: "foo",
sequins: &sequins{
config: sequinsConfig{
Sharding: shardingConfig{
ProxyTimeout: duration{300 * time.Millisecond},
ProxyStageTimeout: duration{100 * time.Millisecond},
},
},
},
}
func readAll(t *testing.T, r io.Reader) string {
b, err := ioutil.ReadAll(r)
require.NoError(t, err)
return string(b)
}
func httptestHost(s *httptest.Server) string {
parsed, _ := url.Parse(s.URL)
return parsed.Host
}
func TestProxySinglePeer(t *testing.T) {
goodPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "all good")
}))
peers := []string{httptestHost(goodPeer)}
r, _ := http.NewRequest("GET", "http://localhost", nil)
res, peer, err := proxyTestVersion.proxy(r, peers)
require.NoError(t, err, "simple proxying should work")
assert.NotNil(t, res, "simple proxying should work")
assert.Equal(t, httptestHost(goodPeer), peer, "the returned peer should be correct")
assert.Equal(t, "all good\n", readAll(t, res.Body))
}
func TestProxySlowPeer(t *testing.T) {
slowPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
fmt.Fprintln(w, "sorry, did you need something?")
}))
goodPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "all good")
}))
notReachedPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.FailNow(t, "proxying should succeed before getting to a third peer")
}))
peers := []string{httptestHost(slowPeer), httptestHost(goodPeer), httptestHost(notReachedPeer)}
r, _ := http.NewRequest("GET", "http://localhost", nil)
res, peer, err := proxyTestVersion.proxy(r, peers)
require.NoError(t, err, "proxying should work on the second peer")
require.NotNil(t, res, "proxying should work on the second peer")
assert.Equal(t, httptestHost(goodPeer), peer, "the returned peer should be correct")
assert.Equal(t, "all good\n", readAll(t, res.Body))
}
func TestProxyErrorPeer(t *testing.T) {
errorPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}))
goodPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "all good")
}))
notReachedPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Fail(t, "proxying should succeed before getting to a third peer")
}))
peers := []string{httptestHost(errorPeer), httptestHost(goodPeer), httptestHost(notReachedPeer)}
r, _ := http.NewRequest("GET", "http://localhost", nil)
res, peer, err := proxyTestVersion.proxy(r, peers)
require.NoError(t, err, "proxying should work on the second peer")
require.NotNil(t, res, "proxying should work on the second peer")
assert.Equal(t, httptestHost(goodPeer), peer, "the returned peer should be correct")
assert.Equal(t, "all good\n", readAll(t, res.Body))
}
func TestProxySlowPeerErrorPeer(t *testing.T) {
slowPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(150 * time.Millisecond)
fmt.Fprintln(w, "all good, sorry to keep you waiting")
}))
errorPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}))
peers := []string{httptestHost(slowPeer), httptestHost(errorPeer)}
r, _ := http.NewRequest("GET", "http://localhost", nil)
res, peer, err := proxyTestVersion.proxy(r, peers)
require.NoError(t, err, "proxying should work on the first peer")
require.NotNil(t, res, "proxying should work on the first peer")
assert.Equal(t, httptestHost(slowPeer), peer, "the returned peer should be correct")
assert.Equal(t, "all good, sorry to keep you waiting\n", readAll(t, res.Body))
}
func TestProxyTimeout(t *testing.T) {
slowPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
fmt.Fprintln(w, "sorry, did you need something?")
}))
notReachedPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Fail(t, "proxying should never try a fourth peer")
}))
peers := []string{
httptestHost(slowPeer),
httptestHost(slowPeer),
httptestHost(slowPeer),
httptestHost(notReachedPeer),
}
r, _ := http.NewRequest("GET", "http://localhost", nil)
res, peer, err := proxyTestVersion.proxy(r, peers)
assert.Equal(t, errProxyTimeout, err, "proxying should time out with all slow peers")
assert.Nil(t, res, "proxying should time out with all slow peers")
assert.Equal(t, "", peer, "peer should be empty if proxying timed out")
}
func TestProxyErrors(t *testing.T) {
errorPeer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}))
peers := []string{
httptestHost(errorPeer),
httptestHost(errorPeer),
httptestHost(errorPeer),
}
r, _ := http.NewRequest("GET", "http://localhost", nil)
res, peer, err := proxyTestVersion.proxy(r, peers)
assert.Equal(t, errNoAvailablePeers, err, "proxying should return errNoAvailablePeers if all error")
assert.Nil(t, res, "proxying should return errNoAvailablePeers if all error")
assert.Equal(t, "", peer, "peer should be empty if proxying timed out")
}