forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
45 lines (38 loc) · 917 Bytes
/
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
package commands
import (
"testing"
"fmt"
)
type TestOutput struct {
Foo, Bar string
Baz int
}
func TestMarshalling(t *testing.T) {
req := NewRequest()
res := Response{
req: req,
Value: TestOutput{ "beep", "boop", 1337 },
}
_, err := res.Marshal()
if err == nil {
t.Error("Should have failed (no encoding type specified in request)")
}
req.SetOption(globalOptions[0], Json)
bytes, err := res.Marshal()
if err != nil {
t.Error("Should have passed")
}
output := string(bytes)
if output != "{\"Foo\":\"beep\",\"Bar\":\"boop\",\"Baz\":1337}" {
t.Error("Incorrect JSON output")
}
res.SetError(fmt.Errorf("You broke something!"), Client)
bytes, err = res.Marshal()
if err != nil {
t.Error("Should have passed")
}
output = string(bytes)
if output != "{\"Message\":\"You broke something!\",\"Code\":1}" {
t.Error("Incorrect JSON output")
}
}