-
Notifications
You must be signed in to change notification settings - Fork 39
/
response_test.go
461 lines (375 loc) · 15.6 KB
/
response_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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package resp_test
import (
"bytes"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
"github.com/bsm/redeo/v2/resp"
)
var _ = Describe("ResponseWriter", func() {
var subject resp.ResponseWriter
var buf = new(bytes.Buffer)
BeforeEach(func() {
buf.Reset()
subject = resp.NewResponseWriter(buf)
})
It("should append bulks", func() {
subject.AppendBulk([]byte("dAtA"))
Expect(buf.String()).To(BeEmpty())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("$4\r\ndAtA\r\n"))
})
It("should append bulk strings", func() {
subject.AppendBulkString("PONG")
Expect(buf.String()).To(BeEmpty())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("$4\r\nPONG\r\n"))
subject.AppendBulkString("日本")
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("$4\r\nPONG\r\n$6\r\n日本\r\n"))
})
It("should append inline bytes", func() {
subject.AppendInline([]byte("dAtA"))
Expect(buf.String()).To(BeEmpty())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("+dAtA\r\n"))
})
It("should append inline strings", func() {
subject.AppendInlineString("PONG")
Expect(buf.String()).To(BeEmpty())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("+PONG\r\n"))
})
It("should append errors", func() {
subject.AppendError("WRONGTYPE not a number")
Expect(buf.String()).To(BeEmpty())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("-WRONGTYPE not a number\r\n"))
})
It("should append ints", func() {
subject.AppendInt(27)
Expect(buf.String()).To(BeEmpty())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal(":27\r\n"))
subject.AppendInt(1)
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal(":27\r\n:1\r\n"))
})
It("should append nils", func() {
subject.AppendNil()
Expect(buf.String()).To(BeEmpty())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("$-1\r\n"))
})
It("should append OK", func() {
subject.AppendOK()
Expect(buf.String()).To(BeEmpty())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("+OK\r\n"))
})
It("should copy from readers", func() {
src := strings.NewReader("this is a streaming data source")
subject.AppendArrayLen(1)
Expect(buf.String()).To(BeEmpty())
Expect(subject.CopyBulk(src, 16)).To(Succeed())
Expect(subject.Flush()).To(Succeed())
Expect(buf.String()).To(Equal("*1\r\n$16\r\nthis is a stream\r\n"))
})
DescribeTable("Append",
func(v interface{}, exp string) {
Expect(subject.Append(v)).To(Succeed())
Expect(subject.Flush()).To(Succeed())
Expect(strconv.Quote(buf.String())).To(Equal(strconv.Quote(exp)))
},
Entry("nil", nil, "$-1\r\n"),
Entry("error", errors.New("failed"), "-ERR failed\r\n"),
Entry("standard error", errors.New("ERR failed"), "-ERR failed\r\n"),
Entry("int", 33, ":33\r\n"),
Entry("int64", int64(33), ":33\r\n"),
Entry("uint", uint(33), ":33\r\n"),
Entry("bool (true)", true, ":1\r\n"),
Entry("bool (false)", false, ":0\r\n"),
Entry("float32", float32(0.1231), "+0.1231\r\n"),
Entry("float64", 0.7357, "+0.7357\r\n"),
Entry("negative float64", -0.4214, "+-0.4214\r\n"),
Entry("string", "many words", "$10\r\nmany words\r\n"),
Entry("[]byte", []byte("many words"), "$10\r\nmany words\r\n"),
Entry("[]string", []string{"a", "b", "c"}, "*3\r\n$1\r\na\r\n$1\r\nb\r\n$1\r\nc\r\n"),
Entry("[][]byte", [][]byte{{'a'}, {'b'}, {'c'}}, "*3\r\n$1\r\na\r\n$1\r\nb\r\n$1\r\nc\r\n"),
Entry("[]int", []int{3, 5, 2}, "*3\r\n:3\r\n:5\r\n:2\r\n"),
Entry("[]int64", []int64{7, 8, 3}, "*3\r\n:7\r\n:8\r\n:3\r\n"),
Entry("[][]int64", [][]int64{{1, 2}, {3, 4}}, "*2\r\n*2\r\n:1\r\n:2\r\n*2\r\n:3\r\n:4\r\n"),
Entry("map[string]string", map[string]string{"a": "b"}, "*2\r\n$1\r\na\r\n$1\r\nb\r\n"),
Entry("map[int64]float64", map[int64]float64{1: 1.1}, "*2\r\n:1\r\n+1.1\r\n"),
Entry("custom response", &customResponse{Host: "foo", Port: 8888}, "$17\r\ncustom 'foo:8888'\r\n"),
Entry("custom error", customErrorResponse("bar"), "-WRONG bar\r\n"),
)
It("should reject bad custom types", func() {
Expect(subject.Append(time.Time{})).To(MatchError(`resp: unsupported type time.Time`))
})
})
var _ = Describe("ResponseReader", func() {
var subject resp.ResponseReader
var buf = new(bytes.Buffer)
BeforeEach(func() {
buf.Reset()
subject = resp.NewResponseReader(buf)
})
It("should read nils", func() {
buf.WriteString("$-1\r\n+OK\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeNil))
err = subject.ReadNil()
Expect(err).NotTo(HaveOccurred())
// ensure we have consumed everything
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
})
It("should read strings", func() {
buf.WriteString("$4\r\nPING\r\n+OK\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeBulk))
s, err := subject.ReadBulkString()
Expect(err).NotTo(HaveOccurred())
Expect(s).To(Equal("PING"))
// ensure we have consumed everything
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
})
It("should read bytes", func() {
buf.WriteString("$4\r\nPiNG\r\n+OK\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeBulk))
s, err := subject.ReadBulk(nil)
Expect(err).NotTo(HaveOccurred())
Expect(s).To(Equal([]byte("PiNG")))
// ensure we have consumed everything
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
})
It("should read ints", func() {
buf.WriteString(":21412\r\n+OK\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInt))
n, err := subject.ReadInt()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(21412)))
// ensure we have consumed everything
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
})
It("should read negative ints", func() {
buf.WriteString(":-321\r\n+OK\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInt))
n, err := subject.ReadInt()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(-321)))
// ensure we have consumed everything
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
})
It("should read arrays", func() {
buf.WriteString("*2\r\n$5\r\nHeLLo\r\n$5\r\nwOrld\r\n+OK\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeArray))
n, err := subject.ReadArrayLen()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(2))
for i := 0; i < n; i++ {
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeBulk))
_, err = subject.ReadBulk(nil)
Expect(err).NotTo(HaveOccurred())
}
// ensure we have consumed everything
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
})
It("should read errors", func() {
buf.WriteString("-WRONGTYPE expected hash\r\n+OK\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeError))
s, err := subject.ReadError()
Expect(err).NotTo(HaveOccurred())
Expect(s).To(Equal("WRONGTYPE expected hash"))
// ensure we have consumed everything
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
})
It("should read statuses", func() {
buf.WriteString("+OK\r\n+OK\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
s, err := subject.ReadInlineString()
Expect(err).NotTo(HaveOccurred())
Expect(s).To(Equal("OK"))
// ensure we have consumed everything
t, err = subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
})
It("should read statuses across buffer overflows", func() {
s := strings.Repeat("x", 4000)
buf.WriteString("+")
buf.WriteString(s)
buf.WriteString("\r\n")
buf.WriteString("+")
buf.WriteString(s)
buf.WriteString("\r\n")
t, err := subject.PeekType()
Expect(err).NotTo(HaveOccurred())
Expect(t).To(Equal(resp.TypeInline))
s, err = subject.ReadInlineString()
Expect(err).NotTo(HaveOccurred())
Expect(len(s)).To(Equal(4000))
s, err = subject.ReadInlineString()
Expect(err).NotTo(HaveOccurred())
Expect(len(s)).To(Equal(4000))
_, err = subject.PeekType()
Expect(err).To(MatchError("EOF"))
})
Describe("Scan", func() {
DescribeTable("success",
func(s string, v interface{}, exp interface{}) {
_, err := buf.WriteString(s)
Expect(err).NotTo(HaveOccurred())
Expect(subject.Scan(v)).To(Succeed())
Expect(reflect.ValueOf(v).Elem().Interface()).To(Equal(exp))
},
Entry("bool (numeric true)", ":1\r\n", new(bool), true),
Entry("bool (numeric false)", ":0\r\n", new(bool), false),
Entry("bool (OK)", "+OK\r\n", new(bool), true),
Entry("bool (true from inline)", "+1\r\n", new(bool), true),
Entry("bool (true from bulk)", "$1\r\n1\r\n", new(bool), true),
Entry("bool (false from inline)", "+0\r\n", new(bool), false),
Entry("bool (false from bulk)", "$1\r\n0\r\n", new(bool), false),
Entry("int64", ":123\r\n", new(int64), int64(123)),
Entry("int32", ":123\r\n", new(int32), int32(123)),
Entry("int16", ":123\r\n", new(int16), int16(123)),
Entry("int8", ":123\r\n", new(int8), int8(123)),
Entry("int", ":123\r\n", new(int), int(123)),
Entry("int (from inline)", "+123\r\n", new(int), int(123)),
Entry("int (from bulk)", "$3\r\n123\r\n", new(int), int(123)),
Entry("uint64", ":123\r\n", new(uint64), uint64(123)),
Entry("uint32", ":123\r\n", new(uint32), uint32(123)),
Entry("uint16", ":123\r\n", new(uint16), uint16(123)),
Entry("uint8", ":123\r\n", new(uint8), uint8(123)),
Entry("uint", ":123\r\n", new(uint), uint(123)),
Entry("uint (from inline)", "+123\r\n", new(uint), uint(123)),
Entry("uint (from bulk)", "$3\r\n123\r\n", new(uint), uint(123)),
Entry("float64 (from string)", "+2.312\r\n", new(float64), 2.312),
Entry("float64 (from int)", ":123\r\n", new(float64), 123.0),
Entry("float32 (from string)", "$5\r\n2.312\r\n", new(float32), float32(2.312)),
Entry("float32 (from int)", ":123\r\n", new(float32), float32(123.0)),
Entry("string (inline)", "+hello\r\n", new(string), "hello"),
Entry("string (bulk)", "$5\r\nhello\r\n", new(string), "hello"),
Entry("string (from int)", ":123\r\n", new(string), "123"),
Entry("bytes (inline)", "+hello\r\n", new([]byte), []byte("hello")),
Entry("bytes (bulk)", "$5\r\nhello\r\n", new([]byte), []byte("hello")),
Entry("bytes (from int)", ":123\r\n", new([]byte), []byte("123")),
Entry("bytes (from nil)", "$-1\r\n", new([]byte), ([]byte)(nil)),
Entry("string slices", "*2\r\n+hello\r\n$5\r\nworld\r\n", new([]string), []string{"hello", "world"}),
Entry("string slices (with ints)", "*2\r\n+hello\r\n:123\r\n", new([]string), []string{"hello", "123"}),
Entry("number slices", "*2\r\n:1\r\n:2\r\n", new([]int64), []int64{1, 2}),
Entry("number slices (from strings)", "*2\r\n:1\r\n+2\r\n", new([]int64), []int64{1, 2}),
Entry("nested slices", "*2\r\n*2\r\n:1\r\n:2\r\n*2\r\n:3\r\n:4\r\n", new([][]int64), [][]int64{
{1, 2},
{3, 4},
}),
Entry("maps", "*2\r\n+hello\r\n$5\r\nworld\r\n", new(map[string]string), map[string]string{
"hello": "world",
}),
Entry("maps (mixed)", "*4\r\n+foo\r\n+bar\r\n+baz\r\n:3\r\n", new(map[string]string), map[string]string{
"foo": "bar",
"baz": "3",
}),
Entry("maps (nested)", "*4\r\n+foo\r\n*2\r\n+bar\r\n:1\r\n+baz\r\n*2\r\n+boo\r\n:2\r\n", new(map[string]map[string]int), map[string]map[string]int{
"foo": {"bar": 1},
"baz": {"boo": 2},
}),
Entry("slice of maps", "*2\r\n*2\r\n+bar\r\n:1\r\n*2\r\n+boo\r\n:2\r\n", new([]map[string]int), []map[string]int{
{"bar": 1},
{"boo": 2},
}),
Entry("nullable (from nil)", "$-1\r\n", new(resp.NullString), resp.NullString{}),
Entry("nullable (inline)", "+foo\r\n", new(resp.NullString), resp.NullString{Value: "foo", Valid: true}),
Entry("scannable", "*2\r\n"+
"*6\r\n+llen\r\n:2\r\n*2\r\n+readonly\r\n+fast\r\n:1\r\n:1\r\n:1\r\n"+
"*6\r\n+mset\r\n:-3\r\n*1\r\n+write\r\n:1\r\n:-1\r\n:2\r\n",
new([]ScannableStruct), []ScannableStruct{
{Name: "llen", Arity: 2, Flags: []string{"readonly", "fast"}, FirstKey: 1, LastKey: 1, KeyStep: 1},
{Name: "mset", Arity: -3, Flags: []string{"write"}, FirstKey: 1, LastKey: -1, KeyStep: 2},
}),
)
DescribeTable("failure",
func(s string, v interface{}, exp string) {
_, err := buf.WriteString(s)
Expect(err).NotTo(HaveOccurred())
Expect(subject.Scan(v)).To(MatchError(exp))
},
Entry("errors", "-ERR something bad\r\n", new(string), `resp: server error "ERR something bad"`),
Entry("bad type", "+hello\r\n", new(time.Time), `resp: error on Scan into *time.Time: unsupported conversion from "hello"`),
Entry("not a pointer", "+hello\r\n", "value", `resp: error on Scan into string: destination not a pointer`),
Entry("bool (bad type)", "*3\r\n", new(bool), `resp: error on Scan into *bool: unsupported conversion from array[3]`),
Entry("bool (string)", "+hello\r\n", new(bool), `resp: error on Scan into *bool: unsupported conversion from "hello"`),
Entry("bool (bad numeric)", ":2\r\n", new(bool), `resp: error on Scan into *bool: unsupported conversion from 2`),
Entry("bool (from nil)", "$-1\r\n", new(bool), `resp: error on Scan into *bool: unsupported conversion from <nil>`),
Entry("int64 (bad type)", "*3\r\n", new(int64), `resp: error on Scan into *int64: unsupported conversion from array[3]`),
Entry("int64 (string)", "+hello\r\n", new(int64), `resp: error on Scan into *int64: unsupported conversion from "hello"`),
Entry("int64 (from nil)", "$-1\r\n", new(int64), `resp: error on Scan into *int64: unsupported conversion from <nil>`),
Entry("string (bad type)", "*3\r\n", new(string), `resp: error on Scan into *string: unsupported conversion from array[3]`),
Entry("string (nil)", "$-1\r\n", new(string), `resp: error on Scan into *string: unsupported conversion from <nil>`),
Entry("float64 (bad type)", "*3\r\n", new(float64), `resp: error on Scan into *float64: unsupported conversion from array[3]`),
Entry("float64 (bad string)", "+hello\r\n", new(float64), `resp: error on Scan into *float64: unsupported conversion from "hello"`),
Entry("slices (bad type)", "+hello\r\n", new([]string), `resp: error on Scan into *[]string: unsupported conversion from "hello"`),
Entry("maps (odd number)", "*3\r\n+foo\r\n+bar\r\n+ba\r\n", new(map[string]string), `resp: error on Scan into *map[string]string: unsupported conversion from array[3]`),
)
DescribeTable("nil",
func(s string, v interface{}) {
_, err := buf.WriteString(s)
Expect(err).NotTo(HaveOccurred())
Expect(subject.Scan(v)).To(Succeed())
},
Entry("nil (from nil)", "$-1\r\n", nil),
Entry("nil (from int)", ":123\r\n", nil),
Entry("nil (from inline)", "+foo\r\n", nil),
Entry("nil (from array)", "*1\r\n+foo\r\n", nil),
)
})
})
type customResponse struct {
Host string
Port int
}
func (r *customResponse) AppendTo(w resp.ResponseWriter) {
w.AppendBulkString(fmt.Sprintf("custom '%s:%d'", r.Host, r.Port))
}
type customErrorResponse string
func (r customErrorResponse) AppendTo(w resp.ResponseWriter) {
w.AppendError(r.Error())
}
func (r customErrorResponse) Error() string {
return "WRONG " + string(r)
}