forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
58 lines (45 loc) · 1.36 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
package cmds
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/ipfs/go-ipfs-cmdkit"
)
type TestOutput struct {
Foo, Bar string
Baz int
}
func TestMarshalling(t *testing.T) {
cmd := &Command{}
opts, _ := cmd.GetOptions(nil)
req, _ := NewRequest(nil, nil, nil, nil, nil, opts)
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")
}
req.SetOption(cmdkit.EncShort, JSON)
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}" {
t.Log("expected: {\"Message\":\"Oops!\",\"Code\":1}")
t.Log("got:", removeWhitespace(buf.String()))
t.Error("Incorrect JSON output")
}
}
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)
}