mirror of
https://github.com/labstack/echo.git
synced 2025-03-31 22:05:06 +02:00
Change context.Bind() return type to HTTPError #344
This commit is contained in:
parent
055e7ae0e6
commit
4cb8421b73
@ -34,6 +34,7 @@ func TestContext(t *testing.T) {
|
|||||||
userJSONIndent := "{\n_?\"id\": \"1\",\n_?\"name\": \"Joe\"\n_}"
|
userJSONIndent := "{\n_?\"id\": \"1\",\n_?\"name\": \"Joe\"\n_}"
|
||||||
userXML := `<user><id>1</id><name>Joe</name></user>`
|
userXML := `<user><id>1</id><name>Joe</name></user>`
|
||||||
userXMLIndent := "_<user>\n_?<id>1</id>\n_?<name>Joe</name>\n_</user>"
|
userXMLIndent := "_<user>\n_?<id>1</id>\n_?<name>Joe</name>\n_</user>"
|
||||||
|
incorrectContent := "this is incorrect content"
|
||||||
|
|
||||||
var nonMarshallableChannel chan bool
|
var nonMarshallableChannel chan bool
|
||||||
|
|
||||||
@ -68,14 +69,18 @@ func TestContext(t *testing.T) {
|
|||||||
//------
|
//------
|
||||||
|
|
||||||
// JSON
|
// JSON
|
||||||
testBind(t, c, "application/json")
|
testBindOk(t, c, ApplicationJSON)
|
||||||
|
c.request, _ = http.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
||||||
|
testBindError(t, c, ApplicationJSON)
|
||||||
|
|
||||||
// XML
|
// XML
|
||||||
c.request, _ = http.NewRequest(POST, "/", strings.NewReader(userXML))
|
c.request, _ = http.NewRequest(POST, "/", strings.NewReader(userXML))
|
||||||
testBind(t, c, ApplicationXML)
|
testBindOk(t, c, ApplicationXML)
|
||||||
|
c.request, _ = http.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
||||||
|
testBindError(t, c, ApplicationXML)
|
||||||
|
|
||||||
// Unsupported
|
// Unsupported
|
||||||
testBind(t, c, "")
|
testBindError(t, c, "")
|
||||||
|
|
||||||
//--------
|
//--------
|
||||||
// Render
|
// Render
|
||||||
@ -280,14 +285,30 @@ func TestContextNetContext(t *testing.T) {
|
|||||||
assert.Equal(t, "val", c.Value("key"))
|
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)
|
c.request.Header.Set(ContentType, ct)
|
||||||
u := new(user)
|
u := new(user)
|
||||||
err := c.Bind(u)
|
err := c.Bind(u)
|
||||||
if ct == "" {
|
if assert.NoError(t, err) {
|
||||||
assert.Error(t, UnsupportedMediaType)
|
|
||||||
} else if assert.NoError(t, err) {
|
|
||||||
assert.Equal(t, "1", u.ID)
|
assert.Equal(t, "1", u.ID)
|
||||||
assert.Equal(t, "Joe", u.Name)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11
echo.go
11
echo.go
@ -164,7 +164,7 @@ var (
|
|||||||
// Errors
|
// Errors
|
||||||
//--------
|
//--------
|
||||||
|
|
||||||
UnsupportedMediaType = errors.New("unsupported media type")
|
UnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
|
||||||
RendererNotRegistered = errors.New("renderer not registered")
|
RendererNotRegistered = errors.New("renderer not registered")
|
||||||
InvalidRedirectCode = errors.New("invalid redirect status code")
|
InvalidRedirectCode = errors.New("invalid redirect status code")
|
||||||
|
|
||||||
@ -693,9 +693,14 @@ func (binder) Bind(r *http.Request, i interface{}) (err error) {
|
|||||||
ct := r.Header.Get(ContentType)
|
ct := r.Header.Get(ContentType)
|
||||||
err = UnsupportedMediaType
|
err = UnsupportedMediaType
|
||||||
if strings.HasPrefix(ct, ApplicationJSON) {
|
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) {
|
} 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
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user