Repository: zeromicro/go-zero
Pull request #1856 (2022-05-02 12:55:58)

test: add more tests


Patch:
diff --git a/zrpc/resolver/internal/kubebuilder.go b/zrpc/resolver/internal/kubebuilder.go
index 427cef12..1a78eda5 100644
--- a/zrpc/resolver/internal/kubebuilder.go
+++ b/zrpc/resolver/internal/kubebuilder.go
@@ -24,7 +24,7 @@ const (
 type kubeBuilder struct{}
 
 func (b *kubeBuilder) Build(target resolver.Target, cc resolver.ClientConn,
-	opts resolver.BuildOptions) (resolver.Resolver, error) {
+	_ resolver.BuildOptions) (resolver.Resolver, error) {
 	svc, err := kube.ParseTarget(target)
 	if err != nil {
 		return nil, err


Test patch:
diff --git a/core/fs/temps_test.go b/core/fs/temps_test.go
new file mode 100644
index 00000000..0cf1fc04
--- /dev/null
+++ b/core/fs/temps_test.go
@@ -0,0 +1,49 @@
+package fs
+
+import (
+	"io/ioutil"
+	"os"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestTempFileWithText(t *testing.T) {
+	f, err := TempFileWithText("test")
+	if err != nil {
+		t.Error(err)
+	}
+	if f == nil {
+		t.Error("TempFileWithText returned nil")
+	}
+	if f.Name() == "" {
+		t.Error("TempFileWithText returned empty file name")
+	}
+	defer os.Remove(f.Name())
+
+	bs, err := ioutil.ReadAll(f)
+	assert.Nil(t, err)
+	if len(bs) != 4 {
+		t.Error("TempFileWithText returned wrong file size")
+	}
+	if f.Close() != nil {
+		t.Error("TempFileWithText returned error on close")
+	}
+}
+
+func TestTempFilenameWithText(t *testing.T) {
+	f, err := TempFilenameWithText("test")
+	if err != nil {
+		t.Error(err)
+	}
+	if f == "" {
+		t.Error("TempFilenameWithText returned empty file name")
+	}
+	defer os.Remove(f)
+
+	bs, err := ioutil.ReadFile(f)
+	assert.Nil(t, err)
+	if len(bs) != 4 {
+		t.Error("TempFilenameWithText returned wrong file size")
+	}
+}
diff --git a/core/jsonx/json_test.go b/core/jsonx/json_test.go
new file mode 100644
index 00000000..48221e36
--- /dev/null
+++ b/core/jsonx/json_test.go
@@ -0,0 +1,87 @@
+package jsonx
+
+import (
+	"strings"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestMarshal(t *testing.T) {
+	var v = struct {
+		Name string `json:"name"`
+		Age  int    `json:"age"`
+	}{
+		Name: "John",
+		Age:  30,
+	}
+	bs, err := Marshal(v)
+	assert.Nil(t, err)
+	assert.Equal(t, `{"name":"John","age":30}`, string(bs))
+}
+
+func TestUnmarshal(t *testing.T) {
+	const s = `{"name":"John","age":30}`
+	var v struct {
+		Name string `json:"name"`
+		Age  int    `json:"age"`
+	}
+	err := Unmarshal([]byte(s), &v)
+	assert.Nil(t, err)
+	assert.Equal(t, "John", v.Name)
+	assert.Equal(t, 30, v.Age)
+}
+
+func TestUnmarshalError(t *testing.T) {
+	const s = `{"name":"John","age":30`
+	var v struct {
+		Name string `json:"name"`
+		Age  int    `json:"age"`
+	}
+	err := Unmarshal([]byte(s), &v)
+	assert.NotNil(t, err)
+}
+
+func TestUnmarshalFromString(t *testing.T) {
+	const s = `{"name":"John","age":30}`
+	var v struct {
+		Name string `json:"name"`
+		Age  int    `json:"age"`
+	}
+	err := UnmarshalFromString(s, &v)
+	assert.Nil(t, err)
+	assert.Equal(t, "John", v.Name)
+	assert.Equal(t, 30, v.Age)
+}
+
+func TestUnmarshalFromStringError(t *testing.T) {
+	const s = `{"name":"John","age":30`
+	var v struct {
+		Name string `json:"name"`
+		Age  int    `json:"age"`
+	}
+	err := UnmarshalFromString(s, &v)
+	assert.NotNil(t, err)
+}
+
+func TestUnmarshalFromRead(t *testing.T) {
+	const s = `{"name":"John","age":30}`
+	var v struct {
+		Name string `json:"name"`
+		Age  int    `json:"age"`
+	}
+	err := UnmarshalFromReader(strings.NewReader(s), &v)
+	assert.Nil(t, err)
+	assert.Equal(t, "John", v.Name)
+	assert.Equal(t, 30, v.Age)
+}
+
+func TestUnmarshalFromReaderError(t *testing.T) {
+	const s = `{"name":"John","age":30`
+	var v struct {
+		Name string `json:"name"`
+		Age  int    `json:"age"`
+	}
+	err := UnmarshalFromReader(strings.NewReader(s), &v)
+	assert.NotNil(t, err)
+}
diff --git a/rest/httpc/requests_test.go b/rest/httpc/requests_test.go
index 551f058c..c6160c48 100644
--- a/rest/httpc/requests_test.go
+++ b/rest/httpc/requests_test.go
@@ -158,3 +158,32 @@ func TestDo_BadRequest(t *testing.T) {
 	_, err = Do(context.Background(), http.MethodPost, "/nodes/:val", val5)
 	assert.NotNil(t, err)
 }
+
+func TestDo_Json(t *testing.T) {
+	type Data struct {
+		Key    string   `path:"key"`
+		Value  int      `form:"value"`
+		Header string   `header:"X-Header"`
+		Body   chan int `json:"body"`
+	}
+
+	rt := router.NewRouter()
+	err := rt.Handle(http.MethodPost, "/nodes/:key",
+		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+			var req Data
+			assert.Nil(t, httpx.Parse(r, &req))
+		}))
+	assert.Nil(t, err)
+
+	svr := httptest.NewServer(http.HandlerFunc(rt.ServeHTTP))
+	defer svr.Close()
+
+	data := Data{
+		Key:    "foo",
+		Value:  10,
+		Header: "my-header",
+		Body:   make(chan int),
+	}
+	_, err = Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
+	assert.NotNil(t, err)
+}
diff --git a/rest/httpx/responses_test.go b/rest/httpx/responses_test.go
index 9a553cd0..2be5a7f6 100644
--- a/rest/httpx/responses_test.go
+++ b/rest/httpx/responses_test.go
@@ -129,7 +129,18 @@ func TestWriteJsonTimeout(t *testing.T) {
 	// only log it and ignore
 	w := tracedResponseWriter{
 		headers: make(map[string][]string),
-		timeout: true,
+		err:     http.ErrHandlerTimeout,
+	}
+	msg := message{Name: "anyone"}
+	WriteJson(&w, http.StatusOK, msg)
+	assert.Equal(t, http.StatusOK, w.code)
+}
+
+func TestWriteJsonError(t *testing.T) {
+	// only log it and ignore
+	w := tracedResponseWriter{
+		headers: make(map[string][]string),
+		err:     errors.New("foo"),
 	}
 	msg := message{Name: "anyone"}
 	WriteJson(&w, http.StatusOK, msg)
@@ -162,8 +173,8 @@ type tracedResponseWriter struct {
 	hasBody     bool
 	code        int
 	lessWritten bool
-	timeout     bool
 	wroteHeader bool
+	err         error
 }
 
 func (w *tracedResponseWriter) Header() http.Header {
@@ -171,8 +182,8 @@ func (w *tracedResponseWriter) Header() http.Header {
 }
 
 func (w *tracedResponseWriter) Write(bytes []byte) (n int, err error) {
-	if w.timeout {
-		return 0, http.ErrHandlerTimeout
+	if w.err != nil {
+		return 0, w.err
 	}
 
 	n, err = w.builder.Write(bytes)
diff --git a/zrpc/resolver/register_test.go b/zrpc/resolver/register_test.go
new file mode 100644
index 00000000..91ba96cb
--- /dev/null
+++ b/zrpc/resolver/register_test.go
@@ -0,0 +1,13 @@
+package resolver
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestRegister(t *testing.T) {
+	assert.NotPanics(t, func() {
+		Register()
+	})
+}