Browse Source

Added TestJSONDecoder

Piotr Czajkowski 4 years ago
parent
commit
01244855f5
1 changed files with 31 additions and 0 deletions
  1. 31 0
      rest_test.go

+ 31 - 0
rest_test.go

@@ -0,0 +1,31 @@
+package rest
+
+import (
+	"io/ioutil"
+	"strings"
+	"testing"
+)
+
+type Something struct {
+	First  int
+	Second string
+}
+
+func TestJSONDecoder(t *testing.T) {
+	const json = `{ "First": 15, "Second": "Some string" }`
+	reader := strings.NewReader(json)
+	readcloser := ioutil.NopCloser(reader)
+	defer readcloser.Close()
+
+	expected := Something{First: 15, Second: "Some string"}
+
+	var result Something
+	err := JSONDecoder(readcloser, &result)
+	if err != nil {
+		t.Error(err)
+	}
+
+	if expected.First != result.First || expected.Second != result.Second {
+		t.Errorf("Wrong result: %v", result)
+	}
+}