From 81f36df910533de63dc5ac66f38b5481961cc0c8 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 26 Mar 2015 20:31:00 +0100 Subject: [PATCH] CompileArgs --- rpc/args.go | 12 ++++++++---- rpc/args_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/rpc/args.go b/rpc/args.go index c11ffa3cce..0169ece583 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -617,14 +617,18 @@ type CompileArgs struct { func (args *CompileArgs) UnmarshalJSON(b []byte) (err error) { var obj []interface{} - r := bytes.NewReader(b) - if err := json.NewDecoder(r).Decode(&obj); err != nil { + if err := json.Unmarshal(b, &obj); err != nil { return NewDecodeParamError(err.Error()) } - if len(obj) > 0 { - args.Source = obj[0].(string) + if len(obj) < 1 { + return NewInsufficientParamsError(len(obj), 1) + } + argstr, ok := obj[0].(string) + if !ok { + return NewInvalidTypeError("arg0", "is not a string") } + args.Source = argstr return nil } diff --git a/rpc/args_test.go b/rpc/args_test.go index 0b243e7603..7cb63b67e5 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -1126,6 +1126,36 @@ func TestCompileArgs(t *testing.T) { } } +func TestCompileArgsInvalid(t *testing.T) { + input := `{}` + + args := new(CompileArgs) + str := ExpectDecodeParamError(json.Unmarshal([]byte(input), args)) + if len(str) > 0 { + t.Error(str) + } +} + +func TestCompileArgsEmpty(t *testing.T) { + input := `[]` + + args := new(CompileArgs) + str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args)) + if len(str) > 0 { + t.Error(str) + } +} + +func TestCompileArgsBool(t *testing.T) { + input := `[false]` + + args := new(CompileArgs) + str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args)) + if len(str) > 0 { + t.Error(str) + } +} + func TestFilterStringArgs(t *testing.T) { input := `["pending"]` expected := new(FilterStringArgs)