test: add more tests (#1856)

master
Kevin Wan 3 years ago committed by GitHub
parent d0a58d1f2d
commit f21970c117
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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")
}
}

@ -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)
}

@ -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)
}

@ -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)

@ -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

@ -0,0 +1,13 @@
package resolver
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRegister(t *testing.T) {
assert.NotPanics(t, func() {
Register()
})
}
Loading…
Cancel
Save