-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_middleware_test.go
67 lines (51 loc) · 1.31 KB
/
client_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
package amqprpc
import (
"bytes"
"fmt"
"io"
"testing"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func traceClientMiddleware(id int, w io.Writer) ClientMiddlewareFunc {
return func(next SendFunc) SendFunc {
return func(r *Request) (*amqp.Delivery, error) {
fmt.Fprint(w, id)
res, err := next(r)
fmt.Fprint(w, id)
return res, err
}
}
}
func TestClientMiddlewareChain(t *testing.T) {
var (
req = NewRequest()
b = bytes.Buffer{}
)
mw := ClientMiddlewareChain(
func(_ *Request) (*amqp.Delivery, error) {
fmt.Fprintf(&b, "X")
dx := amqp.Delivery{}
return &dx, nil
},
traceClientMiddleware(1, &b),
traceClientMiddleware(2, &b),
)
res, err := mw(req)
require.NoError(t, err, "no errors chaining middlewares")
assert.NotNil(t, res, "result is not nil after passing through middlewares")
assert.Equal(t, []byte("12X21"), b.Bytes(), "middlewares executed in correct order")
}
func TestClientClientAddMiddlewares(t *testing.T) {
c := NewClient("")
assert.Empty(t, c.middlewares, "zero middlewares at start")
c.AddMiddleware(
func(n SendFunc) SendFunc {
return func(r *Request) (*amqp.Delivery, error) {
return n(r)
}
},
)
assert.Len(t, c.middlewares, 1, "adding middlewares working")
}