diff --git a/jsonfxns.go b/jsonfxns.go index 2d336259..555f612c 100644 --- a/jsonfxns.go +++ b/jsonfxns.go @@ -14,9 +14,9 @@ import ( ) // MarshallAndSend takes the reply structure, marshalls it to json, and -// sends it back to the http writer, returning a log message and an error if -// there is one. -func MarshallAndSend(rawReply Reply, w http.ResponseWriter) (string, error) { +// sends it back to the io.Writer (most likely an http.ResponseWriter). +// returning a log message and an error if there is one. +func MarshallAndSend(rawReply Reply, w io.Writer) (string, error) { finalReply, err := json.Marshal(rawReply) if err != nil { msg := fmt.Sprintf("[RPCS] Error Marshalling reply: %v", err) diff --git a/jsonfxns_test.go b/jsonfxns_test.go new file mode 100644 index 00000000..5f1b316a --- /dev/null +++ b/jsonfxns_test.go @@ -0,0 +1,55 @@ +// Copyright (c) 2013 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" + "github.com/conformal/btcjson" + "testing" +) + +// 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: { }" { + 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 +} diff --git a/test_coverage.txt b/test_coverage.txt index 382d31d6..bdbebbc3 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,10 +1,10 @@ -github.com/conformal/btcjson/jsonapi.go JsonCreateMessage 100.00% (310/310) -github.com/conformal/btcjson/jsonfxns.go JsonGetRaw 100.00% (6/6) -github.com/conformal/btcjson/jsonapi.go jsonWithArgs 100.00% (5/5) -github.com/conformal/btcjson/jsonfxns.go jsonRpcSend 100.00% (4/4) -github.com/conformal/btcjson/jsonapi.go JsonRpcCommand 66.67% (18/27) -github.com/conformal/btcjson/jsonapi.go readResultCmd 40.00% (20/50) -github.com/conformal/btcjson/jsonfxns.go JsonMarshallAndSend 0.00% (0/7) -github.com/conformal/btcjson ------------------- 88.75% (363/409) +github.com/conformal/btcjson/jsonapi.go CreateMessage 100.00% (310/310) +github.com/conformal/btcjson/jsonfxns.go MarshallAndSend 100.00% (7/7) +github.com/conformal/btcjson/jsonfxns.go GetRaw 100.00% (6/6) +github.com/conformal/btcjson/jsonapi.go jsonWithArgs 100.00% (5/5) +github.com/conformal/btcjson/jsonfxns.go jsonRpcSend 100.00% (4/4) +github.com/conformal/btcjson/jsonapi.go RpcCommand 66.67% (18/27) +github.com/conformal/btcjson/jsonapi.go readResultCmd 40.00% (20/50) +github.com/conformal/btcjson --------------- 90.46% (370/409)