forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
169 lines (137 loc) · 3.29 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
package cmds
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"testing"
"github.com/ipfs/go-ipfs-cmdkit"
)
type TestOutput struct {
Foo, Bar string
Baz int
}
func eqStringSlice(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestMarshalling(t *testing.T) {
cmd := &Command{}
req, err := NewRequest(context.Background(), nil, map[string]interface{}{
EncLong: JSON,
}, nil, nil, cmd)
if err != nil {
t.Error(err, "Should have passed")
}
buf := bytes.NewBuffer(nil)
wc := writecloser{Writer: buf, Closer: nopCloser{}}
re := NewWriterResponseEmitter(wc, req, Encoders[JSON])
err = re.Emit(TestOutput{"beep", "boop", 1337})
if err != nil {
t.Error(err, "Should have passed")
}
output := buf.String()
if removeWhitespace(output) != "{\"Foo\":\"beep\",\"Bar\":\"boop\",\"Baz\":1337}" {
t.Log("expected: {\"Foo\":\"beep\",\"Bar\":\"boop\",\"Baz\":1337}")
t.Log("got:", removeWhitespace(buf.String()))
t.Error("Incorrect JSON output")
}
buf.Reset()
re.SetError(fmt.Errorf("Oops!"), cmdkit.ErrClient)
output = buf.String()
if removeWhitespace(output) != `{"Message":"Oops!","Code":1,"Type":"error"}` {
t.Log(`expected: {"Message":"Oops!","Code":1,"Type":"error"}`)
t.Log("got:", removeWhitespace(buf.String()))
t.Error("Incorrect JSON output")
}
}
func TestHandleError_Error(t *testing.T) {
var (
out []string
exp = []string{"1", "2", "received command error"}
)
cmd := &Command{}
req, err := NewRequest(context.Background(), nil, nil, nil, nil, cmd)
if err != nil {
t.Error(err, "Should have passed")
}
re, res := NewChanResponsePair(req)
reFwd, resFwd := NewChanResponsePair(req)
go func() {
re.Emit(1)
re.Emit(2)
re.Emit(&cmdkit.Error{Message: "test errors", Code: cmdkit.ErrNormal})
re.Close()
}()
go func() {
for v, err := resFwd.Next(); err != io.EOF; {
t.Logf("received forwarded value %#v, error %#v", v, err)
}
}()
for {
v, err := res.Next()
if err == nil {
t.Log("err == nil")
out = append(out, fmt.Sprint(v))
} else {
t.Log("err != nil")
out = append(out, fmt.Sprint(err))
}
if !HandleError(err, res, reFwd) {
break
}
}
if !eqStringSlice(out, exp) {
t.Fatalf("expected %v, got %v", exp, out)
}
}
func TestHandleError(t *testing.T) {
var (
out []string
exp = []string{"1", "2", "3", "EOF"}
)
cmd := &Command{}
req, err := NewRequest(context.Background(), nil, nil, nil, nil, cmd)
if err != nil {
t.Error(err, "Should have passed")
}
re, res := NewChanResponsePair(req)
reFwd, resFwd := NewChanResponsePair(req)
go func() {
re.Emit(1)
re.Emit(2)
re.Emit(3)
re.Close()
}()
go func() {
for v, err := resFwd.Next(); err != io.EOF; {
t.Logf("received forwarded value %#v, error %#v", v, err)
}
}()
for HandleError(err, res, reFwd) {
var v interface{}
v, err = res.Next()
if v != nil {
out = append(out, fmt.Sprint(v))
} else {
out = append(out, fmt.Sprint(err))
}
}
if !eqStringSlice(out, exp) {
t.Fatalf("expected %v, got %v", exp, out)
}
}
func removeWhitespace(input string) string {
input = strings.Replace(input, " ", "", -1)
input = strings.Replace(input, "\t", "", -1)
input = strings.Replace(input, "\n", "", -1)
return strings.Replace(input, "\r", "", -1)
}