-
Notifications
You must be signed in to change notification settings - Fork 8
/
server_middleware_test.go
51 lines (43 loc) · 1.27 KB
/
server_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
package amqprpc
import (
"context"
"fmt"
"testing"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/assert"
)
func traceServerMiddleware(id int) ServerMiddlewareFunc {
return func(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, rw *ResponseWriter, d amqp.Delivery) {
fmt.Fprint(rw, id)
next(ctx, rw, d)
fmt.Fprint(rw, id)
}
}
}
func TestServerMiddlewareChain(t *testing.T) {
handler := ServerMiddlewareChain(
func(_ context.Context, rw *ResponseWriter, _ amqp.Delivery) {
fmt.Fprint(rw, "X")
},
traceServerMiddleware(1),
traceServerMiddleware(2),
traceServerMiddleware(3),
traceServerMiddleware(4),
)
unevenHandler := ServerMiddlewareChain(
func(_ context.Context, rw *ResponseWriter, _ amqp.Delivery) {
fmt.Fprint(rw, "Y")
},
traceServerMiddleware(1),
traceServerMiddleware(2),
traceServerMiddleware(3),
)
rWriter := &ResponseWriter{
Publishing: &amqp.Publishing{},
}
handler(context.Background(), rWriter, amqp.Delivery{})
assert.Equal(t, "1234X4321", string(rWriter.Publishing.Body), "middlewares are called in correct order")
unevenHandler(context.Background(), rWriter, amqp.Delivery{})
assert.Equal(t, "1234X4321123Y321", string(rWriter.Publishing.Body), "all middlewares are called")
}