Skip to content

Commit c8fd197

Browse files
ribicevishr
authored andcommitted
Replace http constants with stdlib ones, i.e.: http.MethodGet instead of echo.GET (labstack#1205)
1 parent 059c099 commit c8fd197

33 files changed

+270
-271
lines changed

bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type (
3131
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
3232
req := c.Request()
3333
if req.ContentLength == 0 {
34-
if req.Method == GET || req.Method == DELETE {
34+
if req.Method == http.MethodGet || req.Method == http.MethodDelete {
3535
if err = b.bindData(i, c.QueryParams(), "query"); err != nil {
3636
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
3737
}

bind_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestBindForm(t *testing.T) {
145145
testBindOkay(assert, strings.NewReader(userForm), MIMEApplicationForm)
146146
testBindError(assert, nil, MIMEApplicationForm, nil)
147147
e := New()
148-
req := httptest.NewRequest(POST, "/", strings.NewReader(userForm))
148+
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userForm))
149149
rec := httptest.NewRecorder()
150150
c := e.NewContext(req, rec)
151151
req.Header.Set(HeaderContentType, MIMEApplicationForm)
@@ -155,7 +155,7 @@ func TestBindForm(t *testing.T) {
155155

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

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

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

195195
func TestBindUnmarshalParam(t *testing.T) {
196196
e := New()
197-
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)
197+
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)
198198
rec := httptest.NewRecorder()
199199
c := e.NewContext(req, rec)
200200
result := struct {
@@ -218,7 +218,7 @@ func TestBindUnmarshalParam(t *testing.T) {
218218

219219
func TestBindUnmarshalParamPtr(t *testing.T) {
220220
e := New()
221-
req := httptest.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z", nil)
221+
req := httptest.NewRequest(http.MethodGet, "/?ts=2016-12-06T19:09:05Z", nil)
222222
rec := httptest.NewRecorder()
223223
c := e.NewContext(req, rec)
224224
result := struct {
@@ -257,7 +257,7 @@ func TestBindbindData(t *testing.T) {
257257
func TestBindUnmarshalTypeError(t *testing.T) {
258258
body := bytes.NewBufferString(`{ "id": "text" }`)
259259
e := New()
260-
req := httptest.NewRequest(POST, "/", body)
260+
req := httptest.NewRequest(http.MethodPost, "/", body)
261261
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
262262

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

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

379379
func testBindError(assert *assert.Assertions, r io.Reader, ctype string, expectedInternal error) {
380380
e := New()
381-
req := httptest.NewRequest(POST, "/", r)
381+
req := httptest.NewRequest(http.MethodPost, "/", r)
382382
rec := httptest.NewRecorder()
383383
c := e.NewContext(req, rec)
384384
req.Header.Set(HeaderContentType, ctype)

context_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c Context)
2929

3030
func TestContext(t *testing.T) {
3131
e := New()
32-
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
32+
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
3333
rec := httptest.NewRecorder()
3434
c := e.NewContext(req, rec).(*context)
3535

@@ -73,7 +73,7 @@ func TestContext(t *testing.T) {
7373
}
7474

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

8787
// JSONPretty
8888
rec = httptest.NewRecorder()
@@ -122,7 +122,7 @@ func TestContext(t *testing.T) {
122122
}
123123

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

136136
// XML (error)
137137
rec = httptest.NewRecorder()
@@ -227,7 +227,7 @@ func TestContext(t *testing.T) {
227227

228228
func TestContextCookie(t *testing.T) {
229229
e := New()
230-
req := httptest.NewRequest(GET, "/", nil)
230+
req := httptest.NewRequest(http.MethodGet, "/", nil)
231231
theme := "theme=light"
232232
user := "user=Jon Snow"
233233
req.Header.Add(HeaderCookie, theme)
@@ -276,23 +276,23 @@ func TestContextPath(t *testing.T) {
276276
e := New()
277277
r := e.Router()
278278

279-
r.Add(GET, "/users/:id", nil)
279+
r.Add(http.MethodGet, "/users/:id", nil)
280280
c := e.NewContext(nil, nil)
281-
r.Find(GET, "/users/1", c)
281+
r.Find(http.MethodGet, "/users/1", c)
282282

283283
assert := assert.New(t)
284284

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

287-
r.Add(GET, "/users/:uid/files/:fid", nil)
287+
r.Add(http.MethodGet, "/users/:uid/files/:fid", nil)
288288
c = e.NewContext(nil, nil)
289-
r.Find(GET, "/users/1/files/1", c)
289+
r.Find(http.MethodGet, "/users/1/files/1", c)
290290
assert.Equal("/users/:uid/files/:fid", c.Path())
291291
}
292292

293293
func TestContextPathParam(t *testing.T) {
294294
e := New()
295-
req := httptest.NewRequest(GET, "/", nil)
295+
req := httptest.NewRequest(http.MethodGet, "/", nil)
296296
c := e.NewContext(req, nil)
297297

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

315315
e := New()
316-
req := httptest.NewRequest(POST, "/", strings.NewReader(f.Encode()))
316+
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
317317
req.Header.Add(HeaderContentType, MIMEApplicationForm)
318318
c := e.NewContext(req, nil)
319319

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

@@ -359,7 +359,7 @@ func TestContextFormFile(t *testing.T) {
359359
w.Write([]byte("test"))
360360
}
361361
mr.Close()
362-
req := httptest.NewRequest(POST, "/", buf)
362+
req := httptest.NewRequest(http.MethodPost, "/", buf)
363363
req.Header.Set(HeaderContentType, mr.FormDataContentType())
364364
rec := httptest.NewRecorder()
365365
c := e.NewContext(req, rec)
@@ -375,7 +375,7 @@ func TestContextMultipartForm(t *testing.T) {
375375
mw := multipart.NewWriter(buf)
376376
mw.WriteField("name", "Jon Snow")
377377
mw.Close()
378-
req := httptest.NewRequest(POST, "/", buf)
378+
req := httptest.NewRequest(http.MethodPost, "/", buf)
379379
req.Header.Set(HeaderContentType, mw.FormDataContentType())
380380
rec := httptest.NewRecorder()
381381
c := e.NewContext(req, rec)
@@ -387,7 +387,7 @@ func TestContextMultipartForm(t *testing.T) {
387387

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

411-
r.Add(GET, "/handler", func(Context) error {
411+
r.Add(http.MethodGet, "/handler", func(Context) error {
412412
_, err := b.Write([]byte("handler"))
413413
return err
414414
})
415415
c := e.NewContext(nil, nil)
416-
r.Find(GET, "/handler", c)
416+
r.Find(http.MethodGet, "/handler", c)
417417
c.Handler()(c)
418418
assert.Equal(t, "handler", b.String())
419419
}

echo.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,6 @@ type (
128128
}
129129
)
130130

131-
// HTTP methods
132-
const (
133-
CONNECT = "CONNECT"
134-
DELETE = "DELETE"
135-
GET = "GET"
136-
HEAD = "HEAD"
137-
OPTIONS = "OPTIONS"
138-
PATCH = "PATCH"
139-
POST = "POST"
140-
PROPFIND = "PROPFIND"
141-
PUT = "PUT"
142-
TRACE = "TRACE"
143-
)
144-
145131
// MIME types
146132
const (
147133
MIMEApplicationJSON = "application/json"
@@ -165,6 +151,7 @@ const (
165151

166152
const (
167153
charsetUTF8 = "charset=UTF-8"
154+
PROPFIND = "PROPFIND"
168155
)
169156

170157
// Headers
@@ -234,16 +221,16 @@ ____________________________________O/_______
234221

235222
var (
236223
methods = [...]string{
237-
CONNECT,
238-
DELETE,
239-
GET,
240-
HEAD,
241-
OPTIONS,
242-
PATCH,
243-
POST,
224+
http.MethodConnect,
225+
http.MethodDelete,
226+
http.MethodGet,
227+
http.MethodHead,
228+
http.MethodOptions,
229+
http.MethodPatch,
230+
http.MethodPost,
244231
PROPFIND,
245-
PUT,
246-
TRACE,
232+
http.MethodPut,
233+
http.MethodTrace,
247234
}
248235
)
249236

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

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

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

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

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

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

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

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

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

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

423410
// Any registers a new route for all HTTP methods and path with matching handler

0 commit comments

Comments
 (0)