Skip to content

Commit

Permalink
Change context.Bind() return type to HTTPError. labstack#344
Browse files Browse the repository at this point in the history
  • Loading branch information
o1egl committed Jan 30, 2016
1 parent 4f57798 commit 981e3ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
35 changes: 28 additions & 7 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestContext(t *testing.T) {
userJSONIndent := "{\n_?\"id\": \"1\",\n_?\"name\": \"Joe\"\n_}"
userXML := `<user><id>1</id><name>Joe</name></user>`
userXMLIndent := "_<user>\n_?<id>1</id>\n_?<name>Joe</name>\n_</user>"
incorrectContent := "this is incorrect content"

var nonMarshallableChannel chan bool

Expand Down Expand Up @@ -66,14 +67,18 @@ func TestContext(t *testing.T) {
//------

// JSON
testBind(t, c, "application/json")
testBindOk(t, c, ApplicationJSON)
c.X().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
testBindError(t, c, ApplicationJSON)

// XML
c.X().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
testBind(t, c, ApplicationXML)
testBindOk(t, c, ApplicationXML)
c.X().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
testBindError(t, c, ApplicationXML)

// Unsupported
testBind(t, c, "")
testBindError(t, c, "")

//--------
// Render
Expand Down Expand Up @@ -273,14 +278,30 @@ func TestContextNetContext(t *testing.T) {
// assert.Equal(t, "val", c.Value("key"))
}

func testBind(t *testing.T, c Context, ct string) {
func testBindOk(t *testing.T, c Context, ct string) {
c.Request().Header().Set(ContentType, ct)
u := new(user)
err := c.Bind(u)
if ct == "" {
assert.Error(t, UnsupportedMediaType)
} else if assert.NoError(t, err) {
if assert.NoError(t, err) {
assert.Equal(t, "1", u.ID)
assert.Equal(t, "Joe", u.Name)
}
}

func testBindError(t *testing.T, c Context, ct string) {
c.Request().Header().Set(ContentType, ct)
u := new(user)
err := c.Bind(u)

switch ct {
case ApplicationJSON, ApplicationXML:
if assert.IsType(t, new(HTTPError), err) {
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).code)
}
default:
if assert.IsType(t, new(HTTPError), err) {
assert.Equal(t, UnsupportedMediaType, err)
}

}
}
10 changes: 7 additions & 3 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ var (
// Errors
//--------

UnsupportedMediaType = errors.New("unsupported media type")
UnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
RendererNotRegistered = errors.New("renderer not registered")
InvalidRedirectCode = errors.New("invalid redirect status code")

Expand Down Expand Up @@ -647,9 +647,13 @@ func (binder) Bind(r engine.Request, i interface{}) (err error) {
ct := r.Header().Get(ContentType)
err = UnsupportedMediaType
if strings.HasPrefix(ct, ApplicationJSON) {
err = json.NewDecoder(r.Body()).Decode(i)
if err = json.NewDecoder(r.Body()).Decode(i); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error())
}
} else if strings.HasPrefix(ct, ApplicationXML) {
err = xml.NewDecoder(r.Body()).Decode(i)
if err = xml.NewDecoder(r.Body()).Decode(i); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error())
}
}
return
}

0 comments on commit 981e3ad

Please sign in to comment.