2015-04-09 13:15:02 +03:00
|
|
|
// 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) {
|
2015-05-05 16:06:38 +03:00
|
|
|
assert.Equal(t, Default("GET", ""), Form)
|
|
|
|
assert.Equal(t, Default("GET", MIMEJSON), Form)
|
2015-04-09 13:15:02 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
assert.Equal(t, Default("POST", MIMEPOSTForm), Form)
|
|
|
|
assert.Equal(t, Default("DELETE", MIMEPOSTForm), Form)
|
2015-04-09 13:15:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingJSON(t *testing.T) {
|
2015-05-05 16:06:38 +03:00
|
|
|
testBodyBinding(t,
|
2015-04-09 13:15:02 +03:00
|
|
|
JSON, "json",
|
|
|
|
"/", "/",
|
|
|
|
`{"foo": "bar"}`, `{"bar": "foo"}`)
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func TestBindingForm(t *testing.T) {
|
|
|
|
testFormBinding(t, "POST",
|
2015-04-09 13:15:02 +03:00
|
|
|
"/", "/",
|
|
|
|
"foo=bar", "bar=foo")
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func TestBindingForm2(t *testing.T) {
|
|
|
|
testFormBinding(t, "GET",
|
2015-04-09 13:15:02 +03:00
|
|
|
"/?foo=bar", "/?bar=foo",
|
|
|
|
"", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindingXML(t *testing.T) {
|
2015-05-05 16:06:38 +03:00
|
|
|
testBodyBinding(t,
|
2015-04-09 13:15:02 +03:00
|
|
|
XML, "xml",
|
|
|
|
"/", "/",
|
|
|
|
"<map><foo>bar</foo></map>", "<map><bar>foo</bar></map>")
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
|
|
|
|
b := Form
|
|
|
|
assert.Equal(t, b.Name(), "query")
|
|
|
|
|
|
|
|
obj := FooStruct{}
|
|
|
|
req := requestWithBody(method, path, body)
|
|
|
|
if method == "POST" {
|
|
|
|
req.Header.Add("Content-Type", MIMEPOSTForm)
|
|
|
|
}
|
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, obj.Foo, "bar")
|
|
|
|
|
|
|
|
obj = FooStruct{}
|
|
|
|
req = requestWithBody(method, badPath, badBody)
|
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
|
2015-04-09 13:15:02 +03:00
|
|
|
assert.Equal(t, b.Name(), name)
|
|
|
|
|
|
|
|
obj := FooStruct{}
|
2015-05-05 16:06:38 +03:00
|
|
|
req := requestWithBody("POST", path, body)
|
2015-04-09 13:15:02 +03:00
|
|
|
err := b.Bind(req, &obj)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, obj.Foo, "bar")
|
|
|
|
|
|
|
|
obj = FooStruct{}
|
2015-05-05 16:06:38 +03:00
|
|
|
req = requestWithBody("POST", badPath, badBody)
|
2015-04-09 13:15:02 +03:00
|
|
|
err = JSON.Bind(req, &obj)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
func requestWithBody(method, path, body string) (req *http.Request) {
|
|
|
|
req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
|
2015-04-09 13:15:02 +03:00
|
|
|
return
|
|
|
|
}
|