Repository: labstack/echo
File: bind_test.go (Go)

Code before:
	testBindOkay(assertion, strings.NewReader(userJSON), dummyQuery, MIMEApplicationJSON)
	testBindArrayOkay(assertion, strings.NewReader(usersJSON), nil, MIMEApplicationJSON)
	testBindArrayOkay(assertion, strings.NewReader(usersJSON), dummyQuery, MIMEApplicationJSON)
	testBindError(assertion, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{})
	testBindError(assertion, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{})
}

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

	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), 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{})
}

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

	testBindOkay(assertion, strings.NewReader(userForm), nil, MIMEApplicationForm)
	testBindOkay(assertion, strings.NewReader(userForm), dummyQuery, MIMEApplicationForm)
	e := New()
	req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userForm))
	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)
	req.Header.Set(HeaderContentType, MIMEApplicationForm)
	err := c.Bind(&[]struct{ Field string }{})
	assertion.Error(err)
}

func TestBindQueryParams(t *testing.T) {
	e := New()
	req := httptest.NewRequest(http.MethodGet, "/?id=1&name=Jon+Snow", nil)
	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)
	u := new(user)
	err := c.Bind(u)
	if assert.NoError(t, err) {
		assert.Equal(t, 1, u.ID)
		assert.Equal(t, "Jon Snow", u.Name)
	}
}


Review comment (refactor):
Instead of using `assert.New(t)` refactor all asserts to use `assert.Equal(t, .......)` variants.

Code after:
	testBindArrayOkay(t, strings.NewReader(usersJSON), nil, MIMEApplicationJSON)
	testBindArrayOkay(t, strings.NewReader(usersJSON), dummyQuery, MIMEApplicationJSON)
	testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{})
	testBindError(t, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{})
}

func TestBindXML(t *testing.T) {
	testBindOkay(t, strings.NewReader(userXML), nil, MIMEApplicationXML)
	testBindOkay(t, strings.NewReader(userXML), dummyQuery, MIMEApplicationXML)
	testBindArrayOkay(t, strings.NewReader(userXML), nil, MIMEApplicationXML)
	testBindArrayOkay(t, strings.NewReader(userXML), dummyQuery, MIMEApplicationXML)
	testBindError(t, strings.NewReader(invalidContent), MIMEApplicationXML, errors.New(""))
	testBindError(t, strings.NewReader(userXMLConvertNumberError), MIMEApplicationXML, &strconv.NumError{})
	testBindError(t, strings.NewReader(userXMLUnsupportedTypeError), MIMEApplicationXML, &xml.SyntaxError{})
	testBindOkay(t, strings.NewReader(userXML), nil, MIMETextXML)
	testBindOkay(t, strings.NewReader(userXML), dummyQuery, MIMETextXML)
	testBindError(t, strings.NewReader(invalidContent), MIMETextXML, errors.New(""))
	testBindError(t, strings.NewReader(userXMLConvertNumberError), MIMETextXML, &strconv.NumError{})
	testBindError(t, strings.NewReader(userXMLUnsupportedTypeError), MIMETextXML, &xml.SyntaxError{})
}

func TestBindForm(t *testing.T) {

	testBindOkay(t, strings.NewReader(userForm), nil, MIMEApplicationForm)
	testBindOkay(t, strings.NewReader(userForm), dummyQuery, MIMEApplicationForm)
	e := New()
	req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userForm))
	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)
	req.Header.Set(HeaderContentType, MIMEApplicationForm)
	err := c.Bind(&[]struct{ Field string }{})
	assert.Error(t, err)
}

func TestBindQueryParams(t *testing.T) {
	e := New()
	req := httptest.NewRequest(http.MethodGet, "/?id=1&name=Jon+Snow", nil)
	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)
	u := new(user)
	err := c.Bind(u)
	if assert.NoError(t, err) {
		assert.Equal(t, 1, u.ID)
		assert.Equal(t, "Jon Snow", u.Name)
	}
}

func TestBindQueryParamsCaseInsensitive(t *testing.T) {
	e := New()
	req := httptest.NewRequest(http.MethodGet, "/?ID=1&NAME=Jon+Snow", nil)
	rec := httptest.NewRecorder()