Browse Source

Added TestJSONDecoderBadJSON

Piotr Czajkowski 4 years ago
parent
commit
e30c0def30
1 changed files with 19 additions and 0 deletions
  1. 19 0
      rest_test.go

+ 19 - 0
rest_test.go

@@ -29,3 +29,22 @@ func TestJSONDecoder(t *testing.T) {
 		t.Errorf("Wrong result: %v", result)
 	}
 }
+
+func TestJSONDecoderBadJSON(t *testing.T) {
+	const badJSON = `{ First: 15, "Second": "Some string" }`
+	reader := strings.NewReader(badJSON)
+	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("There should be an error")
+	}
+
+	if expected.First == result.First || expected.Second == result.Second {
+		t.Errorf("There should be an error on decoding, %v", result)
+	}
+}