-
Notifications
You must be signed in to change notification settings - Fork 343
/
proxytest.go
146 lines (121 loc) · 3.3 KB
/
proxytest.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
package proxytest
import (
"crypto/tls"
"io"
"net"
"net/http"
"net/http/httptest"
"time"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/loadbalancer"
"github.com/zalando/skipper/logging/loggingtest"
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/routing"
"github.com/zalando/skipper/routing/testdataclient"
)
type TestProxy struct {
URL string
Port string
Log *loggingtest.Logger
dc *testdataclient.Client
routing *routing.Routing
proxy *proxy.Proxy
server *httptest.Server
}
type TestClient struct {
*http.Client
}
type Config struct {
RoutingOptions routing.Options
ProxyParams proxy.Params
Routes []*eskip.Route
Certificates []tls.Certificate
}
func WithParamsAndRoutingOptions(fr filters.Registry, proxyParams proxy.Params, o routing.Options, routes ...*eskip.Route) *TestProxy {
o.FilterRegistry = fr
return Config{
RoutingOptions: o,
ProxyParams: proxyParams,
Routes: routes,
}.Create()
}
func WithRoutingOptions(fr filters.Registry, o routing.Options, routes ...*eskip.Route) *TestProxy {
o.FilterRegistry = fr
return Config{
RoutingOptions: o,
ProxyParams: proxy.Params{CloseIdleConnsPeriod: -time.Second},
Routes: routes,
}.Create()
}
func WithParams(fr filters.Registry, proxyParams proxy.Params, routes ...*eskip.Route) *TestProxy {
return Config{
RoutingOptions: routing.Options{FilterRegistry: fr},
ProxyParams: proxyParams,
Routes: routes,
}.Create()
}
func New(fr filters.Registry, routes ...*eskip.Route) *TestProxy {
return Config{
RoutingOptions: routing.Options{FilterRegistry: fr},
ProxyParams: proxy.Params{CloseIdleConnsPeriod: -time.Second},
Routes: routes,
}.Create()
}
func (c Config) Create() *TestProxy {
tl := loggingtest.New()
var dc *testdataclient.Client
if len(c.RoutingOptions.DataClients) == 0 {
dc = testdataclient.New(c.Routes)
c.RoutingOptions.DataClients = []routing.DataClient{dc}
}
c.RoutingOptions.Log = tl
c.RoutingOptions.PostProcessors = append(c.RoutingOptions.PostProcessors, loadbalancer.NewAlgorithmProvider())
rt := routing.New(c.RoutingOptions)
c.ProxyParams.Routing = rt
pr := proxy.WithParams(c.ProxyParams)
var tsp *httptest.Server
if len(c.Certificates) > 0 {
tsp = httptest.NewUnstartedServer(pr)
tsp.TLS = &tls.Config{Certificates: c.Certificates}
tsp.StartTLS()
} else {
tsp = httptest.NewServer(pr)
}
if err := tl.WaitFor("route settings applied", 3*time.Second); err != nil {
panic(err)
}
_, port, _ := net.SplitHostPort(tsp.Listener.Addr().String())
return &TestProxy{
URL: tsp.URL,
Port: port,
Log: tl,
dc: dc,
routing: rt,
proxy: pr,
server: tsp,
}
}
func (p *TestProxy) Client() *TestClient {
return &TestClient{p.server.Client()}
}
func (p *TestProxy) Close() error {
p.Log.Close()
if p.dc != nil {
p.dc.Close()
}
p.routing.Close()
p.server.Close()
return p.proxy.Close()
}
// GetBody issues a GET to the specified URL, reads and closes response body and
// returns response, response body bytes and error if any.
func (c *TestClient) GetBody(url string) (rsp *http.Response, body []byte, err error) {
rsp, err = c.Get(url)
if err != nil {
return
}
defer rsp.Body.Close()
body, err = io.ReadAll(rsp.Body)
return
}