-
Notifications
You must be signed in to change notification settings - Fork 0
/
reg_middleware_test.go
72 lines (66 loc) · 1.65 KB
/
reg_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
package hureg
import (
"testing"
"github.com/danielgtaylor/huma/v2"
"github.com/stretchr/testify/require"
"github.com/cardinalby/hureg/pkg/huma/op_handler"
)
func TestNewRegMiddleware(t *testing.T) {
t.Parallel()
op := huma.Operation{
Tags: []string{"tag1"},
}
rm := NewRegMiddleware(
op_handler.AddTags("tag2"),
op_handler.AddTags("tag3"),
)
receiverWasCalled := false
rm(op, func(op huma.Operation) {
receiverWasCalled = true
require.Equal(t, []string{"tag1", "tag2", "tag3"}, op.Tags)
})
require.True(t, receiverWasCalled)
}
func TestRegMiddlewares_Chain(t *testing.T) {
t.Parallel()
op := huma.Operation{
Tags: []string{"tag1"},
}
rms := RegMiddlewares{
NewRegMiddleware(op_handler.AddTags("tag2")),
NewRegMiddleware(op_handler.AddTags("tag3")),
}
chained := rms.Chain()
receiverWasCalled := false
chained(op, func(op huma.Operation) {
receiverWasCalled = true
require.Equal(t, []string{"tag1", "tag2", "tag3"}, op.Tags)
})
require.True(t, receiverWasCalled)
}
func TestRegMiddlewares_FanOut(t *testing.T) {
t.Parallel()
op := huma.Operation{
Method: "GET",
Path: "/",
Tags: []string{"tag1"},
}
rms := RegMiddlewares{
NewRegMiddleware(op_handler.AddTags("tag2")),
NewRegMiddleware(op_handler.AddTags("tag3")),
}
fanOut := rms.FanOut()
receiverCallsNumber := 0
fanOut(op, func(op huma.Operation) {
receiverCallsNumber++
switch receiverCallsNumber {
case 1:
require.Equal(t, []string{"tag1", "tag2"}, op.Tags)
case 2:
require.Equal(t, []string{"tag1", "tag3"}, op.Tags)
default:
require.Fail(t, "unexpected receiver call: %d", receiverCallsNumber)
}
})
require.Equal(t, 2, receiverCallsNumber)
}