-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtyped_test.go
143 lines (109 loc) · 3.49 KB
/
typed_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package peanats
import (
"encoding/json"
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type testAdapterArgument struct {
Arg string `json:"arg"`
}
type testAdapterResult struct {
Res string `json:"res"`
}
func TestTyped(t *testing.T) {
t.Run("ok", func(t *testing.T) {
arg := testAdapterArgument{Arg: "Hello, world!"}
res := testAdapterResult{Res: "Goodbye, world!"}
p := TypedHandlerFunc[testAdapterArgument, testAdapterResult](
func(pub TypedPublisher[testAdapterResult], req TypedRequest[testAdapterArgument]) error {
return pub.Publish(&res)
},
)
f := Typed[testAdapterArgument, testAdapterResult](&JsonCodec{}, p)
pub := new(publisherMock)
defer pub.AssertExpectations(t)
req := new(requestMock)
defer req.AssertExpectations(t)
req.On("Data").Return(must(json.Marshal(&arg)))
pub.On("Publish", mock.Anything).Run(func(args mock.Arguments) {
data := args.Get(0).([]byte)
assert.Equal(t, must(json.Marshal(&res)), data)
}).Return(nil)
err := f.Serve(pub, req)
require.NoError(t, err)
req.AssertExpectations(t)
pub.AssertExpectations(t)
})
t.Run("decode error", func(t *testing.T) {
p := TypedHandlerFunc[testAdapterArgument, testAdapterResult](
func(pub TypedPublisher[testAdapterResult], req TypedRequest[testAdapterArgument]) error {
t.Fatal("should not be called")
return nil
},
)
errDecodeError := errors.New("decode error")
c := new(codecMock)
defer c.AssertExpectations(t)
f := Typed[testAdapterArgument, testAdapterResult](c, p)
pub := new(publisherMock)
defer pub.AssertExpectations(t)
req := new(requestMock)
defer req.AssertExpectations(t)
req.On("Data").Return([]byte("Hello, world!"))
c.On("Decode", []byte("Hello, world!"), mock.Anything).Return(errDecodeError)
err := f.Serve(pub, req)
require.Error(t, err)
require.True(t, errors.Is(err, errDecodeError))
var errObj *Error
require.True(t, errors.As(err, &errObj))
require.Equal(t, http.StatusBadRequest, errObj.Code)
req.AssertExpectations(t)
pub.AssertExpectations(t)
})
t.Run("encode error", func(t *testing.T) {
arg := testAdapterArgument{Arg: "Hello, world!"}
res := testAdapterResult{Res: "Goodbye, world!"}
c := new(codecMock)
defer c.AssertExpectations(t)
af := TypedHandlerFunc[testAdapterArgument, testAdapterResult](
func(pub TypedPublisher[testAdapterResult], req TypedRequest[testAdapterArgument]) error {
return pub.Publish(&res)
},
)
f := Typed[testAdapterArgument, testAdapterResult](c, af)
pub := new(publisherMock)
defer pub.AssertExpectations(t)
req := new(requestMock)
defer req.AssertExpectations(t)
req.On("Data").Return(must(json.Marshal(&arg)))
encodeErr := errors.New("encode error")
c.On("Decode", mock.Anything, mock.Anything).Return(nil)
c.On("Encode", mock.Anything).Return(nil, encodeErr)
err := f.Serve(pub, req)
require.Error(t, err)
require.True(t, errors.Is(err, encodeErr))
var errObj *Error
require.True(t, errors.As(err, &errObj))
require.Equal(t, http.StatusInternalServerError, errObj.Code)
req.AssertExpectations(t)
pub.AssertExpectations(t)
})
}
type codecMock struct {
mock.Mock
}
func (c *codecMock) Encode(v any) ([]byte, error) {
args := c.Called(v)
if err := args.Error(1); err != nil {
return nil, err
}
return args.Get(0).([]byte), nil
}
func (c *codecMock) Decode(data []byte, vPtr any) error {
args := c.Called(data, vPtr)
return args.Error(0)
}