forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
333 lines (274 loc) · 9.26 KB
/
http_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"github.com/flynn/flynn/Godeps/_workspace/src/code.google.com/p/go.net/websocket"
. "github.com/flynn/flynn/Godeps/_workspace/src/gopkg.in/check.v1"
"github.com/flynn/flynn/discoverd/testutil/etcdrunner"
"github.com/flynn/flynn/router/types"
)
var httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{ServerName: "example.com"},
},
}
// borrowed from net/http/httptest/server.go
// localhostCert is a PEM-encoded TLS cert with SAN IPs
// "127.0.0.1" and "[::1]", expiring at the last second of 2049 (the end
// of ASN.1 time).
// generated from src/pkg/crypto/tls:
// go run generate_cert.go --rsa-bits 512 --host 127.0.0.1,::1,example.com --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h
var localhostCert = []byte(`-----BEGIN CERTIFICATE-----
MIIBdzCCASOgAwIBAgIBADALBgkqhkiG9w0BAQUwEjEQMA4GA1UEChMHQWNtZSBD
bzAeFw03MDAxMDEwMDAwMDBaFw00OTEyMzEyMzU5NTlaMBIxEDAOBgNVBAoTB0Fj
bWUgQ28wWjALBgkqhkiG9w0BAQEDSwAwSAJBAN55NcYKZeInyTuhcCwFMhDHCmwa
IUSdtXdcbItRB/yfXGBhiex00IaLXQnSU+QZPRZWYqeTEbFSgihqi1PUDy8CAwEA
AaNoMGYwDgYDVR0PAQH/BAQDAgCkMBMGA1UdJQQMMAoGCCsGAQUFBwMBMA8GA1Ud
EwEB/wQFMAMBAf8wLgYDVR0RBCcwJYILZXhhbXBsZS5jb22HBH8AAAGHEAAAAAAA
AAAAAAAAAAAAAAEwCwYJKoZIhvcNAQEFA0EAAoQn/ytgqpiLcZu9XKbCJsJcvkgk
Se6AbGXgSlq+ZCEVo0qIwSgeBqmsJxUu7NCSOwVJLYNEBO2DtIxoYVk+MA==
-----END CERTIFICATE-----`)
// localhostKey is the private key for localhostCert.
var localhostKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIBPAIBAAJBAN55NcYKZeInyTuhcCwFMhDHCmwaIUSdtXdcbItRB/yfXGBhiex0
0IaLXQnSU+QZPRZWYqeTEbFSgihqi1PUDy8CAwEAAQJBAQdUx66rfh8sYsgfdcvV
NoafYpnEcB5s4m/vSVe6SU7dCK6eYec9f9wpT353ljhDUHq3EbmE4foNzJngh35d
AekCIQDhRQG5Li0Wj8TM4obOnnXUXf1jRv0UkzE9AHWLG5q3AwIhAPzSjpYUDjVW
MCUXgckTpKCuGwbJk7424Nb8bLzf3kllAiA5mUBgjfr/WtFSJdWcPQ4Zt9KTMNKD
EUO0ukpTwEIl6wIhAMbGqZK3zAAFdq8DD2jPx+UJXnh0rnOkZBzDtJ6/iN69AiEA
1Aq8MJgTaYsDQWyU/hDq5YkDJc9e9DSCvUIzqxQWMQE=
-----END RSA PRIVATE KEY-----`)
func init() {
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(localhostCert)
httpClient.Transport.(*http.Transport).TLSClientConfig.RootCAs = pool
}
func httpTestHandler(id string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(id))
})
}
type httpListener struct {
*HTTPListener
cleanup func()
}
func (l *httpListener) Close() error {
l.HTTPListener.Close()
if l.cleanup != nil {
l.cleanup()
}
return nil
}
func newHTTPListenerClients(t etcdrunner.TestingT, etcd EtcdClient, discoverd discoverdClient) (*httpListener, discoverdClient) {
discoverd, etcd, cleanup := setup(t, etcd, discoverd)
l := &httpListener{
NewHTTPListener("127.0.0.1:0", "127.0.0.1:0", nil, NewEtcdDataStore(etcd, "/router/http/"), discoverd),
cleanup,
}
if err := l.Start(); err != nil {
t.Fatal(err)
}
return l, discoverd
}
func newHTTPListener(t etcdrunner.TestingT) (*httpListener, discoverdClient) {
return newHTTPListenerClients(t, nil, nil)
}
// https://code.google.com/p/go/issues/detail?id=5381
func (s *S) TestIssue5381(c *C) {
srv := httptest.NewServer(httpTestHandler(""))
defer srv.Close()
l, discoverd := newHTTPListener(c)
defer l.Close()
addHTTPRoute(c, l)
discoverdRegisterHTTP(c, l, srv.Listener.Addr().String())
defer discoverd.UnregisterAll()
assertGet(c, "http://"+l.Addr, "example.com", "")
}
func (s *S) TestAddHTTPRoute(c *C) {
srv1 := httptest.NewServer(httpTestHandler("1"))
srv2 := httptest.NewServer(httpTestHandler("2"))
defer srv1.Close()
defer srv2.Close()
l, discoverd := newHTTPListener(c)
defer l.Close()
r := addHTTPRoute(c, l)
discoverdRegisterHTTP(c, l, srv1.Listener.Addr().String())
defer discoverd.UnregisterAll()
assertGet(c, "http://"+l.Addr, "example.com", "1")
assertGet(c, "https://"+l.TLSAddr, "example.com", "1")
discoverd.Unregister("test", srv1.Listener.Addr().String())
discoverdRegisterHTTP(c, l, srv2.Listener.Addr().String())
// Close the connection we just used to trigger a new backend choice
httpClient.Transport.(*http.Transport).CloseIdleConnections()
assertGet(c, "http://"+l.Addr, "example.com", "2")
assertGet(c, "https://"+l.TLSAddr, "example.com", "2")
wait := waitForEvent(c, l, "remove", r.ID)
err := l.RemoveRoute(r.ID)
c.Assert(err, IsNil)
wait()
httpClient.Transport.(*http.Transport).CloseIdleConnections()
res, err := httpClient.Do(newReq("http://"+l.Addr, "example.com"))
c.Assert(err, IsNil)
c.Assert(res.StatusCode, Equals, 404)
res.Body.Close()
}
func newReq(url, host string) *http.Request {
req, _ := http.NewRequest("GET", url, nil)
req.Host = host
return req
}
func assertGet(c *C, url, host, expected string) *http.Cookie {
return assertGetCookie(c, url, host, expected, nil)
}
func assertGetCookie(c *C, url, host, expected string, cookie *http.Cookie) *http.Cookie {
req := newReq(url, host)
if cookie != nil {
req.AddCookie(cookie)
}
res, err := httpClient.Do(req)
c.Assert(err, IsNil)
c.Assert(res.StatusCode, Equals, 200)
data, err := ioutil.ReadAll(res.Body)
res.Body.Close()
c.Assert(err, IsNil)
c.Assert(string(data), Equals, expected)
for _, c := range res.Cookies() {
if c.Name == stickyCookie {
return c
}
}
return nil
}
func addHTTPRoute(c *C, l *httpListener) *router.Route {
wait := waitForEvent(c, l, "set", "")
r := (&router.HTTPRoute{
Domain: "example.com",
Service: "test",
TLSCert: string(localhostCert),
TLSKey: string(localhostKey),
}).ToRoute()
err := l.AddRoute(r)
c.Assert(err, IsNil)
wait()
return r
}
func addStickyHTTPRoute(c *C, l *httpListener) *router.Route {
wait := waitForEvent(c, l, "set", "")
r := (&router.HTTPRoute{
Domain: "example.com",
Service: "test",
Sticky: true,
}).ToRoute()
err := l.AddRoute(r)
c.Assert(err, IsNil)
wait()
return r
}
func (s *S) TestHTTPInitialSync(c *C) {
etcd, cleanup := newEtcd(c)
defer cleanup()
l, _ := newHTTPListenerClients(c, etcd, nil)
addHTTPRoute(c, l)
l.Close()
srv := httptest.NewServer(httpTestHandler("1"))
defer srv.Close()
l, discoverd := newHTTPListenerClients(c, etcd, nil)
defer l.Close()
discoverdRegisterHTTP(c, l, srv.Listener.Addr().String())
defer discoverd.UnregisterAll()
assertGet(c, "http://"+l.Addr, "example.com", "1")
assertGet(c, "https://"+l.TLSAddr, "example.com", "1")
}
// issue #26
func (s *S) TestHTTPServiceHandlerBackendConnectionClosed(c *C) {
srv := httptest.NewServer(httpTestHandler("1"))
l, discoverd := newHTTPListener(c)
defer l.Close()
addHTTPRoute(c, l)
discoverdRegisterHTTP(c, l, srv.Listener.Addr().String())
defer discoverd.UnregisterAll()
// a single request is allowed to successfully get issued
assertGet(c, "http://"+l.Addr, "example.com", "1")
// the backend server's connection gets closed, but router is
// able to recover
srv.CloseClientConnections()
assertGet(c, "http://"+l.Addr, "example.com", "1")
}
// Act as an app to test HTTP headers
func httpHeaderTestHandler(c *C, id string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
c.Assert(req.Header["X-Forwarded-Proto"][0], Equals, "http")
c.Assert(len(req.Header["X-Request-Start"][0]), Equals, 13)
c.Assert(req.Header["X-Forwarded-For"][0], Equals, "127.0.0.1")
w.Write([]byte(id))
})
}
// issue #105
func (s *S) TestHTTPHeaders(c *C) {
srv := httptest.NewServer(httpHeaderTestHandler(c, "1"))
l, discoverd := newHTTPListener(c)
defer l.Close()
addHTTPRoute(c, l)
discoverdRegisterHTTP(c, l, srv.Listener.Addr().String())
defer discoverd.UnregisterAll()
assertGet(c, "http://"+l.Addr, "example.com", "1")
}
func (s *S) TestHTTPWebsocket(c *C) {
done := make(chan struct{})
srv := httptest.NewServer(
websocket.Handler(func(conn *websocket.Conn) {
_, err := conn.Write([]byte("1"))
c.Assert(err, IsNil)
res := make([]byte, 1)
_, err = conn.Read(res)
c.Assert(err, IsNil)
c.Assert(res[0], Equals, byte('2'))
close(done)
}),
)
l, discoverd := newHTTPListener(c)
defer l.Close()
addHTTPRoute(c, l)
discoverdRegisterHTTP(c, l, srv.Listener.Addr().String())
defer discoverd.UnregisterAll()
conn, err := net.Dial("tcp", l.Addr)
c.Assert(err, IsNil)
defer conn.Close()
conf, err := websocket.NewConfig("ws://example.com", "http://example.net")
c.Assert(err, IsNil)
wc, err := websocket.NewClient(conf, conn)
c.Assert(err, IsNil)
res := make([]byte, 1)
_, err = wc.Read(res)
c.Assert(err, IsNil)
c.Assert(res[0], Equals, byte('1'))
_, err = wc.Write([]byte("2"))
c.Assert(err, IsNil)
}
func (s *S) TestStickyHTTPRoute(c *C) {
srv1 := httptest.NewServer(httpTestHandler("1"))
srv2 := httptest.NewServer(httpTestHandler("2"))
defer srv1.Close()
defer srv2.Close()
l, discoverd := newHTTPListener(c)
defer l.Close()
addStickyHTTPRoute(c, l)
discoverdRegisterHTTP(c, l, srv1.Listener.Addr().String())
defer discoverd.UnregisterAll()
cookie := assertGet(c, "http://"+l.Addr, "example.com", "1")
discoverdRegisterHTTP(c, l, srv2.Listener.Addr().String())
for i := 0; i < 10; i++ {
resCookie := assertGetCookie(c, "http://"+l.Addr, "example.com", "1", cookie)
c.Assert(resCookie, IsNil)
httpClient.Transport.(*http.Transport).CloseIdleConnections()
}
discoverd.Unregister("test", srv1.Listener.Addr().String())
for i := 0; i < 10; i++ {
resCookie := assertGetCookie(c, "http://"+l.Addr, "example.com", "2", cookie)
c.Assert(resCookie, Not(IsNil))
}
}