-
Notifications
You must be signed in to change notification settings - Fork 11
/
proxy_test.go
67 lines (59 loc) · 1.98 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
package metrics
import (
"context"
"net/url"
"testing"
"time"
"github.com/luraproject/lura/v2/proxy"
"github.com/rcrowley/go-metrics"
)
func TestNewProxyMiddleware(t *testing.T) {
URL, _ := url.Parse("http://example.com/12345")
request := &proxy.Request{URL: URL}
response := &proxy.Response{Data: map[string]interface{}{}, IsComplete: true}
assertion := func(_ context.Context, req *proxy.Request) (*proxy.Response, error) {
if request != req {
t.Errorf("unexpected request! want [%v], have [%v]\n", request, req)
}
time.Sleep(time.Millisecond)
return response, nil
}
registry := metrics.NewRegistry()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
proxyMetric := NewProxyMetrics(®istry)
mw := NewProxyMiddleware("some", "none", proxyMetric)
for i := 0; i < 100; i++ {
resp, err := mw(assertion)(ctx, request)
if err != nil {
t.Error("unexpected error:", err)
return
}
if resp != response {
t.Errorf("unexpected response! want [%v], have [%v]\n", response, resp)
return
}
}
expected := map[string]struct{}{
"proxy.latency.layer.some.name.none.complete.true.error.true": {},
"proxy.latency.layer.some.name.none.complete.true.error.false": {},
"proxy.latency.layer.some.name.none.complete.false.error.true": {},
"proxy.latency.layer.some.name.none.complete.false.error.false": {},
"proxy.requests.layer.some.name.none.complete.true.error.true": {},
"proxy.requests.layer.some.name.none.complete.true.error.false": {},
"proxy.requests.layer.some.name.none.complete.false.error.true": {},
"proxy.requests.layer.some.name.none.complete.false.error.false": {},
}
tracked := make([]string, 0, len(expected))
proxyMetric.register.Each(func(k string, _ interface{}) {
tracked = append(tracked, k)
})
if len(tracked) != len(expected) {
t.Error("unexpected size of the tracked list", tracked)
}
for _, k := range tracked {
if _, ok := expected[k]; !ok {
t.Error("the key", k, " has not been tracked")
}
}
}