OSDN Git Service

Hulk did something
[bytom/vapor.git] / net / http / httpjson / io_test.go
diff --git a/net/http/httpjson/io_test.go b/net/http/httpjson/io_test.go
new file mode 100644 (file)
index 0000000..19ac638
--- /dev/null
@@ -0,0 +1,37 @@
+package httpjson
+
+import (
+       "context"
+       "net/http/httptest"
+       "strings"
+       "testing"
+)
+
+func TestWriteArray(t *testing.T) {
+       examples := []struct {
+               in   []int
+               want string
+       }{
+               {nil, "[]"},
+               {[]int{}, "[]"},
+               {make([]int, 0), "[]"},
+       }
+
+       for _, ex := range examples {
+               rec := httptest.NewRecorder()
+               Write(context.Background(), rec, 200, ex.in)
+               got := strings.TrimSpace(rec.Body.String())
+               if got != ex.want {
+                       t.Errorf("Write(%v) = %v want %v", ex.in, got, ex.want)
+               }
+       }
+}
+
+type errResponse struct {
+       *httptest.ResponseRecorder
+       err error
+}
+
+func (r *errResponse) Write([]byte) (int, error) {
+       return 0, r.err
+}