Skip to content

Commit

Permalink
Replace http constants with stdlib ones, i.e.: http.MethodGet instead…
Browse files Browse the repository at this point in the history
… of echo.GET (labstack#1205)
  • Loading branch information
ribice authored and vishr committed Oct 14, 2018
1 parent 059c099 commit c8fd197
Show file tree
Hide file tree
Showing 33 changed files with 270 additions and 271 deletions.
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type (
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
req := c.Request()
if req.ContentLength == 0 {
if req.Method == GET || req.Method == DELETE {
if req.Method == http.MethodGet || req.Method == http.MethodDelete {
if err = b.bindData(i, c.QueryParams(), "query"); err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
}
Expand Down
18 changes: 9 additions & 9 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestBindForm(t *testing.T) {
testBindOkay(assert, strings.NewReader(userForm), MIMEApplicationForm)
testBindError(assert, nil, MIMEApplicationForm, nil)
e := New()
req := httptest.NewRequest(POST, "/", strings.NewReader(userForm))
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userForm))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, MIMEApplicationForm)
Expand All @@ -155,7 +155,7 @@ func TestBindForm(t *testing.T) {

func TestBindQueryParams(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?id=1&name=Jon+Snow", nil)
req := httptest.NewRequest(http.MethodGet, "/?id=1&name=Jon+Snow", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
u := new(user)
Expand All @@ -168,7 +168,7 @@ func TestBindQueryParams(t *testing.T) {

func TestBindQueryParamsCaseInsensitive(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?ID=1&NAME=Jon+Snow", nil)
req := httptest.NewRequest(http.MethodGet, "/?ID=1&NAME=Jon+Snow", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
u := new(user)
Expand All @@ -181,7 +181,7 @@ func TestBindQueryParamsCaseInsensitive(t *testing.T) {

func TestBindQueryParamsCaseSensitivePrioritized(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?id=1&ID=2&NAME=Jon+Snow&name=Jon+Doe", nil)
req := httptest.NewRequest(http.MethodGet, "/?id=1&ID=2&NAME=Jon+Snow&name=Jon+Doe", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
u := new(user)
Expand All @@ -194,7 +194,7 @@ func TestBindQueryParamsCaseSensitivePrioritized(t *testing.T) {

func TestBindUnmarshalParam(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z&sa=one,two,three&ta=2016-12-06T19:09:05Z&ta=2016-12-06T19:09:05Z&ST=baz", nil)
req := httptest.NewRequest(http.MethodGet, "/?ts=2016-12-06T19:09:05Z&sa=one,two,three&ta=2016-12-06T19:09:05Z&ta=2016-12-06T19:09:05Z&ST=baz", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
result := struct {
Expand All @@ -218,7 +218,7 @@ func TestBindUnmarshalParam(t *testing.T) {

func TestBindUnmarshalParamPtr(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z", nil)
req := httptest.NewRequest(http.MethodGet, "/?ts=2016-12-06T19:09:05Z", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
result := struct {
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestBindbindData(t *testing.T) {
func TestBindUnmarshalTypeError(t *testing.T) {
body := bytes.NewBufferString(`{ "id": "text" }`)
e := New()
req := httptest.NewRequest(POST, "/", body)
req := httptest.NewRequest(http.MethodPost, "/", body)
req.Header.Set(HeaderContentType, MIMEApplicationJSON)

rec := httptest.NewRecorder()
Expand Down Expand Up @@ -364,7 +364,7 @@ func assertBindTestStruct(a *assert.Assertions, ts *bindTestStruct) {

func testBindOkay(assert *assert.Assertions, r io.Reader, ctype string) {
e := New()
req := httptest.NewRequest(POST, "/", r)
req := httptest.NewRequest(http.MethodPost, "/", r)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, ctype)
Expand All @@ -378,7 +378,7 @@ func testBindOkay(assert *assert.Assertions, r io.Reader, ctype string) {

func testBindError(assert *assert.Assertions, r io.Reader, ctype string, expectedInternal error) {
e := New()
req := httptest.NewRequest(POST, "/", r)
req := httptest.NewRequest(http.MethodPost, "/", r)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
req.Header.Set(HeaderContentType, ctype)
Expand Down
36 changes: 18 additions & 18 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c Context)

func TestContext(t *testing.T) {
e := New()
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
rec := httptest.NewRecorder()
c := e.NewContext(req, rec).(*context)

Expand Down Expand Up @@ -73,7 +73,7 @@ func TestContext(t *testing.T) {
}

// JSON with "?pretty"
req = httptest.NewRequest(GET, "/?pretty", nil)
req = httptest.NewRequest(http.MethodGet, "/?pretty", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context)
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
Expand All @@ -82,7 +82,7 @@ func TestContext(t *testing.T) {
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(userJSONPretty, rec.Body.String())
}
req = httptest.NewRequest(GET, "/", nil) // reset
req = httptest.NewRequest(http.MethodGet, "/", nil) // reset

// JSONPretty
rec = httptest.NewRecorder()
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestContext(t *testing.T) {
}

// XML with "?pretty"
req = httptest.NewRequest(GET, "/?pretty", nil)
req = httptest.NewRequest(http.MethodGet, "/?pretty", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context)
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
Expand All @@ -131,7 +131,7 @@ func TestContext(t *testing.T) {
assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(xml.Header+userXMLPretty, rec.Body.String())
}
req = httptest.NewRequest(GET, "/", nil)
req = httptest.NewRequest(http.MethodGet, "/", nil)

// XML (error)
rec = httptest.NewRecorder()
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestContext(t *testing.T) {

func TestContextCookie(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/", nil)
req := httptest.NewRequest(http.MethodGet, "/", nil)
theme := "theme=light"
user := "user=Jon Snow"
req.Header.Add(HeaderCookie, theme)
Expand Down Expand Up @@ -276,23 +276,23 @@ func TestContextPath(t *testing.T) {
e := New()
r := e.Router()

r.Add(GET, "/users/:id", nil)
r.Add(http.MethodGet, "/users/:id", nil)
c := e.NewContext(nil, nil)
r.Find(GET, "/users/1", c)
r.Find(http.MethodGet, "/users/1", c)

assert := assert.New(t)

assert.Equal("/users/:id", c.Path())

r.Add(GET, "/users/:uid/files/:fid", nil)
r.Add(http.MethodGet, "/users/:uid/files/:fid", nil)
c = e.NewContext(nil, nil)
r.Find(GET, "/users/1/files/1", c)
r.Find(http.MethodGet, "/users/1/files/1", c)
assert.Equal("/users/:uid/files/:fid", c.Path())
}

func TestContextPathParam(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/", nil)
req := httptest.NewRequest(http.MethodGet, "/", nil)
c := e.NewContext(req, nil)

// ParamNames
Expand All @@ -313,7 +313,7 @@ func TestContextFormValue(t *testing.T) {
f.Set("email", "[email protected]")

e := New()
req := httptest.NewRequest(POST, "/", strings.NewReader(f.Encode()))
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
req.Header.Add(HeaderContentType, MIMEApplicationForm)
c := e.NewContext(req, nil)

Expand All @@ -335,7 +335,7 @@ func TestContextQueryParam(t *testing.T) {
q := make(url.Values)
q.Set("name", "Jon Snow")
q.Set("email", "[email protected]")
req := httptest.NewRequest(GET, "/?"+q.Encode(), nil)
req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
e := New()
c := e.NewContext(req, nil)

Expand All @@ -359,7 +359,7 @@ func TestContextFormFile(t *testing.T) {
w.Write([]byte("test"))
}
mr.Close()
req := httptest.NewRequest(POST, "/", buf)
req := httptest.NewRequest(http.MethodPost, "/", buf)
req.Header.Set(HeaderContentType, mr.FormDataContentType())
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
Expand All @@ -375,7 +375,7 @@ func TestContextMultipartForm(t *testing.T) {
mw := multipart.NewWriter(buf)
mw.WriteField("name", "Jon Snow")
mw.Close()
req := httptest.NewRequest(POST, "/", buf)
req := httptest.NewRequest(http.MethodPost, "/", buf)
req.Header.Set(HeaderContentType, mw.FormDataContentType())
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
Expand All @@ -387,7 +387,7 @@ func TestContextMultipartForm(t *testing.T) {

func TestContextRedirect(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/", nil)
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
Expand All @@ -408,12 +408,12 @@ func TestContextHandler(t *testing.T) {
r := e.Router()
b := new(bytes.Buffer)

r.Add(GET, "/handler", func(Context) error {
r.Add(http.MethodGet, "/handler", func(Context) error {
_, err := b.Write([]byte("handler"))
return err
})
c := e.NewContext(nil, nil)
r.Find(GET, "/handler", c)
r.Find(http.MethodGet, "/handler", c)
c.Handler()(c)
assert.Equal(t, "handler", b.String())
}
53 changes: 20 additions & 33 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,6 @@ type (
}
)

// HTTP methods
const (
CONNECT = "CONNECT"
DELETE = "DELETE"
GET = "GET"
HEAD = "HEAD"
OPTIONS = "OPTIONS"
PATCH = "PATCH"
POST = "POST"
PROPFIND = "PROPFIND"
PUT = "PUT"
TRACE = "TRACE"
)

// MIME types
const (
MIMEApplicationJSON = "application/json"
Expand All @@ -165,6 +151,7 @@ const (

const (
charsetUTF8 = "charset=UTF-8"
PROPFIND = "PROPFIND"
)

// Headers
Expand Down Expand Up @@ -234,16 +221,16 @@ ____________________________________O/_______

var (
methods = [...]string{
CONNECT,
DELETE,
GET,
HEAD,
OPTIONS,
PATCH,
POST,
http.MethodConnect,
http.MethodDelete,
http.MethodGet,
http.MethodHead,
http.MethodOptions,
http.MethodPatch,
http.MethodPost,
PROPFIND,
PUT,
TRACE,
http.MethodPut,
http.MethodTrace,
}
)

Expand Down Expand Up @@ -345,7 +332,7 @@ func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {

// Send response
if !c.Response().Committed {
if c.Request().Method == HEAD { // Issue #608
if c.Request().Method == http.MethodHead { // Issue #608
err = c.NoContent(code)
} else {
err = c.JSON(code, msg)
Expand All @@ -369,55 +356,55 @@ func (e *Echo) Use(middleware ...MiddlewareFunc) {
// CONNECT registers a new CONNECT route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(CONNECT, path, h, m...)
return e.Add(http.MethodConnect, path, h, m...)
}

// DELETE registers a new DELETE route for a path with matching handler in the router
// with optional route-level middleware.
func (e *Echo) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(DELETE, path, h, m...)
return e.Add(http.MethodDelete, path, h, m...)
}

// GET registers a new GET route for a path with matching handler in the router
// with optional route-level middleware.
func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(GET, path, h, m...)
return e.Add(http.MethodGet, path, h, m...)
}

// HEAD registers a new HEAD route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(HEAD, path, h, m...)
return e.Add(http.MethodHead, path, h, m...)
}

// OPTIONS registers a new OPTIONS route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(OPTIONS, path, h, m...)
return e.Add(http.MethodOptions, path, h, m...)
}

// PATCH registers a new PATCH route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(PATCH, path, h, m...)
return e.Add(http.MethodPatch, path, h, m...)
}

// POST registers a new POST route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(POST, path, h, m...)
return e.Add(http.MethodPost, path, h, m...)
}

// PUT registers a new PUT route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(PUT, path, h, m...)
return e.Add(http.MethodPut, path, h, m...)
}

// TRACE registers a new TRACE route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return e.Add(TRACE, path, h, m...)
return e.Add(http.MethodTrace, path, h, m...)
}

// Any registers a new route for all HTTP methods and path with matching handler
Expand Down
Loading

0 comments on commit c8fd197

Please sign in to comment.