-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproxy_test.go
161 lines (129 loc) · 3.78 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
package metrics
import (
"context"
"errors"
"testing"
"time"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/proxy"
"github.com/newrelic/go-agent"
)
func TestProxyFactory_okAppNil(t *testing.T) {
app = nil
cfg := &config.EndpointConfig{
Endpoint: "/my_endpoint",
Timeout: time.Second,
Method: "GET",
}
errorExpected := errors.New("expected error")
pf := proxy.FactoryFunc(func(_ *config.EndpointConfig) (proxy.Proxy, error) {
return proxy.NoopProxy, errorExpected
})
if _, err := ProxyFactory("segm", pf)(cfg); err != errorExpected {
t.Errorf("unexpected error: %v", err)
}
}
func TestProxyFactory_okNRApp(t *testing.T) {
nrApp := newApp()
defer func() { app = nil }()
app = &Application{nrApp, Config{InstrumentationRate: 100}}
cfg := &config.EndpointConfig{
Endpoint: "/my_endpoint",
Timeout: time.Second,
Method: "GET",
}
expectedResponse := &proxy.Response{
Data: map[string]interface{}{
"key": "result",
}}
pf := proxy.FactoryFunc(func(_ *config.EndpointConfig) (proxy.Proxy, error) {
return func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return expectedResponse, nil
}, nil
})
totalCalls := 0
txn := newTx()
txn.startSegmentNow = func() newrelic.SegmentStartTime {
totalCalls++
return newrelic.SegmentStartTime{}
}
pr, err := ProxyFactory("segm", pf)(cfg)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
return
}
if resp, err := pr(context.WithValue(context.Background(), nrCtxKey, txn), nil); err != nil {
t.Errorf("unexpected error: %s", err.Error())
} else if resp != expectedResponse {
t.Errorf("unexpected response: %v", resp)
}
if totalCalls != 1 {
t.Errorf("wrong number of segments, got: %d, wanted 1", totalCalls)
}
}
func TestNewProxyMiddleware_okAppNil(t *testing.T) {
app = nil
expectedResponse := &proxy.Response{
Data: map[string]interface{}{
"key": "result",
}}
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return expectedResponse, nil
}
totalCalls := 0
txn := newTx()
txn.startSegmentNow = func() newrelic.SegmentStartTime {
totalCalls++
return newrelic.SegmentStartTime{}
}
pr := NewProxyMiddleware("segm")(p)
if resp, err := pr(context.WithValue(context.Background(), nrCtxKey, txn), nil); err != nil {
t.Errorf("unexpected error, %s", err.Error())
} else if resp != expectedResponse {
t.Errorf("unexpected response: %v", resp)
}
if totalCalls != 0 {
t.Errorf("wrong number of segments, got: %d, wanted 0", totalCalls)
}
}
func TestNewProxyMiddleware_koPanicTooManyProxies(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected panic, too many proxies for this proxy middleware")
}
}()
nrApp := newApp()
defer func() { app = nil }()
app = &Application{nrApp, Config{InstrumentationRate: 100}}
expectedResponse := &proxy.Response{
Data: map[string]interface{}{
"key": "result",
}}
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return expectedResponse, nil
}
totalCalls := 0
txn := newTx()
txn.startSegmentNow = func() newrelic.SegmentStartTime {
totalCalls++
return newrelic.SegmentStartTime{}
}
NewProxyMiddleware("segm")(p, p)(context.WithValue(context.Background(), nrCtxKey, txn), nil)
}
func TestNewProxyMiddleware_koPanicNoProxies(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected panic, not enough proxies for this endpoint")
}
}()
nrApp := newApp()
defer func() { app = nil }()
app = &Application{nrApp, Config{InstrumentationRate: 100}}
totalCalls := 0
txn := newTx()
txn.startSegmentNow = func() newrelic.SegmentStartTime {
totalCalls++
return newrelic.SegmentStartTime{}
}
NewProxyMiddleware("segm")()(context.WithValue(context.Background(), nrCtxKey, txn), nil)
}