chore: add return or remove else for reduce indent (#1470)
This commit is contained in:
parent
7e64d32269
commit
e5bb4f62a2
23
gin.go
23
gin.go
@ -349,7 +349,9 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
||||
// Find root of the tree for the given HTTP method
|
||||
t := engine.trees
|
||||
for i, tl := 0, len(t); i < tl; i++ {
|
||||
if t[i].method == httpMethod {
|
||||
if t[i].method != httpMethod {
|
||||
continue
|
||||
}
|
||||
root := t[i].root
|
||||
// Find route in tree
|
||||
handlers, params, tsr := root.getValue(path, c.Params, unescape)
|
||||
@ -371,11 +373,12 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if engine.HandleMethodNotAllowed {
|
||||
for _, tree := range engine.trees {
|
||||
if tree.method != httpMethod {
|
||||
if tree.method == httpMethod {
|
||||
continue
|
||||
}
|
||||
if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
|
||||
c.handlers = engine.allNoMethod
|
||||
serveError(c, http.StatusMethodNotAllowed, default405Body)
|
||||
@ -383,7 +386,6 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
c.handlers = engine.allNoRoute
|
||||
serveError(c, http.StatusNotFound, default404Body)
|
||||
}
|
||||
@ -393,14 +395,16 @@ var mimePlain = []string{MIMEPlain}
|
||||
func serveError(c *Context, code int, defaultMessage []byte) {
|
||||
c.writermem.status = code
|
||||
c.Next()
|
||||
if !c.writermem.Written() {
|
||||
if c.writermem.Written() {
|
||||
return
|
||||
}
|
||||
if c.writermem.Status() == code {
|
||||
c.writermem.Header()["Content-Type"] = mimePlain
|
||||
c.Writer.Write(defaultMessage)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
c.writermem.WriteHeaderNow()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func redirectTrailingSlash(c *Context) {
|
||||
@ -411,10 +415,9 @@ func redirectTrailingSlash(c *Context) {
|
||||
code = http.StatusTemporaryRedirect
|
||||
}
|
||||
|
||||
req.URL.Path = path + "/"
|
||||
if length := len(path); length > 1 && path[length-1] == '/' {
|
||||
req.URL.Path = path[:length-1]
|
||||
} else {
|
||||
req.URL.Path = path + "/"
|
||||
}
|
||||
debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())
|
||||
http.Redirect(c.Writer, req, req.URL.String(), code)
|
||||
|
@ -333,6 +333,16 @@ func TestRouteNotAllowedEnabled(t *testing.T) {
|
||||
assert.Equal(t, http.StatusTeapot, w.Code)
|
||||
}
|
||||
|
||||
func TestRouteNotAllowedEnabled2(t *testing.T) {
|
||||
router := New()
|
||||
router.HandleMethodNotAllowed = true
|
||||
// add one methodTree to trees
|
||||
router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}})
|
||||
router.GET("/path2", func(c *Context) {})
|
||||
w := performRequest(router, "POST", "/path2")
|
||||
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||
}
|
||||
|
||||
func TestRouteNotAllowedDisabled(t *testing.T) {
|
||||
router := New()
|
||||
router.HandleMethodNotAllowed = false
|
||||
|
Loading…
Reference in New Issue
Block a user