Skip to content

Commit 2be8b7c

Browse files
committed
fix: tests regarding GET and the autoHeadInGet flag
1 parent f21a770 commit 2be8b7c

4 files changed

Lines changed: 11 additions & 9 deletions

File tree

echo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func (e *Echo) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) RouteInfo
454454
// to the same path.
455455
func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) RouteInfo {
456456
if e.autoHeadInGet {
457-
_ = e.Add(http.MethodHead, path, h, m...)
457+
_ = e.HEAD(path, h, m...)
458458
}
459459

460460
return e.Add(http.MethodGet, path, h, m...)
@@ -658,7 +658,7 @@ func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...Middl
658658

659659
// Group creates a new router group with prefix and optional group-level middleware.
660660
func (e *Echo) Group(prefix string, m ...MiddlewareFunc) (g *Group) {
661-
g = &Group{prefix: prefix, echo: e}
661+
g = &Group{prefix: prefix, echo: e, autoHeadInGet: true}
662662
g.Use(m...)
663663
return
664664
}

echo_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,9 @@ func TestEchoAutoHead(t *testing.T) {
598598
return c.String(http.StatusTeapot, "OK")
599599
})
600600

601-
assert.Equal(t, http.MethodHead, ri.Method)
601+
assert.Equal(t, http.MethodGet, ri.Method)
602602
assert.Equal(t, "/", ri.Path)
603-
assert.Equal(t, http.MethodHead+":/", ri.Name)
603+
assert.Equal(t, http.MethodGet+":/", ri.Name)
604604
assert.Nil(t, ri.Parameters)
605605

606606
status, body := request(http.MethodHead, "/", e)
@@ -937,7 +937,7 @@ func TestEchoMethodNotAllowed(t *testing.T) {
937937
e.ServeHTTP(rec, req)
938938

939939
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
940-
assert.Equal(t, "OPTIONS, GET", rec.Header().Get(HeaderAllow))
940+
assert.Equal(t, "OPTIONS, GET, HEAD", rec.Header().Get(HeaderAllow))
941941
}
942942

943943
func TestEcho_OnAddRoute(t *testing.T) {
@@ -978,6 +978,7 @@ func TestEcho_OnAddRoute(t *testing.T) {
978978
t.Run(tc.name, func(t *testing.T) {
979979

980980
e := New()
981+
e.AutoHeadCancel()
981982

982983
added := make([]string, 0)
983984
cnt := 0

group.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (g *Group) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) RouteInf
4242
// GET implements `Echo#GET()` for sub-routes within the Group. Panics on error.
4343
func (g *Group) GET(path string, h HandlerFunc, m ...MiddlewareFunc) RouteInfo {
4444
if g.autoHeadInGet {
45-
_ = g.Add(http.MethodHead, path, h, m...)
45+
_ = g.HEAD(path, h, m...)
4646
}
4747

4848
return g.Add(http.MethodGet, path, h, m...)
@@ -115,6 +115,7 @@ func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) (sg *Group) {
115115
m = append(m, g.middleware...)
116116
m = append(m, middleware...)
117117
sg = g.echo.Group(g.prefix+prefix, m...)
118+
sg.autoHeadInGet = true
118119
return
119120
}
120121

group_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@ func TestGroup_AutoHEAD_in_GET(t *testing.T) {
218218
})
219219

220220
assert.Equal(t, true, users.autoHeadInGet)
221-
assert.Equal(t, http.MethodHead, ri.Method)
221+
assert.Equal(t, http.MethodGet, ri.Method)
222222
assert.Equal(t, "/users/activate", ri.Path)
223-
assert.Equal(t, http.MethodHead+":/users/activate", ri.Name)
223+
assert.Equal(t, http.MethodGet+":/users/activate", ri.Name)
224224
assert.Nil(t, ri.Parameters)
225225

226226
status, body := request(http.MethodHead, "/users/activate", e)
227227
assert.Equal(t, http.StatusTeapot, status)
228-
assert.Equal(t, `OK`, body)
228+
assert.Equal(t, "OK", body)
229229
}
230230

231231
func TestGroup_HEAD(t *testing.T) {

0 commit comments

Comments
 (0)