mirror of
				https://github.com/gin-gonic/gin.git
				synced 2025-11-04 17:22:12 +08:00 
			
		
		
		
	* ci: update golangci-lint configuration and lint settings - Update golangci-lint to version 2 - Enable new linters and adjust existing ones - Update lint settings across multiple test files - Remove unused struct and variable checks - Add new lint exclusions for generated code and specific directories Signed-off-by: Flc <four_leaf_clover@foxmail.com> * ci(github): update golangci-lint-action to v8 and lint version to v2.3.4 Signed-off-by: Flc <four_leaf_clover@foxmail.com> * ci: downgrade golangci-lint to v2.1.6 Signed-off-by: Flc <four_leaf_clover@foxmail.com> * ci(golangci): add gofumpt linter and fix related issues- Added gofumpt linter to .golangci.yml Signed-off-by: Flc <four_leaf_clover@foxmail.com> * test: ignore testifylint and gofumpt lints in specific test cases Signed-off-by: Flc <four_leaf_clover@foxmail.com> * build(deps): remove golang.org/x/lint - Remove golang.org/x/lint package from go.mod - Update related dependencies in go.sum Signed-off-by: flc1125 <four_leaf_clover@foxmail.com> * build(deps): downgrade golang.org/x/mod and golang.org/x/tools - Downgrade golang.org/x/mod from v0.24.0 to v0.18.0 - Downgrade golang.org/x/tools from v0.33.0 to v.22.0 These changes are made to address compatibility issues with the current project setup. Signed-off-by: flc1125 <four_leaf_clover@foxmail.com> --------- Signed-off-by: Flc <four_leaf_clover@foxmail.com> Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
		
			
				
	
	
		
			254 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			254 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
 | 
						|
// Use of this source code is governed by a MIT style
 | 
						|
// license that can be found in the LICENSE file.
 | 
						|
 | 
						|
package gin
 | 
						|
 | 
						|
import (
 | 
						|
	"errors"
 | 
						|
	"net/http"
 | 
						|
	"strings"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/gin-contrib/sse"
 | 
						|
	"github.com/stretchr/testify/assert"
 | 
						|
)
 | 
						|
 | 
						|
func TestMiddlewareGeneralCase(t *testing.T) {
 | 
						|
	signature := ""
 | 
						|
	router := New()
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "A"
 | 
						|
		c.Next()
 | 
						|
		signature += "B"
 | 
						|
	})
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "C"
 | 
						|
	})
 | 
						|
	router.GET("/", func(c *Context) {
 | 
						|
		signature += "D"
 | 
						|
	})
 | 
						|
	router.NoRoute(func(c *Context) {
 | 
						|
		signature += " X "
 | 
						|
	})
 | 
						|
	router.NoMethod(func(c *Context) {
 | 
						|
		signature += " XX "
 | 
						|
	})
 | 
						|
	// RUN
 | 
						|
	w := PerformRequest(router, http.MethodGet, "/")
 | 
						|
 | 
						|
	// TEST
 | 
						|
	assert.Equal(t, http.StatusOK, w.Code)
 | 
						|
	assert.Equal(t, "ACDB", signature)
 | 
						|
}
 | 
						|
 | 
						|
func TestMiddlewareNoRoute(t *testing.T) {
 | 
						|
	signature := ""
 | 
						|
	router := New()
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "A"
 | 
						|
		c.Next()
 | 
						|
		signature += "B"
 | 
						|
	})
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "C"
 | 
						|
		c.Next()
 | 
						|
		c.Next()
 | 
						|
		c.Next()
 | 
						|
		c.Next()
 | 
						|
		signature += "D"
 | 
						|
	})
 | 
						|
	router.NoRoute(func(c *Context) {
 | 
						|
		signature += "E"
 | 
						|
		c.Next()
 | 
						|
		signature += "F"
 | 
						|
	}, func(c *Context) {
 | 
						|
		signature += "G"
 | 
						|
		c.Next()
 | 
						|
		signature += "H"
 | 
						|
	})
 | 
						|
	router.NoMethod(func(c *Context) {
 | 
						|
		signature += " X "
 | 
						|
	})
 | 
						|
	// RUN
 | 
						|
	w := PerformRequest(router, http.MethodGet, "/")
 | 
						|
 | 
						|
	// TEST
 | 
						|
	assert.Equal(t, http.StatusNotFound, w.Code)
 | 
						|
	assert.Equal(t, "ACEGHFDB", signature)
 | 
						|
}
 | 
						|
 | 
						|
func TestMiddlewareNoMethodEnabled(t *testing.T) {
 | 
						|
	signature := ""
 | 
						|
	router := New()
 | 
						|
	router.HandleMethodNotAllowed = true
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "A"
 | 
						|
		c.Next()
 | 
						|
		signature += "B"
 | 
						|
	})
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "C"
 | 
						|
		c.Next()
 | 
						|
		signature += "D"
 | 
						|
	})
 | 
						|
	router.NoMethod(func(c *Context) {
 | 
						|
		signature += "E"
 | 
						|
		c.Next()
 | 
						|
		signature += "F"
 | 
						|
	}, func(c *Context) {
 | 
						|
		signature += "G"
 | 
						|
		c.Next()
 | 
						|
		signature += "H"
 | 
						|
	})
 | 
						|
	router.NoRoute(func(c *Context) {
 | 
						|
		signature += " X "
 | 
						|
	})
 | 
						|
	router.POST("/", func(c *Context) {
 | 
						|
		signature += " XX "
 | 
						|
	})
 | 
						|
	// RUN
 | 
						|
	w := PerformRequest(router, http.MethodGet, "/")
 | 
						|
 | 
						|
	// TEST
 | 
						|
	assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
 | 
						|
	assert.Equal(t, "ACEGHFDB", signature)
 | 
						|
}
 | 
						|
 | 
						|
