-
Notifications
You must be signed in to change notification settings - Fork 32
/
middleware_test.go
62 lines (54 loc) · 1.7 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
package loomchain
import (
"testing"
"github.com/stretchr/testify/require"
common "github.com/tendermint/tendermint/libs/common"
)
type appHandler struct {
t *testing.T
}
var appTag = common.KVPair{Key: []byte("AppKey"), Value: []byte("AppValue")}
func (a *appHandler) ProcessTx(state State, txBytes []byte, isCheckTx bool) (TxHandlerResult, error) {
require.Equal(a.t, txBytes, []byte("AppData"))
return TxHandlerResult{
Tags: []common.KVPair{appTag},
}, nil
}
// Test that middleware is applied in the correct order, and that tags are set correctly.
func TestMiddlewareTxHandler(t *testing.T) {
allBytes := []byte("FirstMW/SecondMW/AppData")
mw1Tag := common.KVPair{Key: []byte("MW1Key"), Value: []byte("MW1Value")}
mw1Func := TxMiddlewareFunc(
func(state State, txBytes []byte, next TxHandlerFunc, isCheckTx bool) (TxHandlerResult, error) {
require.Equal(t, txBytes, allBytes)
r, err := next(state, txBytes[len("FirstMW/"):], false)
if err != nil {
return r, err
}
r.Tags = append(r.Tags, mw1Tag)
return r, err
},
)
mw2Tag := common.KVPair{Key: []byte("MW2Key"), Value: []byte("MW2Value")}
mw2Func := TxMiddlewareFunc(
func(state State, txBytes []byte, next TxHandlerFunc, isCheckTx bool) (TxHandlerResult, error) {
require.Equal(t, txBytes, []byte("SecondMW/AppData"))
r, err := next(state, txBytes[len("SecondMW/"):], false)
if err != nil {
return r, err
}
r.Tags = append(r.Tags, mw2Tag)
return r, err
},
)
mwHandler := MiddlewareTxHandler(
[]TxMiddleware{
mw1Func,
mw2Func,
},
&appHandler{t: t},
[]PostCommitMiddleware{},
)
r, _ := mwHandler.ProcessTx(nil, allBytes, false)
require.Equal(t, r.Tags, []common.KVPair{appTag, mw2Tag, mw1Tag})
}