From 06eacf70a298ca2d98357f2e1cfc7bc07eb4af99 Mon Sep 17 00:00:00 2001 From: ycdesu Date: Fri, 5 Feb 2021 22:38:45 +0800 Subject: [PATCH] util: test Response struct --- pkg/util/http_response_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 pkg/util/http_response_test.go diff --git a/pkg/util/http_response_test.go b/pkg/util/http_response_test.go new file mode 100644 index 000000000..13fbe7897 --- /dev/null +++ b/pkg/util/http_response_test.go @@ -0,0 +1,28 @@ +package util + +import ( + "bytes" + "io/ioutil" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestResponse_DecodeJSON(t *testing.T) { + type temp struct { + Name string `json:"name"` + } + json := `{"name":"Test Name","a":"a"}` + reader := ioutil.NopCloser(bytes.NewReader([]byte(json))) + resp, err := NewResponse(&http.Response{ + StatusCode: 200, + Body: reader, + }) + assert.NoError(t, err) + assert.Equal(t, json, resp.String()) + + var result temp + assert.NoError(t, resp.DecodeJSON(&result)) + assert.Equal(t, "Test Name", result.Name) +}