response_test.go 623 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package commands

import "testing"

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)")
  }

23
  req.SetOption(globalOptions[0], Json)
24 25 26 27 28 29 30 31 32
  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")
  }
}