response_test.go 884 Bytes
Newer Older
1 2
package commands

3
import (
Matt Bell's avatar
Matt Bell committed
4 5
	"fmt"
	"testing"
6
)
7 8

type TestOutput struct {
Matt Bell's avatar
Matt Bell committed
9 10
	Foo, Bar string
	Baz      int
11 12 13
}

func TestMarshalling(t *testing.T) {
Matt Bell's avatar
Matt Bell committed
14
	req := NewRequest()
15

Matt Bell's avatar
Matt Bell committed
16 17 18 19
	res := Response{
		req:   req,
		Value: TestOutput{"beep", "boop", 1337},
	}
20

Matt Bell's avatar
Matt Bell committed
21 22 23 24
	_, err := res.Marshal()
	if err == nil {
		t.Error("Should have failed (no encoding type specified in request)")
	}
25

Matt Bell's avatar
Matt Bell committed
26 27 28 29 30 31 32 33 34
	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")
	}
35

Matt Bell's avatar
Matt Bell committed
36 37 38 39 40 41 42 43 44
	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")
	}
45
}