func TestMiddlewareNoMethodDisabled(t *testing.T) {
 | 
						|
	signature := ""
 | 
						|
	router := New()
 | 
						|
 | 
						|
	// NoMethod disabled
 | 
						|
	router.HandleMethodNotAllowed = false
 | 
						|
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "A"
 | 
						|
		c.Next()
 | 
						|
		signature += "B"
 | 
						|
	})
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "C"
 | 
						|
		c.Next()
 | 
						|
		signature += "D"
 | 
						|
	})
 | 
						|
	router.NoMethod(func(c *Context) {
 | 
						|
		signature += "E"
 | 
						|
		c.Next()
 | 
						|
		signature += "F"
 | 
						|
	}, func(c *Context) {
 | 
						|
		signature += "G"
 | 
						|
		c.Next()
 | 
						|
		signature += "H"
 | 
						|
	})
 | 
						|
	router.NoRoute(func(c *Context) {
 | 
						|
		signature += " X "
 | 
						|
	})
 | 
						|
	router.POST("/", func(c *Context) {
 | 
						|
		signature += " XX "
 | 
						|
	})
 | 
						|
 | 
						|
	// RUN
 | 
						|
	w := PerformRequest(router, http.MethodGet, "/")
 | 
						|
 | 
						|
	// TEST
 | 
						|
	assert.Equal(t, http.StatusNotFound, w.Code)
 | 
						|
	assert.Equal(t, "AC X DB", signature)
 | 
						|
}
 | 
						|
 | 
						|
func TestMiddlewareAbort(t *testing.T) {
 | 
						|
	signature := ""
 | 
						|
	router := New()
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "A"
 | 
						|
	})
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "C"
 | 
						|
		c.AbortWithStatus(http.StatusUnauthorized)
 | 
						|
		c.Next()
 | 
						|
		signature += "D"
 | 
						|
	})
 | 
						|
	router.GET("/", func(c *Context) {
 | 
						|
		signature += " X "
 | 
						|
		c.Next()
 | 
						|
		signature += " XX "
 | 
						|
	})
 | 
						|
 | 
						|
	// RUN
 | 
						|
	w := PerformRequest(router, http.MethodGet, "/")
 | 
						|
 | 
						|
	// TEST
 | 
						|
	assert.Equal(t, http.StatusUnauthorized, w.Code)
 | 
						|
	assert.Equal(t, "ACD", signature)
 | 
						|
}
 | 
						|
 | 
						|
func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
 | 
						|
	signature := ""
 | 
						|
	router := New()
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		signature += "A"
 | 
						|
		c.Next()
 | 
						|
		c.AbortWithStatus(http.StatusGone)
 | 
						|
		signature += "B"
 | 
						|
	})
 | 
						|
	router.GET("/", func(c *Context) {
 | 
						|
		signature += "C"
 | 
						|
		c.Next()
 | 
						|
	})
 | 
						|
	// RUN
 | 
						|
	w := PerformRequest(router, http.MethodGet, "/")
 | 
						|
 | 
						|
	// TEST
 | 
						|
	assert.Equal(t, http.StatusGone, w.Code)
 | 
						|
	assert.Equal(t, "ACB", signature)
 | 
						|
}
 | 
						|
 | 
						|
// TestMiddlewareFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
 | 
						|
// as well as Abort
 | 
						|
func TestMiddlewareFailHandlersChain(t *testing.T) {
 | 
						|
	// SETUP
 | 
						|
	signature := ""
 | 
						|
	router := New()
 | 
						|
	router.Use(func(context *Context) {
 | 
						|
		signature += "A"
 | 
						|
		context.AbortWithError(http.StatusInternalServerError, errors.New("foo")) //nolint: errcheck
 | 
						|
	})
 | 
						|
	router.Use(func(context *Context) {
 | 
						|
		signature += "B"
 | 
						|
		context.Next()
 | 
						|
		signature += "C"
 | 
						|
	})
 | 
						|
	// RUN
 | 
						|
	w := PerformRequest(router, http.MethodGet, "/")
 | 
						|
 | 
						|
	// TEST
 | 
						|
	assert.Equal(t, http.StatusInternalServerError, w.Code)
 | 
						|
	assert.Equal(t, "A", signature)
 | 
						|
}
 | 
						|
 | 
						|
func TestMiddlewareWrite(t *testing.T) {
 | 
						|
	router := New()
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		c.String(http.StatusBadRequest, "hola\n")
 | 
						|
	})
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		c.XML(http.StatusBadRequest, H{"foo": "bar"})
 | 
						|
	})
 | 
						|
	router.Use(func(c *Context) {
 | 
						|
		c.JSON(http.StatusBadRequest, H{"foo": "bar"})
 | 
						|
	})
 | 
						|
	router.GET("/", func(c *Context) {
 | 
						|
		c.JSON(http.StatusBadRequest, H{"foo": "bar"})
 | 
						|
	}, func(c *Context) {
 | 
						|
		c.Render(http.StatusBadRequest, sse.Event{
 | 
						|
			Event: "test",
 | 
						|
			Data:  "message",
 | 
						|
		})
 | 
						|
	})
 | 
						|
 | 
						|
	w := PerformRequest(router, http.MethodGet, "/")
 | 
						|
 | 
						|
	assert.Equal(t, http.StatusBadRequest, w.Code)
 | 
						|
	assert.Equal(t, strings.ReplaceAll("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", ""), strings.ReplaceAll(w.Body.String(), " ", ""))
 | 
						|
}
 |