Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bound interface to be a slice of data #1574

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag

// !struct
if typ.Kind() != reflect.Struct {
if tag == "param" || tag == "query" {
// incompatible type, data is probably to be found in the body
return nil
}
return errors.New("binding element must be a struct")
}

Expand Down
52 changes: 43 additions & 9 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -162,19 +163,26 @@ var values = map[string][]string{

func TestBindJSON(t *testing.T) {
assert := assert.New(t)
testBindOkay(assert, strings.NewReader(userJSON), MIMEApplicationJSON)
testBindOkay(assert, strings.NewReader(userJSON), nil, MIMEApplicationJSON)
testBindOkay(assert, strings.NewReader(userJSON), dummyQuery, MIMEApplicationJSON)
testBindArrayOkay(assert, strings.NewReader(usersJSON), nil, MIMEApplicationJSON)
testBindArrayOkay(assert, strings.NewReader(usersJSON), dummyQuery, MIMEApplicationJSON)
testBindError(assert, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{})
testBindError(assert, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{})
}

func TestBindXML(t *testing.T) {
assert := assert.New(t)

testBindOkay(assert, strings.NewReader(userXML), MIMEApplicationXML)
testBindOkay(assert, strings.NewReader(userXML), nil, MIMEApplicationXML)
testBindOkay(assert, strings.NewReader(userXML), dummyQuery, MIMEApplicationXML)
testBindArrayOkay(assert, strings.NewReader(userXML), nil, MIMEApplicationXML)
testBindArrayOkay(assert, strings.NewReader(userXML), dummyQuery, MIMEApplicationXML)
testBindError(assert, strings.NewReader(invalidContent), MIMEApplicationXML, errors.New(""))
testBindError(assert, strings.NewReader(userXMLConvertNumberError), MIMEApplicationXML, &strconv.NumError{})
testBindError(assert, strings.NewReader(userXMLUnsupportedTypeError), MIMEApplicationXML, &xml.SyntaxError{})
testBindOkay(assert, strings.NewReader(userXML), MIMETextXML)
testBindOkay(assert, strings.NewReader(userXML), nil, MIMETextXML)
testBindOkay(assert, strings.NewReader(userXML), dummyQuery, MIMETextXML)
testBindError(assert, strings.NewReader(invalidContent), MIMETextXML, errors.New(""))
testBindError(assert, strings.NewReader(userXMLConvertNumberError), MIMETextXML, &strconv.NumError{})
testBindError(assert, strings.NewReader(userXMLUnsupportedTypeError), MIMETextXML, &xml.SyntaxError{})
Expand All @@ -183,7 +191,8 @@ func TestBindXML(t *testing.T) {
func TestBindForm(t *testing.T) {
assert := assert.New(t)

testBindOkay(assert, strings.NewReader(userForm), MIMEApplicationForm)
testBindOkay(assert, strings.NewReader(userForm), nil, MIMEApplicationForm)
testBindOkay(assert, strings.NewReader(userForm), dummyQuery, MIMEApplicationForm)
e := New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userForm))
rec := httptest.NewRecorder()
Expand Down Expand Up @@ -307,14 +316,16 @@ func TestBindUnmarshalTextPtr(t *testing.T) {
}

func TestBindMultipartForm(t *testing.T) {
body := new(bytes.Buffer)
mw := multipart.NewWriter(body)
bodyBuffer := new(bytes.Buffer)
mw := multipart.NewWriter(bodyBuffer)
mw.WriteField("id", "1")
mw.WriteField("name", "Jon Snow")
mw.Close()
body := bodyBuffer.Bytes()

assert := assert.New(t)
testBindOkay(assert, body, mw.FormDataContentType())
testBindOkay(assert, bytes.NewReader(body), nil, mw.FormDataContentType())
testBindOkay(assert, bytes.NewReader(body), dummyQuery, mw.FormDataContentType())
}

func TestBindUnsupportedMediaType(t *testing.T) {
Expand Down Expand Up @@ -516,9 +527,13 @@ func assertBindTestStruct(a *assert.Assertions, ts *bindTestStruct) {
a.Equal("", ts.GetCantSet())
}

func testBindOkay(assert *assert.Assertions, r io.Reader, ctype string) {
func testBindOkay(assert *assert.Assertions, r io.Reader, query url.Values, ctype string) {
e := New()
req := httptest.NewRequest(http.MethodPost, "/", r)
path := "/"
if len(query) > 0 {
path += "?" + query.Encode()
}
req := httptest.NewRequest(http.MethodPost, path, r)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, ctype)
Expand All @@ -530,6 +545,25 @@ func testBindOkay(assert *assert.Assertions, r io.Reader, ctype string) {
}
}

func testBindArrayOkay(assert *assert.Assertions, r io.Reader, query url.Values, ctype string) {
e := New()
path := "/"
if len(query) > 0 {
path += "?" + query.Encode()
}
req := httptest.NewRequest(http.MethodPost, path, r)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, ctype)
u := []user{}
err := c.Bind(&u)
if assert.NoError(err) {
assert.Equal(1, len(u))
assert.Equal(1, u[0].ID)
assert.Equal("Jon Snow", u[0].Name)
}
}

func testBindError(assert *assert.Assertions, r io.Reader, ctype string, expectedInternal error) {
e := New()
req := httptest.NewRequest(http.MethodPost, "/", r)
Expand Down
4 changes: 4 additions & 0 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
Expand All @@ -26,6 +27,7 @@ type (

const (
userJSON = `{"id":1,"name":"Jon Snow"}`
usersJSON = `[{"id":1,"name":"Jon Snow"}]`
userXML = `<user><id>1</id><name>Jon Snow</name></user>`
userForm = `id=1&name=Jon Snow`
invalidContent = "invalid content"
Expand All @@ -44,6 +46,8 @@ const userXMLPretty = `<user>
<name>Jon Snow</name>
</user>`

var dummyQuery = url.Values{"dummy": []string{"useless"}}

func TestEcho(t *testing.T) {
e := New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
Expand Down