Add tests for MarshallAndSend function.

This commit is contained in:
John C. Vernaleo 2013-05-14 10:13:44 -04:00
parent 648e6317bb
commit 2fc8982be5
3 changed files with 66 additions and 11 deletions

View file

@ -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)

55
jsonfxns_test.go Normal file
View file

@ -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: {<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
}

View file

@ -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)