forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac1ee3f
commit 0a192fb
Showing
26 changed files
with
1,477 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
Godeps/* | ||
!Godeps/Godeps.json | ||
coverage.out | ||
count.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.