Fix tests #4
13
.drone.yml
13
.drone.yml
|
@ -1,17 +1,4 @@
|
|||
kind: pipeline
|
||||
name: go1-1-1
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang:1.11
|
||||
environment:
|
||||
GOPROXY: https://goproxy.cn
|
||||
commands:
|
||||
- go build -v
|
||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: go1-1-2
|
||||
|
||||
steps:
|
||||
|
|
42
bind_test.go
42
bind_test.go
|
@ -17,34 +17,30 @@ package binding
|
|||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func Test_Bind(t *testing.T) {
|
||||
Convey("Bind test", t, func() {
|
||||
Convey("Bind form", func() {
|
||||
for _, testCase := range formTestCases {
|
||||
performFormTest(t, Bind, testCase)
|
||||
}
|
||||
})
|
||||
t.Run("Bind form", func(t *testing.T) {
|
||||
for _, testCase := range formTestCases {
|
||||
performFormTest(t, Bind, testCase)
|
||||
}
|
||||
})
|
||||
|
||||
Convey("Bind JSON", func() {
|
||||
for _, testCase := range jsonTestCases {
|
||||
performJsonTest(t, Bind, testCase)
|
||||
}
|
||||
})
|
||||
t.Run("Bind JSON", func(t *testing.T) {
|
||||
for _, testCase := range jsonTestCases {
|
||||
performJsonTest(t, Bind, testCase)
|
||||
}
|
||||
})
|
||||
|
||||
Convey("Bind multipart form", func() {
|
||||
for _, testCase := range multipartFormTestCases {
|
||||
performMultipartFormTest(t, Bind, testCase)
|
||||
}
|
||||
})
|
||||
t.Run("Bind multipart form", func(t *testing.T) {
|
||||
for _, testCase := range multipartFormTestCases {
|
||||
performMultipartFormTest(t, Bind, testCase)
|
||||
}
|
||||
})
|
||||
|
||||
Convey("Bind with file", func() {
|
||||
for _, testCase := range fileTestCases {
|
||||
performFileTest(t, Bind, testCase)
|
||||
}
|
||||
})
|
||||
t.Run("Bind with file", func(t *testing.T) {
|
||||
for _, testCase := range fileTestCases {
|
||||
performFileTest(t, Bind, testCase)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var errorTestCases = []errorTestCase{
|
||||
|
@ -127,11 +127,9 @@ var errorTestCases = []errorTestCase{
|
|||
}
|
||||
|
||||
func Test_ErrorHandler(t *testing.T) {
|
||||
Convey("Error handler", t, func() {
|
||||
for _, testCase := range errorTestCases {
|
||||
performErrorTest(t, testCase)
|
||||
}
|
||||
})
|
||||
for _, testCase := range errorTestCases {
|
||||
performErrorTest(t, testCase)
|
||||
}
|
||||
}
|
||||
|
||||
func performErrorTest(t *testing.T, testCase errorTestCase) {
|
||||
|
@ -139,12 +137,12 @@ func performErrorTest(t *testing.T, testCase errorTestCase) {
|
|||
|
||||
errorHandler(testCase.errors, resp)
|
||||
|
||||
So(resp.Code, ShouldEqual, testCase.expected.statusCode)
|
||||
So(resp.Header().Get("Content-Type"), ShouldEqual, testCase.expected.contentType)
|
||||
assert.EqualValues(t, resp.Code, testCase.expected.statusCode)
|
||||
assert.EqualValues(t, resp.Header().Get("Content-Type"), testCase.expected.contentType)
|
||||
|
||||
actualBody, err := ioutil.ReadAll(resp.Body)
|
||||
So(err, ShouldBeNil)
|
||||
So(string(actualBody), ShouldEqual, testCase.expected.body)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, string(actualBody), testCase.expected.body)
|
||||
}
|
||||
|
||||
type (
|
||||
|
|
|
@ -19,57 +19,51 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_ErrorsAdd(t *testing.T) {
|
||||
Convey("Add new error", t, func() {
|
||||
var actual Errors
|
||||
expected := Errors{
|
||||
Error{
|
||||
FieldNames: []string{"Field1", "Field2"},
|
||||
Classification: "ErrorClass",
|
||||
Message: "Some message",
|
||||
},
|
||||
}
|
||||
var actual Errors
|
||||
expected := Errors{
|
||||
Error{
|
||||
FieldNames: []string{"Field1", "Field2"},
|
||||
Classification: "ErrorClass",
|
||||
Message: "Some message",
|
||||
},
|
||||
}
|
||||
|
||||
actual.Add(expected[0].FieldNames, expected[0].Classification, expected[0].Message)
|
||||
actual.Add(expected[0].FieldNames, expected[0].Classification, expected[0].Message)
|
||||
|
||||
So(len(actual), ShouldEqual, 1)
|
||||
So(fmt.Sprintf("%#v", actual), ShouldEqual, fmt.Sprintf("%#v", expected))
|
||||
})
|
||||
assert.EqualValues(t, len(actual), 1)
|
||||
assert.EqualValues(t, fmt.Sprintf("%#v", actual), fmt.Sprintf("%#v", expected))
|
||||
}
|
||||
|
||||
func Test_ErrorsLen(t *testing.T) {
|
||||
Convey("Get number of errors", t, func() {
|
||||
So(errorsTestSet.Len(), ShouldEqual, len(errorsTestSet))
|
||||
})
|
||||
assert.EqualValues(t, errorsTestSet.Len(), len(errorsTestSet))
|
||||
}
|
||||
|
||||
func Test_ErrorsHas(t *testing.T) {
|
||||
Convey("Check error class", t, func() {
|
||||
So(errorsTestSet.Has("ClassA"), ShouldBeTrue)
|
||||
So(errorsTestSet.Has("ClassQ"), ShouldBeFalse)
|
||||
})
|
||||
assert.True(t, errorsTestSet.Has("ClassA"))
|
||||
assert.False(t, errorsTestSet.Has("ClassQ"))
|
||||
}
|
||||
|
||||
func Test_ErrorGetters(t *testing.T) {
|
||||
Convey("Get error detail", t, func() {
|
||||
err := Error{
|
||||
FieldNames: []string{"field1", "field2"},
|
||||
Classification: "ErrorClass",
|
||||
Message: "The message",
|
||||
}
|
||||
|
||||
fieldsActual := err.Fields()
|
||||
err := Error{
|
||||
FieldNames: []string{"field1", "field2"},
|
||||
Classification: "ErrorClass",
|
||||
Message: "The message",
|
||||
}
|
||||
|
||||
So(len(fieldsActual), ShouldEqual, 2)
|
||||
So(fieldsActual[0], ShouldEqual, "field1")
|
||||
So(fieldsActual[1], ShouldEqual, "field2")
|
||||
fieldsActual := err.Fields()
|
||||
|
||||
assert.EqualValues(t, len(fieldsActual), 2)
|
||||
assert.EqualValues(t, fieldsActual[0], "field1")
|
||||
assert.EqualValues(t, fieldsActual[1], "field2")
|
||||
|
||||
assert.EqualValues(t, err.Kind(), "ErrorClass")
|
||||
assert.EqualValues(t, err.Error(), "The message")
|
||||
|
||||
So(err.Kind(), ShouldEqual, "ErrorClass")
|
||||
So(err.Error(), ShouldEqual, "The message")
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
20
file_test.go
20
file_test.go
|
@ -23,7 +23,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var fileTestCases = []fileTestCase{
|
||||
|
@ -67,11 +67,9 @@ var fileTestCases = []fileTestCase{
|
|||
}
|
||||
|
||||
func Test_FileUploads(t *testing.T) {
|
||||
Convey("Test file upload", t, func() {
|
||||
for _, testCase := range fileTestCases {
|
||||
performFileTest(t, MultipartForm, testCase)
|
||||
}
|
||||
})
|
||||
for _, testCase := range fileTestCases {
|
||||
performFileTest(t, MultipartForm, testCase)
|
||||
}
|
||||
}
|
||||
|
||||
func performFileTest(t *testing.T, binder handlerFunc, testCase fileTestCase) {
|
||||
|
@ -80,7 +78,7 @@ func performFileTest(t *testing.T, binder handlerFunc, testCase fileTestCase) {
|
|||
|
||||
fileTestHandler := func(actual BlogPost, errs Errors) {
|
||||
assertFileAsExpected(t, testCase, actual.HeaderImage, testCase.singleFile)
|
||||
So(len(testCase.multipleFiles), ShouldEqual, len(actual.Pictures))
|
||||
assert.EqualValues(t, len(testCase.multipleFiles), len(actual.Pictures))
|
||||
|
||||
for i, expectedFile := range testCase.multipleFiles {
|
||||
if i >= len(actual.Pictures) {
|
||||
|
@ -112,15 +110,15 @@ func assertFileAsExpected(t *testing.T, testCase fileTestCase, actual *multipart
|
|||
}
|
||||
|
||||
if expected != nil && actual == nil {
|
||||
So(actual, ShouldNotBeNil)
|
||||
assert.NotNil(t, actual)
|
||||
return
|
||||
} else if expected == nil && actual != nil {
|
||||
So(actual, ShouldBeNil)
|
||||
assert.Nil(t, actual)
|
||||
return
|
||||
}
|
||||
|
||||
So(actual.Filename, ShouldEqual, expected.fileName)
|
||||
So(unpackFileHeaderData(actual), ShouldEqual, expected.data)
|
||||
assert.EqualValues(t, actual.Filename, expected.fileName)
|
||||
assert.EqualValues(t, unpackFileHeaderData(actual), expected.data)
|
||||
}
|
||||
|
||||
func buildRequestWithFile(testCase fileTestCase) *http.Request {
|
||||
|
|
46
form_test.go
46
form_test.go
|
@ -24,7 +24,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var formTestCases = []formTestCase{
|
||||
|
@ -164,13 +164,11 @@ func init() {
|
|||
}
|
||||
|
||||
func Test_Form(t *testing.T) {
|
||||
Convey("Test form", t, func() {
|
||||
for _, testCase := range formTestCases {
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
performFormTest(t, Form, testCase)
|
||||
})
|
||||
}
|
||||
})
|
||||
for _, testCase := range formTestCases {
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
performFormTest(t, Form, testCase)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func performFormTest(t *testing.T, binder handlerFunc, testCase formTestCase) {
|
||||
|
@ -179,14 +177,14 @@ func performFormTest(t *testing.T, binder handlerFunc, testCase formTestCase) {
|
|||
|
||||
formTestHandler := func(actual interface{}, errs Errors) {
|
||||
if testCase.shouldSucceed && len(errs) > 0 {
|
||||
So(len(errs), ShouldEqual, 0)
|
||||
assert.EqualValues(t, 0, len(errs))
|
||||
} else if !testCase.shouldSucceed && len(errs) == 0 {
|
||||
So(len(errs), ShouldNotEqual, 0)
|
||||
assert.NotEqual(t, 0, len(errs))
|
||||
}
|
||||
expString := fmt.Sprintf("%+v", testCase.expected)
|
||||
actString := fmt.Sprintf("%+v", actual)
|
||||
if actString != expString && !(testCase.deepEqual && reflect.DeepEqual(testCase.expected, actual)) {
|
||||
So(actString, ShouldEqual, expString)
|
||||
assert.EqualValues(t, actString, expString)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -197,7 +195,7 @@ func performFormTest(t *testing.T, binder handlerFunc, testCase formTestCase) {
|
|||
var actual Post
|
||||
errs := binder(req, &actual)
|
||||
p := testCase.expected.(Post)
|
||||
So(actual.Title, ShouldEqual, p.Title)
|
||||
assert.EqualValues(t, actual.Title, p.Title)
|
||||
formTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
|
@ -219,7 +217,7 @@ func performFormTest(t *testing.T, binder handlerFunc, testCase formTestCase) {
|
|||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
p := testCase.expected.(BlogPost)
|
||||
So(actual.Title, ShouldEqual, p.Title)
|
||||
assert.EqualValues(t, actual.Title, p.Title)
|
||||
formTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
|
@ -288,17 +286,15 @@ type defaultForm struct {
|
|||
}
|
||||
|
||||
func Test_Default(t *testing.T) {
|
||||
Convey("Test default value", t, func() {
|
||||
m := chi.NewRouter()
|
||||
m.Get("/", func(resp http.ResponseWriter, req *http.Request) {
|
||||
var f defaultForm
|
||||
Bind(req, &f)
|
||||
So(f.Default, ShouldEqual, "hello world")
|
||||
})
|
||||
resp := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
m.ServeHTTP(resp, req)
|
||||
m := chi.NewRouter()
|
||||
m.Get("/", func(resp http.ResponseWriter, req *http.Request) {
|
||||
var f defaultForm
|
||||
Bind(req, &f)
|
||||
assert.EqualValues(t, f.Default, "hello world")
|
||||
})
|
||||
resp := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
m.ServeHTTP(resp, req)
|
||||
}
|
||||
|
|
4
go.mod
4
go.mod
|
@ -4,7 +4,7 @@ go 1.13
|
|||
|
||||
require (
|
||||
github.com/go-chi/chi v1.5.1
|
||||
github.com/goccy/go-json v0.4.7
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337
|
||||
github.com/goccy/go-json v0.4.11
|
||||
github.com/stretchr/testify v1.3.0
|
||||
github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e
|
||||
)
|
||||
|
|
25
go.sum
25
go.sum
|
@ -1,24 +1,21 @@
|
|||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-chi/chi v1.5.1 h1:kfTK3Cxd/dkMu/rKs5ZceWYp+t5CtiE7vmaTv3LjC6w=
|
||||
github.com/go-chi/chi v1.5.1/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k=
|
||||
github.com/goccy/go-json v0.4.7 h1:xGUjaNfhpqhKAV2LoyNXihFLZ8ABSST8B+W+duHqkPI=
|
||||
github.com/goccy/go-json v0.4.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/goccy/go-json v0.4.11 h1:92nyX606ZN/cUFwctfxwDWm8YWSA38Zlv9s7taFeLyo=
|
||||
github.com/goccy/go-json v0.4.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
|
||||
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 h1:Jpy1PXuP99tXNrhbq2BaPz9B+jNAvH1JPQQpG/9GCXY=
|
||||
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c h1:Ho+uVpkel/udgjbwB5Lktg9BtvJSh2DT0Hi6LPSyI2w=
|
||||
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8=
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e h1:GSGeB9EAKY2spCABz6xOX5DbxZEXolK+nBSvmsQwRjM=
|
||||
github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
|
|
222
json_test.go
222
json_test.go
|
@ -20,11 +20,13 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var jsonTestCases = []jsonTestCase{
|
||||
|
@ -78,7 +80,7 @@ var jsonTestCases = []jsonTestCase{
|
|||
shouldSucceedOnJson: false,
|
||||
payload: `{"title":"foo"`,
|
||||
contentType: _JSON_CONTENT_TYPE,
|
||||
expected: Post{},
|
||||
expected: Post{Title: "foo"},
|
||||
},
|
||||
{
|
||||
description: "Deserialization with nested and embedded struct",
|
||||
|
@ -126,120 +128,130 @@ var jsonTestCases = []jsonTestCase{
|
|||
}
|
||||
|
||||
func Test_Json(t *testing.T) {
|
||||
Convey("Test JSON", t, func() {
|
||||
for _, testCase := range jsonTestCases {
|
||||
performJsonTest(t, JSON, testCase)
|
||||
}
|
||||
})
|
||||
for _, testCase := range jsonTestCases {
|
||||
performJsonTest(t, JSON, testCase)
|
||||
}
|
||||
}
|
||||
|
||||
func performJsonTest(t *testing.T, binder handlerFunc, testCase jsonTestCase) {
|
||||
var payload io.Reader
|
||||
httpRecorder := httptest.NewRecorder()
|
||||
m := chi.NewRouter()
|
||||
fnName := runtime.FuncForPC(reflect.ValueOf(binder).Pointer()).Name()
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
var payload io.Reader
|
||||
httpRecorder := httptest.NewRecorder()
|
||||
m := chi.NewRouter()
|
||||
|
||||
jsonTestHandler := func(actual interface{}, errs Errors) {
|
||||
if testCase.shouldSucceedOnJson && len(errs) > 0 {
|
||||
So(len(errs), ShouldEqual, 0)
|
||||
} else if !testCase.shouldSucceedOnJson && len(errs) == 0 {
|
||||
So(len(errs), ShouldNotEqual, 0)
|
||||
}
|
||||
So(fmt.Sprintf("%+v", actual), ShouldEqual, fmt.Sprintf("%+v", testCase.expected))
|
||||
}
|
||||
|
||||
switch testCase.expected.(type) {
|
||||
case []Post:
|
||||
if testCase.withInterface {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual []Post
|
||||
errs := binder(req, &actual)
|
||||
for i, a := range actual {
|
||||
So(a.Title, ShouldEqual, testCase.expected.([]Post)[i].Title)
|
||||
jsonTestHandler(a, errs)
|
||||
jsonTestHandler := func(actual interface{}, errs Errors) {
|
||||
if fnName == "JSON" {
|
||||
if testCase.shouldSucceedOnJson {
|
||||
assert.EqualValues(t, 0, len(errs), errs)
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", testCase.expected), fmt.Sprintf("%+v", actual))
|
||||
} else {
|
||||
assert.NotEqual(t, 0, len(errs))
|
||||
}
|
||||
})
|
||||
} else if fnName == "Bind" {
|
||||
if !testCase.shouldFailOnBind {
|
||||
assert.EqualValues(t, 0, len(errs), errs)
|
||||
} else {
|
||||
assert.NotEqual(t, 0, len(errs))
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", testCase.expected), fmt.Sprintf("%+v", actual))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch testCase.expected.(type) {
|
||||
case []Post:
|
||||
if testCase.withInterface {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual []Post
|
||||
errs := binder(req, &actual)
|
||||
for i, a := range actual {
|
||||
assert.EqualValues(t, testCase.expected.([]Post)[i].Title, a.Title)
|
||||
jsonTestHandler(a, errs)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual []Post
|
||||
errs := binder(req, &actual)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
}
|
||||
|
||||
case Post:
|
||||
if testCase.withInterface {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Post
|
||||
errs := binder(req, &actual)
|
||||
assert.EqualValues(t, actual.Title, testCase.expected.(Post).Title)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Post
|
||||
errs := binder(req, &actual)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
}
|
||||
|
||||
case BlogPost:
|
||||
if testCase.withInterface {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
assert.EqualValues(t, actual.Title, testCase.expected.(BlogPost).Title)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
}
|
||||
case Group:
|
||||
if testCase.withInterface {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Group
|
||||
errs := binder(req, &actual)
|
||||
assert.EqualValues(t, actual.Name, testCase.expected.(Group).Name)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Group
|
||||
errs := binder(req, &actual)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if testCase.payload == "-nil-" {
|
||||
payload = nil
|
||||
} else {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual []Post
|
||||
errs := binder(req, &actual)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
payload = strings.NewReader(testCase.payload)
|
||||
}
|
||||
|
||||
case Post:
|
||||
if testCase.withInterface {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Post
|
||||
errs := binder(req, &actual)
|
||||
So(actual.Title, ShouldEqual, testCase.expected.(Post).Title)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Post
|
||||
errs := binder(req, &actual)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
req, err := http.NewRequest("POST", testRoute, payload)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", testCase.contentType)
|
||||
|
||||
case BlogPost:
|
||||
if testCase.withInterface {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
So(actual.Title, ShouldEqual, testCase.expected.(BlogPost).Title)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
m.ServeHTTP(httpRecorder, req)
|
||||
|
||||
switch httpRecorder.Code {
|
||||
case http.StatusNotFound:
|
||||
panic("Routing is messed up in test fixture (got 404): check method and path")
|
||||
case http.StatusInternalServerError:
|
||||
panic("Something bad happened on '" + testCase.description + "'")
|
||||
default:
|
||||
if testCase.shouldSucceedOnJson &&
|
||||
httpRecorder.Code != http.StatusOK &&
|
||||
!testCase.shouldFailOnBind {
|
||||
assert.EqualValues(t, httpRecorder.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
case Group:
|
||||
if testCase.withInterface {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Group
|
||||
errs := binder(req, &actual)
|
||||
So(actual.Name, ShouldEqual, testCase.expected.(Group).Name)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
} else {
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Group
|
||||
errs := binder(req, &actual)
|
||||
jsonTestHandler(actual, errs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if testCase.payload == "-nil-" {
|
||||
payload = nil
|
||||
} else {
|
||||
payload = strings.NewReader(testCase.payload)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", testRoute, payload)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", testCase.contentType)
|
||||
|
||||
m.ServeHTTP(httpRecorder, req)
|
||||
|
||||
switch httpRecorder.Code {
|
||||
case http.StatusNotFound:
|
||||
panic("Routing is messed up in test fixture (got 404): check method and path")
|
||||
case http.StatusInternalServerError:
|
||||
panic("Something bad happened on '" + testCase.description + "'")
|
||||
default:
|
||||
if testCase.shouldSucceedOnJson &&
|
||||
httpRecorder.Code != http.StatusOK &&
|
||||
!testCase.shouldFailOnBind {
|
||||
So(httpRecorder.Code, ShouldEqual, http.StatusOK)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type (
|
||||
|
|
116
misc_test.go
116
misc_test.go
|
@ -23,84 +23,80 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// When binding from Form data, testing the type of data to bind
|
||||
// and converting a string into that type is tedious, so these tests
|
||||
// cover all those cases.
|
||||
func Test_SetWithProperType(t *testing.T) {
|
||||
Convey("Set with proper type", t, func() {
|
||||
testInputs := map[string]string{
|
||||
"successful": `integer=-1&integer8=-8&integer16=-16&integer32=-32&integer64=-64&uinteger=1&uinteger8=8&uinteger16=16&uinteger32=32&uinteger64=64&boolean_1=true&fl32_1=32.3232&fl64_1=-64.6464646464&str=string`,
|
||||
"errorful": `integer=&integer8=asdf&integer16=--&integer32=&integer64=dsf&uinteger=&uinteger8=asdf&uinteger16=+&uinteger32= 32 &uinteger64=+%20+&boolean_1=&boolean_2=asdf&fl32_1=asdf&fl32_2=&fl64_1=&fl64_2=asdfstr`,
|
||||
}
|
||||
testInputs := map[string]string{
|
||||
"successful": `integer=-1&integer8=-8&integer16=-16&integer32=-32&integer64=-64&uinteger=1&uinteger8=8&uinteger16=16&uinteger32=32&uinteger64=64&boolean_1=true&fl32_1=32.3232&fl64_1=-64.6464646464&str=string`,
|
||||
"errorful": `integer=&integer8=asdf&integer16=--&integer32=&integer64=dsf&uinteger=&uinteger8=asdf&uinteger16=+&uinteger32= 32 &uinteger64=+%20+&boolean_1=&boolean_2=asdf&fl32_1=asdf&fl32_2=&fl64_1=&fl64_2=asdfstr`,
|
||||
}
|
||||
|
||||
expectedOutputs := map[string]Everything{
|
||||
"successful": Everything{
|
||||
Integer: -1,
|
||||
Integer8: -8,
|
||||
Integer16: -16,
|
||||
Integer32: -32,
|
||||
Integer64: -64,
|
||||
Uinteger: 1,
|
||||
Uinteger8: 8,
|
||||
Uinteger16: 16,
|
||||
Uinteger32: 32,
|
||||
Uinteger64: 64,
|
||||
Boolean_1: true,
|
||||
Fl32_1: 32.3232,
|
||||
Fl64_1: -64.6464646464,
|
||||
Str: "string",
|
||||
},
|
||||
"errorful": Everything{},
|
||||
}
|
||||
expectedOutputs := map[string]Everything{
|
||||
"successful": Everything{
|
||||
Integer: -1,
|
||||
Integer8: -8,
|
||||
Integer16: -16,
|
||||
Integer32: -32,
|
||||
Integer64: -64,
|
||||
Uinteger: 1,
|
||||
Uinteger8: 8,
|
||||
Uinteger16: 16,
|
||||
Uinteger32: 32,
|
||||
Uinteger64: 64,
|
||||
Boolean_1: true,
|
||||
Fl32_1: 32.3232,
|
||||
Fl64_1: -64.6464646464,
|
||||
Str: "string",
|
||||
},
|
||||
"errorful": Everything{},
|
||||
}
|
||||
|
||||
for key, testCase := range testInputs {
|
||||
httpRecorder := httptest.NewRecorder()
|
||||
m := chi.NewRouter()
|
||||
for key, testCase := range testInputs {
|
||||
httpRecorder := httptest.NewRecorder()
|
||||
m := chi.NewRouter()
|
||||
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Everything
|
||||
errs := Form(req, &actual)
|
||||
So(fmt.Sprintf("%+v", actual), ShouldEqual, fmt.Sprintf("%+v", expectedOutputs[key]))
|
||||
if key == "errorful" {
|
||||
So(errs, ShouldHaveLength, 10)
|
||||
} else {
|
||||
So(errs, ShouldHaveLength, 0)
|
||||
}
|
||||
})
|
||||
req, err := http.NewRequest("POST", testRoute, strings.NewReader(testCase))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
var actual Everything
|
||||
errs := Form(req, &actual)
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", actual), fmt.Sprintf("%+v", expectedOutputs[key]))
|
||||
if key == "errorful" {
|
||||
assert.EqualValues(t, len(errs), 10)
|
||||
} else {
|
||||
assert.EqualValues(t, len(errs), 0)
|
||||
}
|
||||
req.Header.Set("Content-Type", formContentType)
|
||||
m.ServeHTTP(httpRecorder, req)
|
||||
})
|
||||
req, err := http.NewRequest("POST", testRoute, strings.NewReader(testCase))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
req.Header.Set("Content-Type", formContentType)
|
||||
m.ServeHTTP(httpRecorder, req)
|
||||
}
|
||||
}
|
||||
|
||||
// Each binder middleware should assert that the struct passed in is not
|
||||
// a pointer (to avoid race conditions)
|
||||
func Test_EnsureNotPointer(t *testing.T) {
|
||||
Convey("Ensure field is not a pointer", t, func() {
|
||||
shouldPanic := func() {
|
||||
defer func() {
|
||||
So(recover(), ShouldNotBeNil)
|
||||
}()
|
||||
ensureNotPointer(&Post{})
|
||||
}
|
||||
shouldPanic := func() {
|
||||
defer func() {
|
||||
assert.NotNil(t, recover())
|
||||
}()
|
||||
ensureNotPointer(&Post{})
|
||||
}
|
||||
|
||||
shouldNotPanic := func() {
|
||||
defer func() {
|
||||
So(recover(), ShouldBeNil)
|
||||
}()
|
||||
ensureNotPointer(Post{})
|
||||
}
|
||||
shouldNotPanic := func() {
|
||||
defer func() {
|
||||
assert.Nil(t, recover())
|
||||
}()
|
||||
ensureNotPointer(Post{})
|
||||
}
|
||||
|
||||
shouldPanic()
|
||||
shouldNotPanic()
|
||||
})
|
||||
shouldPanic()
|
||||
shouldNotPanic()
|
||||
}
|
||||
|
||||
// Used in testing setWithProperType; kind of clunky...
|
||||
|
|
|
@ -25,7 +25,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var multipartFormTestCases = []multipartFormTestCase{
|
||||
|
@ -73,11 +73,9 @@ var multipartFormTestCases = []multipartFormTestCase{
|
|||
}
|
||||
|
||||
func Test_MultipartForm(t *testing.T) {
|
||||
Convey("Test multipart form", t, func() {
|
||||
for _, testCase := range multipartFormTestCases {
|
||||
performMultipartFormTest(t, MultipartForm, testCase)
|
||||
}
|
||||
})
|
||||
for _, testCase := range multipartFormTestCases {
|
||||
performMultipartFormTest(t, MultipartForm, testCase)
|
||||
}
|
||||
}
|
||||
|
||||
func performMultipartFormTest(t *testing.T, binder handlerFunc, testCase multipartFormTestCase) {
|
||||
|
@ -88,11 +86,11 @@ func performMultipartFormTest(t *testing.T, binder handlerFunc, testCase multipa
|
|||
var actual BlogPost
|
||||
errs := binder(req, &actual)
|
||||
if testCase.shouldSucceed && len(errs) > 0 {
|
||||
So(len(errs), ShouldEqual, 0)
|
||||
assert.EqualValues(t, 0, len(errs))
|
||||
} else if !testCase.shouldSucceed && len(errs) == 0 {
|
||||
So(len(errs), ShouldNotEqual, 0)
|
||||
assert.NotEqual(t, 0, len(errs))
|
||||
}
|
||||
So(fmt.Sprintf("%+v", actual), ShouldEqual, fmt.Sprintf("%+v", testCase.inputAndExpected))
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", actual), fmt.Sprintf("%+v", testCase.inputAndExpected))
|
||||
})
|
||||
|
||||
multipartPayload, mpWriter := makeMultipartPayload(testCase)
|
||||
|
|
|
@ -22,7 +22,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var validationTestCases = []validationTestCase{
|
||||
|
@ -373,11 +373,9 @@ var validationTestCases = []validationTestCase{
|
|||
}
|
||||
|
||||
func Test_Validation(t *testing.T) {
|
||||
Convey("Test validation", t, func() {
|
||||
for _, testCase := range validationTestCases {
|
||||
performValidationTest(t, testCase)
|
||||
}
|
||||
})
|
||||
for _, testCase := range validationTestCases {
|
||||
performValidationTest(t, testCase)
|
||||
}
|
||||
}
|
||||
|
||||
func performValidationTest(t *testing.T, testCase validationTestCase) {
|
||||
|
@ -386,7 +384,7 @@ func performValidationTest(t *testing.T, testCase validationTestCase) {
|
|||
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
actual := Validate(req, testCase.data)
|
||||
So(fmt.Sprintf("%+v", actual), ShouldEqual, fmt.Sprintf("%+v", testCase.expectedErrors))
|
||||
assert.EqualValues(t, fmt.Sprintf("%+v", actual), fmt.Sprintf("%+v", testCase.expectedErrors))
|
||||
})
|
||||
|
||||
req, err := http.NewRequest("POST", testRoute, nil)
|
||||
|
|
Loading…
Reference in New Issue
Block a user