Tons of unit tests
This commit is contained in:
@ -50,3 +50,10 @@ func Default(method, contentType string) Binding {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Validate(obj interface{}) error {
|
||||
if err := _validator.ValidateStruct(obj); err != nil {
|
||||
return error(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
79
binding/binding_test.go
Normal file
79
binding/binding_test.go
Normal file
@ -0,0 +1,79 @@
|
||||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type FooStruct struct {
|
||||
Foo string `json:"foo" form:"foo" xml:"foo" binding:"required"`
|
||||
}
|
||||
|
||||
func TestBindingDefault(t *testing.T) {
|
||||
assert.Equal(t, Default("GET", ""), GETForm)
|
||||
assert.Equal(t, Default("GET", MIMEJSON), GETForm)
|
||||
|
||||
assert.Equal(t, Default("POST", MIMEJSON), JSON)
|
||||
assert.Equal(t, Default("PUT", MIMEJSON), JSON)
|
||||
|
||||
assert.Equal(t, Default("POST", MIMEXML), XML)
|
||||
assert.Equal(t, Default("PUT", MIMEXML2), XML)
|
||||
|
||||
assert.Equal(t, Default("POST", MIMEPOSTForm), POSTForm)
|
||||
assert.Equal(t, Default("DELETE", MIMEPOSTForm), POSTForm)
|
||||
}
|
||||
|
||||
func TestBindingJSON(t *testing.T) {
|
||||
testBinding(t,
|
||||
JSON, "json",
|
||||
"/", "/",
|
||||
`{"foo": "bar"}`, `{"bar": "foo"}`)
|
||||
}
|
||||
|
||||
func TestBindingPOSTForm(t *testing.T) {
|
||||
testBinding(t,
|
||||
POSTForm, "post_form",
|
||||
"/", "/",
|
||||
"foo=bar", "bar=foo")
|
||||
}
|
||||
|
||||
func TestBindingGETForm(t *testing.T) {
|
||||
testBinding(t,
|
||||
GETForm, "get_form",
|
||||
"/?foo=bar", "/?bar=foo",
|
||||
"", "")
|
||||
}
|
||||
|
||||
func TestBindingXML(t *testing.T) {
|
||||
testBinding(t,
|
||||
XML, "xml",
|
||||
"/", "/",
|
||||
"<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
|
||||
}
|
||||
|
||||
func testBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
||||
assert.Equal(t, b.Name(), name)
|
||||
|
||||
obj := FooStruct{}
|
||||
req := requestWithBody(path, body)
|
||||
err := b.Bind(req, &obj)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, obj.Foo, "bar")
|
||||
|
||||
obj = FooStruct{}
|
||||
req = requestWithBody(badPath, badBody)
|
||||
err = JSON.Bind(req, &obj)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func requestWithBody(path, body string) (req *http.Request) {
|
||||
req, _ = http.NewRequest("POST", path, bytes.NewBufferString(body))
|
||||
return
|
||||
}
|
@ -19,8 +19,5 @@ func (_ getFormBinding) Bind(req *http.Request, obj interface{}) error {
|
||||
if err := mapForm(obj, req.Form); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := _validator.ValidateStruct(obj); err != nil {
|
||||
return error(err)
|
||||
}
|
||||
return nil
|
||||
return Validate(obj)
|
||||
}
|
||||
|
@ -21,8 +21,5 @@ func (_ jsonBinding) Bind(req *http.Request, obj interface{}) error {
|
||||
if err := decoder.Decode(obj); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := _validator.ValidateStruct(obj); err != nil {
|
||||
return error(err)
|
||||
}
|
||||
return nil
|
||||
return Validate(obj)
|
||||
}
|
||||
|
@ -19,8 +19,5 @@ func (_ postFormBinding) Bind(req *http.Request, obj interface{}) error {
|
||||
if err := mapForm(obj, req.PostForm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := _validator.ValidateStruct(obj); err != nil {
|
||||
return error(err)
|
||||
}
|
||||
return nil
|
||||
return Validate(obj)
|
||||
}
|
||||
|
53
binding/validate_test.go
Normal file
53
binding/validate_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package binding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type struct1 struct {
|
||||
Value float64 `binding:"required"`
|
||||
}
|
||||
|
||||
type struct2 struct {
|
||||
RequiredValue string `binding:"required"`
|
||||
Value float64
|
||||
}
|
||||
|
||||
type struct3 struct {
|
||||
Integer int
|
||||
String string
|
||||
BasicSlice []int
|
||||
Boolean bool
|
||||
|
||||
RequiredInteger int `binding:"required"`
|
||||
RequiredString string `binding:"required"`
|
||||
RequiredAnotherStruct struct1 `binding:"required"`
|
||||
RequiredBasicSlice []int `binding:"required"`
|
||||
RequiredComplexSlice []struct2 `binding:"required"`
|
||||
RequiredBoolean bool `binding:"required"`
|
||||
}
|
||||
|
||||
func createStruct() struct3 {
|
||||
return struct3{
|
||||
RequiredInteger: 2,
|
||||
RequiredString: "hello",
|
||||
RequiredAnotherStruct: struct1{1.5},
|
||||
RequiredBasicSlice: []int{1, 2, 3, 4},
|
||||
RequiredComplexSlice: []struct2{
|
||||
{RequiredValue: "A"},
|
||||
{RequiredValue: "B"},
|
||||
},
|
||||
RequiredBoolean: true,
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateGoodObject(t *testing.T) {
|
||||
test := createStruct()
|
||||
assert.Nil(t, Validate(&test))
|
||||
}
|
@ -20,8 +20,5 @@ func (_ xmlBinding) Bind(req *http.Request, obj interface{}) error {
|
||||
if err := decoder.Decode(obj); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := _validator.ValidateStruct(obj); err != nil {
|
||||
return error(err)
|
||||
}
|
||||
return nil
|
||||
return Validate(obj)
|
||||
}
|
||||
|
Reference in New Issue
Block a user