lbcd/jsonfxns_test.go

57 lines
1.4 KiB
Go
Raw Normal View History

2014-01-09 06:49:06 +01:00
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcjson_test
import (
"bytes"
"fmt"
"testing"
2014-07-03 02:41:28 +02:00
"github.com/btcsuite/btcjson"
)
// TestMarshallAndSend tests the MarshallAndSend function to make sure it can
// create a json message to write to the io.Writerr and to make sure
// it fails properly in cases where it cannot generate json.
func TestMarshallAndSend(t *testing.T) {
jsonError := btcjson.Error{
Code: -32700,
Message: "Parse error",
}
// json.Marshal cannot handle channels so this is a good way to get a
// marshal failure.
badRes := make(chan interface{})
rawReply := btcjson.Reply{
Result: badRes,
Error: &jsonError,
Id: nil,
}
var w bytes.Buffer
msg, err := btcjson.MarshallAndSend(rawReply, &w)
if fmt.Sprintf("%s", err) != "json: unsupported type: chan interface {}" {
t.Error("Should not be able to unmarshall channel")
}
// Use something simple so we can compare the reply.
rawReply = btcjson.Reply{
Result: nil,
Error: nil,
Id: nil,
}
msg, err = btcjson.MarshallAndSend(rawReply, &w)
if msg != "[RPCS] reply: {<nil> <nil> <nil>}" {
t.Error("Incorrect reply:", msg)
}
expBuf := "{\"result\":null,\"error\":null,\"id\":null}\n"
if w.String() != expBuf {
t.Error("Incorrect data in buffer:", w.String())
}
return
}