Repository navigation
Expand file tree
/
Copy pathdecoder_test.go
More file actions
114 lines (104 loc) · 2.53 KB
/
Copy pathdecoder_test.go
File metadata and controls
114 lines (104 loc) · 2.53 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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package redis
import (
"bytes"
"testing"
"github.com/CodisLabs/codis/pkg/utils/assert"
)
func TestBtoi(t *testing.T) {
for i, b := range tmap {
v, err := btoi(b)
assert.MustNoError(err)
assert.Must(v == i)
}
}
func TestDecodeInvalidRequests(t *testing.T) {
test := []string{
"*hello\r\n",
"*-100\r\n",
"*3\r\nhi",
"*3\r\nhi\r\n",
"*4\r\n$1",
"*4\r\n$1\r",
"*4\r\n$1\n",
"*2\r\n$3\r\nget\r\n$what?\r\nx\r\n",
"*4\r\n$3\r\nget\r\n$1\r\nx\r\n",
"*2\r\n$3\r\nget\r\n$1\r\nx",
"*2\r\n$3\r\nget\r\n$1\r\nx\r",
"*2\r\n$3\r\nget\r\n$100\r\nx\r\n",
"$6\r\nfoobar\r",
"$0\rn\r\n",
"$-1\n",
"*0",
"*2n$3\r\nfoo\r\n$3\r\nbar\r\n",
"*-\r\n",
"+OK\n",
"-Error message\r",
}
for _, s := range test {
_, err := DecodeFromBytes([]byte(s))
assert.Must(err != nil)
}
}
func TestDecodeSimpleRequest1(t *testing.T) {
resp, err := DecodeFromBytes([]byte("\r\n"))
assert.MustNoError(err)
assert.Must(resp.IsArray())
assert.Must(len(resp.Array) == 0)
}
func TestDecodeSimpleRequest2(t *testing.T) {
test := []string{
"hello world\r\n",
"hello world \r\n",
" hello world \r\n",
" hello world\r\n",
" hello world \r\n",
}
for _, s := range test {
resp, err := DecodeFromBytes([]byte(s))
assert.MustNoError(err)
assert.Must(resp.IsArray())
assert.Must(len(resp.Array) == 2)
s1 := resp.Array[0]
assert.Must(bytes.Equal(s1.Value, []byte("hello")))
s2 := resp.Array[1]
assert.Must(bytes.Equal(s2.Value, []byte("world")))
}
}
func TestDecodeSimpleRequest3(t *testing.T) {
test := []string{"\r", "\n", " \n"}
for _, s := range test {
_, err := DecodeFromBytes([]byte(s))
assert.Must(err != nil)
}
}
func TestDecodeBulkBytes(t *testing.T) {
test := "*2\r\n$4\r\nLLEN\r\n$6\r\nmylist\r\n"
resp, err := DecodeFromBytes([]byte(test))
assert.MustNoError(err)
assert.Must(len(resp.Array) == 2)
s1 := resp.Array[0]
assert.Must(bytes.Equal(s1.Value, []byte("LLEN")))
s2 := resp.Array[1]
assert.Must(bytes.Equal(s2.Value, []byte("mylist")))
}
func TestDecoder(t *testing.T) {
test := []string{
"$6\r\nfoobar\r\n",
"$0\r\n\r\n",
"$-1\r\n",
"*0\r\n",
"*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",
"*3\r\n:1\r\n:2\r\n:3\r\n",
"*-1\r\n",
"+OK\r\n",
"-Error message\r\n",
"*2\r\n$1\r\n0\r\n*0\r\n",
"*3\r\n$4\r\nEVAL\r\n$31\r\nreturn {1,2,{3,'Hello World!'}}\r\n$1\r\n0\r\n",
}
for _, s := range test {
_, err := DecodeFromBytes([]byte(s))
assert.MustNoError(err)
}
}