-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_test.go
54 lines (44 loc) · 1.18 KB
/
endpoint_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
package box
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestChain(t *testing.T) {
records := []string{}
recordingMiddleware := func(name string) Middleware[string, string] {
return Middleware[string, string](func(next Endpoint[string, string]) Endpoint[string, string] {
return Endpoint[string, string](func(ctx context.Context, req string) (string, error) {
records = append(records, fmt.Sprintf("inc-%s", name))
resp, err := next(ctx, req)
records = append(records, fmt.Sprintf("out-%s", name))
return resp, err
})
})
}
// setup endpoint with it's middlewares
mw := Chain(
recordingMiddleware("first"),
recordingMiddleware("second"),
recordingMiddleware("third"),
)
ep := Endpoint[string, string](func(_ context.Context, req string) (string, error) {
records = append(records, req)
return "response", nil
})
ep = mw(ep)
resp, err := ep(context.Background(), "request")
assert.NoError(t, err)
assert.Equal(t, "response", resp)
expectedRecords := []string{
"inc-first",
"inc-second",
"inc-third",
"request",
"out-third",
"out-second",
"out-first",
}
assert.Equal(t, expectedRecords, records)
}