mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-14 18:47:01 +02:00
Added jsonutil functional tests
This commit is contained in:
parent
2dc811d1b3
commit
c62a368ccd
@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Encodes/Marshals the given object into JSON
|
||||
@ -39,12 +38,6 @@ func DecodeJSONFromReader(r io.Reader, out interface{}) error {
|
||||
return fmt.Errorf("output parameter 'out' is nil")
|
||||
}
|
||||
|
||||
// Decoding requires a pointer type to be supplied
|
||||
value := reflect.ValueOf(out)
|
||||
if value.Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("decoding the value into a non-pointer type: %v", reflect.TypeOf(out))
|
||||
}
|
||||
|
||||
dec := json.NewDecoder(r)
|
||||
|
||||
// While decoding JSON values, intepret the integer values as `json.Number`s instead of `float64`.
|
||||
|
66
helper/jsonutil/json_test.go
Normal file
66
helper/jsonutil/json_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
package jsonutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_EncodeJSON(t *testing.T) {
|
||||
input := map[string]interface{}{
|
||||
"test": "data",
|
||||
"validation": "process",
|
||||
}
|
||||
|
||||
actualBytes, err := EncodeJSON(input)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to encode JSON: %v", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(string(actualBytes))
|
||||
expected := `{"test":"data","validation":"process"}`
|
||||
|
||||
if actual != expected {
|
||||
t.Fatal("bad: encoded JSON: expected:%s\nactual:%s\n", expected, string(actualBytes))
|
||||
}
|
||||
}
|
||||
|
||||
func Test_DecodeJSON(t *testing.T) {
|
||||
input := `{"test":"data","validation":"process"}`
|
||||
|
||||
var actual map[string]interface{}
|
||||
|
||||
err := DecodeJSON([]byte(input), &actual)
|
||||
if err != nil {
|
||||
fmt.Printf("decoding err: %v\n", err)
|
||||
}
|
||||
|
||||
expected := map[string]interface{}{
|
||||
"test": "data",
|
||||
"validation": "process",
|
||||
}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatal("bad: expected:%#v\nactual:%#v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_DecodeJSONFromReader(t *testing.T) {
|
||||
input := `{"test":"data","validation":"process"}`
|
||||
|
||||
var actual map[string]interface{}
|
||||
|
||||
err := DecodeJSONFromReader(bytes.NewReader([]byte(input)), &actual)
|
||||
if err != nil {
|
||||
fmt.Printf("decoding err: %v\n", err)
|
||||
}
|
||||
|
||||
expected := map[string]interface{}{
|
||||
"test": "data",
|
||||
"validation": "process",
|
||||
}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatal("bad: expected:%#v\nactual:%#v", expected, actual)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user