-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodec_test.go
More file actions
243 lines (223 loc) · 5.68 KB
/
Copy pathcodec_test.go
File metadata and controls
243 lines (223 loc) · 5.68 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package resonate
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"testing"
)
func newCodec() *Codec { return NewCodec(nil) }
func TestCodecRoundtripInteger(t *testing.T) {
c := newCodec()
v, err := c.Encode(42)
if err != nil {
t.Fatal(err)
}
var out int64
ok, err := c.Decode(v, &out)
if err != nil || !ok || out != 42 {
t.Errorf("got (%v, %v, ok=%v); want (42, nil, ok=true)", out, err, ok)
}
}
func TestCodecRoundtripString(t *testing.T) {
c := newCodec()
v, err := c.Encode("hello")
if err != nil {
t.Fatal(err)
}
var out string
ok, _ := c.Decode(v, &out)
if !ok || out != "hello" {
t.Errorf("got (%q, ok=%v); want (hello, true)", out, ok)
}
}
func TestCodecRoundtripBool(t *testing.T) {
c := newCodec()
v, _ := c.Encode(true)
var out bool
ok, _ := c.Decode(v, &out)
if !ok || out != true {
t.Errorf("got (%v, ok=%v); want (true, true)", out, ok)
}
}
func TestCodecRoundtripObject(t *testing.T) {
c := newCodec()
obj := map[string]any{"func": "f", "args": []any{1.0, "two"}}
v, _ := c.Encode(obj)
var out map[string]any
ok, _ := c.Decode(v, &out)
if !ok {
t.Fatal("decode failed")
}
if out["func"] != "f" {
t.Errorf("func = %v", out["func"])
}
}
func TestCodecRoundtripArray(t *testing.T) {
c := newCodec()
arr := []int{1, 2, 3}
v, _ := c.Encode(arr)
var out []int
ok, _ := c.Decode(v, &out)
if !ok || len(out) != 3 || out[2] != 3 {
t.Errorf("got (%v, ok=%v)", out, ok)
}
}
func TestCodecDecodeBase64StrEmptyReturnsNotOK(t *testing.T) {
c := newCodec()
var out any
ok, err := c.DecodeBase64("", &out)
if err != nil || ok {
t.Errorf("got (ok=%v, err=%v); want (false, nil)", ok, err)
}
}
func TestCodecDecodeBase64StrRoundtrip(t *testing.T) {
c := newCodec()
v, _ := c.Encode(map[string]int{"x": 1})
// Pull the string out of v.Data (which is JSON-quoted)
var s string
if err := json.Unmarshal(v.Data, &s); err != nil {
t.Fatalf("data not a json string: %v", err)
}
var out map[string]int
ok, err := c.DecodeBase64(s, &out)
if err != nil || !ok || out["x"] != 1 {
t.Errorf("got (%v, ok=%v, err=%v)", out, ok, err)
}
}
func TestCodecEncodeNullProducesEmptyData(t *testing.T) {
c := newCodec()
v, _ := c.Encode(nil)
if string(v.Data) != `""` {
t.Errorf("data = %s, want \"\"", v.Data)
}
var out any
ok, _ := c.Decode(v, &out)
if ok {
t.Errorf("decode of null-encoded value should return ok=false, got out=%v", out)
}
}
func TestCodecEncodeProducesValidBase64(t *testing.T) {
c := newCodec()
v, _ := c.Encode("hello")
var s string
if err := json.Unmarshal(v.Data, &s); err != nil {
t.Fatalf("data not a json string: %v", err)
}
if !IsValidBase64(s) {
t.Errorf("expected valid base64, got %q", s)
}
bytes, _ := base64.StdEncoding.DecodeString(s)
var anyV any
if err := json.Unmarshal(bytes, &anyV); err != nil {
t.Errorf("base64 payload is not JSON: %v", err)
}
}
func TestCodecDecodeValueRoundtrip(t *testing.T) {
c := newCodec()
v, err := c.Encode(map[string]any{"x": 1.0})
if err != nil {
t.Fatal(err)
}
v.Headers = map[string]string{"k": "h"}
decoded, err := c.DecodeValue(v)
if err != nil {
t.Fatal(err)
}
if decoded.Headers["k"] != "h" {
t.Errorf("headers not preserved: %v", decoded.Headers)
}
var obj map[string]any
_ = json.Unmarshal(decoded.Data, &obj)
if obj["x"] != 1.0 {
t.Errorf("x = %v", obj["x"])
}
}
func TestCodecDecodeValueEmpty(t *testing.T) {
c := newCodec()
decoded, err := c.DecodeValue(Value{})
if err != nil {
t.Fatal(err)
}
if string(decoded.Data) != "null" {
t.Errorf("Data = %s, want null", decoded.Data)
}
}
func TestCodecDecodePromiseDecodesParamAndValue(t *testing.T) {
c := newCodec()
paramV, _ := c.Encode(map[string]any{"func": "f"})
valueV, _ := c.Encode(map[string]any{"result": 42.0})
pr := PromiseRecord{
ID: "test",
State: PromiseStateResolved,
Param: paramV,
Value: valueV,
}
decoded, err := c.DecodePromise(pr)
if err != nil {
t.Fatal(err)
}
var pObj map[string]any
_ = json.Unmarshal(decoded.Param.Data, &pObj)
if pObj["func"] != "f" {
t.Errorf("param.func = %v", pObj["func"])
}
var vObj map[string]any
_ = json.Unmarshal(decoded.Value.Data, &vObj)
if vObj["result"] != 42.0 {
t.Errorf("value.result = %v", vObj["result"])
}
}
// TestCodecDecodePromisePreservesInt64 verifies that DecodePromise preserves
// integer precision above 2^53 (regression for the int64-→-float64 round-trip
// the previous decodeValueData implementation introduced).
func TestCodecDecodePromisePreservesInt64(t *testing.T) {
c := newCodec()
const big int64 = 1<<60 + 7
v, err := c.Encode(big)
if err != nil {
t.Fatal(err)
}
pr := PromiseRecord{
ID: "test",
State: PromiseStateResolved,
Value: v,
}
decoded, err := c.DecodePromise(pr)
if err != nil {
t.Fatal(err)
}
if string(decoded.Value.Data) != fmt.Sprintf("%d", big) {
t.Fatalf("expected raw %d, got %s", big, decoded.Value.Data)
}
}
func TestCodecDecodeInvalidBase64ReturnsError(t *testing.T) {
c := newCodec()
bad := Value{Data: json.RawMessage(`"not-base64!!!"`)}
var out any
if _, err := c.Decode(bad, &out); err == nil {
t.Error("expected error, got nil")
}
}
func TestEncodeErrorShape(t *testing.T) {
raw := EncodeError(&ApplicationError{Message: "boom"})
var obj map[string]any
_ = json.Unmarshal(raw, &obj)
if obj["__type"] != "error" {
t.Errorf("__type = %v", obj["__type"])
}
if obj["message"] != "application error: boom" {
t.Errorf("message = %v", obj["message"])
}
}
func TestDeserializeErrorRoundtrip(t *testing.T) {
raw := EncodeError(errors.New("kaboom"))
err := DeserializeError(raw)
var ae *ApplicationError
if !errors.As(err, &ae) {
t.Fatalf("got %T, want *ApplicationError", err)
}
if ae.Message != "kaboom" {
t.Errorf("message = %q, want kaboom", ae.Message)
}
}