Skip to content

Commit

Permalink
First commit to v3, labstack#665
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Sep 23, 2016
1 parent 04f4504 commit 2aec035
Show file tree
Hide file tree
Showing 66 changed files with 656 additions and 3,264 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ before_install:
script:
- go test -coverprofile=echo.coverprofile
- go test -coverprofile=middleware.coverprofile ./middleware
- go test -coverprofile=engine_standatd.coverprofile ./engine/standard
- go test -coverprofile=engine_fasthttp.coverprofile ./engine/fasthttp
- $HOME/gopath/bin/gover
- $HOME/gopath/bin/goveralls -coverprofile=gover.coverprofile -service=travis-ci
matrix:
Expand Down
42 changes: 23 additions & 19 deletions binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,47 @@ type (

func (b *binder) Bind(i interface{}, c Context) (err error) {
req := c.Request()
if req.Method() == GET {
if req.Method == GET {
if err = b.bindData(i, c.QueryParams()); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error())
return NewHTTPError(http.StatusBadRequest, err.Error())
}
return
}
ctype := req.Header().Get(HeaderContentType)
if req.Body() == nil {
err = NewHTTPError(http.StatusBadRequest, "request body can't be empty")
return
ctype := req.Header.Get(HeaderContentType)
if req.ContentLength == 0 {
return NewHTTPError(http.StatusBadRequest, "request body can't be empty")
}
err = ErrUnsupportedMediaType
switch {
case strings.HasPrefix(ctype, MIMEApplicationJSON):
if err = json.NewDecoder(req.Body()).Decode(i); err != nil {
if err = json.NewDecoder(req.Body).Decode(i); err != nil {
if ute, ok := err.(*json.UnmarshalTypeError); ok {
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unmarshal type error: expected=%v, got=%v, offset=%v", ute.Type, ute.Value, ute.Offset))
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unmarshal type error: expected=%v, got=%v, offset=%v", ute.Type, ute.Value, ute.Offset))
} else if se, ok := err.(*json.SyntaxError); ok {
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("syntax error: offset=%v, error=%v", se.Offset, se.Error()))
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("syntax error: offset=%v, error=%v", se.Offset, se.Error()))
} else {
err = NewHTTPError(http.StatusBadRequest, err.Error())
return NewHTTPError(http.StatusBadRequest, err.Error())
}
}
case strings.HasPrefix(ctype, MIMEApplicationXML):
if err = xml.NewDecoder(req.Body()).Decode(i); err != nil {
if err = xml.NewDecoder(req.Body).Decode(i); err != nil {
if ute, ok := err.(*xml.UnsupportedTypeError); ok {
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unsupported type error: type=%v, error=%v", ute.Type, ute.Error()))
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unsupported type error: type=%v, error=%v", ute.Type, ute.Error()))
} else if se, ok := err.(*xml.SyntaxError); ok {
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("syntax error: line=%v, error=%v", se.Line, se.Error()))
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("syntax error: line=%v, error=%v", se.Line, se.Error()))
} else {
err = NewHTTPError(http.StatusBadRequest, err.Error())
return NewHTTPError(http.StatusBadRequest, err.Error())
}
}
case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm):
if err = b.bindData(i, req.FormParams()); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error())
params, err := c.FormParams()
if err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error())
}
if err = b.bindData(i, params); err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error())
}
default:
return ErrUnsupportedMediaType
}
return
}
Expand Down Expand Up @@ -100,8 +104,8 @@ func (b *binder) bindData(ptr interface{}, data map[string][]string) error {
if structFieldKind == reflect.Slice && numElems > 0 {
sliceOf := structField.Type().Elem().Kind()
slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
for i := 0; i < numElems; i++ {
if err := setWithProperType(sliceOf, inputValue[i], slice.Index(i)); err != nil {
for j := 0; j < numElems; j++ {
if err := setWithProperType(sliceOf, inputValue[j], slice.Index(j)); err != nil {
return err
}
}
Expand Down
31 changes: 13 additions & 18 deletions binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

"github.com/labstack/echo/test"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -70,19 +70,19 @@ func TestBinderForm(t *testing.T) {
testBinderOkay(t, strings.NewReader(userForm), MIMEApplicationForm)
testBinderError(t, nil, MIMEApplicationForm)
e := New()
req := test.NewRequest(POST, "/", strings.NewReader(userForm))
rec := test.NewResponseRecorder()
req, _ := http.NewRequest(POST, "/", strings.NewReader(userForm))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header().Set(HeaderContentType, MIMEApplicationForm)
var obj = make([]struct{ Field string }, 0)
req.Header.Set(HeaderContentType, MIMEApplicationForm)
obj := []struct{ Field string }{}
err := c.Bind(&obj)
assert.Error(t, err)
}

func TestBinderQueryParams(t *testing.T) {
e := New()
req := test.NewRequest(GET, "/?id=1&name=Jon Snow", nil)
rec := test.NewResponseRecorder()
req, _ := http.NewRequest(GET, "/?id=1&name=Jon Snow", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
u := new(user)
err := c.Bind(u)
Expand All @@ -105,11 +105,6 @@ func TestBinderUnsupportedMediaType(t *testing.T) {
testBinderError(t, strings.NewReader(invalidContent), MIMEApplicationJSON)
}

// func assertCustomer(t *testing.T, c *user) {
// assert.Equal(t, 1, c.ID)
// assert.Equal(t, "Joe", c.Name)
// }

func TestBinderbindForm(t *testing.T) {
ts := new(binderTestStruct)
b := new(binder)
Expand Down Expand Up @@ -201,10 +196,10 @@ func assertBinderTestStruct(t *testing.T, ts *binderTestStruct) {

func testBinderOkay(t *testing.T, r io.Reader, ctype string) {
e := New()
req := test.NewRequest(POST, "/", r)
rec := test.NewResponseRecorder()
req, _ := http.NewRequest(POST, "/", r)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header().Set(HeaderContentType, ctype)
req.Header.Set(HeaderContentType, ctype)
u := new(user)
err := c.Bind(u)
if assert.NoError(t, err) {
Expand All @@ -215,10 +210,10 @@ func testBinderOkay(t *testing.T, r io.Reader, ctype string) {

func testBinderError(t *testing.T, r io.Reader, ctype string) {
e := New()
req := test.NewRequest(POST, "/", r)
rec := test.NewResponseRecorder()
req, _ := http.NewRequest(POST, "/", r)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header().Set(HeaderContentType, ctype)
req.Header.Set(HeaderContentType, ctype)
u := new(user)
err := c.Bind(u)

Expand Down
Loading

0 comments on commit 2aec035

Please sign in to comment.