btcd/btcjson/jsonfxns_test.go
Dave Collins 6b3c3c7498 Import btcjson repo into btcjson directory.
This commit contains the entire btcjson repository along with several
changes needed to move all of the files into the btcjson directory in
order to prepare it for merging.  This does NOT update btcd or any of the
other packages to use the new location as that will be done separately.

- All import paths in the old btcjson test files have been changed to the
  new location
- The coveralls badge has been removed since it unfortunately doesn't
  support coverage of sub-packages

This is ongoing work toward #214.
2015-02-19 11:10:46 -06:00

57 lines
1.4 KiB
Go

// 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"
"github.com/btcsuite/btcd/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
}