forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware_test.go
127 lines (117 loc) · 3.32 KB
/
middleware_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
package main
import (
"context"
"fmt"
"math"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"github.com/kedacore/http-add-on/pkg/queue"
)
func TestCountMiddleware(t *testing.T) {
ctx := context.Background()
const host = "testingkeda.com"
r := require.New(t)
queueCounter := queue.NewFakeCounter()
middleware := countMiddleware(
logr.Discard(),
queueCounter,
http.HandlerFunc(func(wr http.ResponseWriter, req *http.Request) {
wr.WriteHeader(200)
_, err := wr.Write([]byte("OK"))
r.NoError(err)
}),
)
// no host in the request
req, err := http.NewRequest("GET", "/something", nil)
r.NoError(err)
agg, respRecorder := expectResizes(
ctx,
t,
0,
middleware,
req,
queueCounter,
func(t *testing.T, hostAndCount queue.HostAndCount) {},
)
r.Equal(400, respRecorder.Code)
r.Equal("Host not found, not forwarding request", respRecorder.Body.String())
r.Equal(0, agg)
// run middleware with the host in the request
req, err = http.NewRequest("GET", "/something", nil)
r.NoError(err)
req.Host = host
// for a valid request, we expect the queue to be resized twice.
// once to mark a pending HTTP request, then a second time to remove it.
// by the end of both sends, resize1 + resize2 should be 0,
// or in other words, the queue size should be back to zero
agg, respRecorder = expectResizes(
ctx,
t,
2,
middleware,
req,
queueCounter,
func(t *testing.T, hostAndCount queue.HostAndCount) {
t.Helper()
r := require.New(t)
r.Equal(float64(1), math.Abs(float64(hostAndCount.Count)))
r.Equal(host, hostAndCount.Host)
},
)
r.Equal(200, respRecorder.Code)
r.Equal("OK", respRecorder.Body.String())
r.Equal(0, agg)
}
// expectResizes creates a new httptest.ResponseRecorder, then passes req through
// the middleware. every time the middleware calls fakeCounter.Resize(), it calls
// resizeCheckFn with t and the queue.HostCount that represents the resize call
// that was made. it also maintains an aggregate delta of the counts passed to
// Resize. If, for example, the following integers were passed to resize over
// 4 calls: [-1, 1, 1, 2], the aggregate would be -1+1+1+2=3
//
// this function returns the aggregate and the httptest.ResponseRecorder that was
// created and used with the middleware
func expectResizes(
ctx context.Context,
t *testing.T,
nResizes int,
middleware http.Handler,
req *http.Request,
fakeCounter *queue.FakeCounter,
resizeCheckFn func(*testing.T, queue.HostAndCount),
) (int, *httptest.ResponseRecorder) {
t.Helper()
r := require.New(t)
const timeout = 1 * time.Second
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
grp, ctx := errgroup.WithContext(ctx)
agg := 0
grp.Go(func() error {
// we expect the queue to be resized nResizes times
for i := 0; i < nResizes; i++ {
select {
case hostAndCount := <-fakeCounter.ResizedCh:
agg += hostAndCount.Count
resizeCheckFn(t, hostAndCount)
case <-ctx.Done():
return fmt.Errorf(
"timed out waiting for the count middleware. expected %d resizes, timeout was %s, iteration %d",
nResizes,
timeout,
i,
)
}
}
return nil
})
respRecorder := httptest.NewRecorder()
middleware.ServeHTTP(respRecorder, req)
r.NoError(grp.Wait())
return agg, respRecorder
}