|
1 | 1 | package cmds
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - // "bytes" |
5 |
| - // "fmt" |
| 4 | + "bytes" |
| 5 | + "fmt" |
6 | 6 | "strings"
|
7 |
| - // "testing" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ipfs/go-ipfs-cmds/cmdsutil" |
8 | 10 | )
|
9 | 11 |
|
10 | 12 | type TestOutput struct {
|
11 | 13 | Foo, Bar string
|
12 | 14 | Baz int
|
13 | 15 | }
|
14 | 16 |
|
15 |
| -/* |
16 | 17 | func TestMarshalling(t *testing.T) {
|
17 | 18 | cmd := &Command{}
|
18 | 19 | opts, _ := cmd.GetOptions(nil)
|
19 | 20 |
|
20 | 21 | req, _ := NewRequest(nil, nil, nil, nil, nil, opts)
|
21 | 22 |
|
22 |
| - res := NewResponse(req) |
23 |
| - res.SetOutput(TestOutput{"beep", "boop", 1337}) |
24 |
| -
|
25 |
| - _, err := res.Marshal() |
26 |
| - if err == nil { |
27 |
| - t.Error("Should have failed (no encoding type specified in request)") |
28 |
| - } |
| 23 | + buf := bytes.NewBuffer(nil) |
| 24 | + wc := writecloser{Writer: buf, Closer: nopCloser{}} |
| 25 | + re := NewWriterResponseEmitter(wc, req, Encoders[JSON]) |
29 | 26 |
|
30 |
| - req.SetOption(EncShort, JSON) |
31 |
| -
|
32 |
| - reader, err := res.Marshal() |
| 27 | + err := re.Emit(TestOutput{"beep", "boop", 1337}) |
33 | 28 | if err != nil {
|
34 | 29 | t.Error(err, "Should have passed")
|
35 | 30 | }
|
36 |
| - buf := new(bytes.Buffer) |
37 |
| - buf.ReadFrom(reader) |
| 31 | + |
| 32 | + req.SetOption(cmdsutil.EncShort, JSON) |
| 33 | + |
38 | 34 | output := buf.String()
|
39 | 35 | if removeWhitespace(output) != "{\"Foo\":\"beep\",\"Bar\":\"boop\",\"Baz\":1337}" {
|
| 36 | + t.Log("expected: {\"Foo\":\"beep\",\"Bar\":\"boop\",\"Baz\":1337}") |
| 37 | + t.Log("got:", removeWhitespace(buf.String())) |
40 | 38 | t.Error("Incorrect JSON output")
|
41 | 39 | }
|
42 | 40 |
|
43 |
| - res.SetError(fmt.Errorf("Oops!"), ErrClient) |
44 |
| - reader, err = res.Marshal() |
45 |
| - if err != nil { |
46 |
| - t.Error("Should have passed") |
47 |
| - } |
48 | 41 | buf.Reset()
|
49 |
| - buf.ReadFrom(reader) |
| 42 | + |
| 43 | + re.SetError(fmt.Errorf("Oops!"), cmdsutil.ErrClient) |
| 44 | + |
50 | 45 | output = buf.String()
|
51 |
| - fmt.Println(removeWhitespace(output)) |
52 | 46 | if removeWhitespace(output) != "{\"Message\":\"Oops!\",\"Code\":1}" {
|
| 47 | + t.Log("expected: {\"Message\":\"Oops!\",\"Code\":1}") |
| 48 | + t.Log("got:", removeWhitespace(buf.String())) |
53 | 49 | t.Error("Incorrect JSON output")
|
54 | 50 | }
|
55 | 51 | }
|
56 |
| -*/ |
57 | 52 |
|
58 | 53 | func removeWhitespace(input string) string {
|
59 | 54 | input = strings.Replace(input, " ", "", -1)
|
|
0 commit comments