mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-10 21:06:00 +08:00
Compare commits
8 Commits
066b654122
...
7fe24a2c5e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7fe24a2c5e | ||
|
|
19b877fa50 | ||
|
|
3a07160a18 | ||
|
|
14fd129b90 | ||
|
|
312b621908 | ||
|
|
cdaceb18a5 | ||
|
|
c7c51af0ec | ||
|
|
ef93bbeb9b |
@ -82,6 +82,9 @@ type Context struct {
|
||||
// This mutex protects Keys map.
|
||||
mu sync.RWMutex
|
||||
|
||||
// This mutex protects headers map
|
||||
hmu sync.RWMutex
|
||||
|
||||
// Keys is a key/value pair exclusively for the context of each request.
|
||||
Keys map[any]any
|
||||
|
||||
@ -1031,6 +1034,8 @@ func (c *Context) IsWebsocket() bool {
|
||||
}
|
||||
|
||||
func (c *Context) requestHeader(key string) string {
|
||||
c.hmu.RLock()
|
||||
defer c.hmu.RUnlock()
|
||||
return c.Request.Header.Get(key)
|
||||
}
|
||||
|
||||
@ -1060,6 +1065,8 @@ func (c *Context) Status(code int) {
|
||||
// It writes a header in the response.
|
||||
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
|
||||
func (c *Context) Header(key, value string) {
|
||||
c.hmu.Lock()
|
||||
defer c.hmu.Unlock()
|
||||
if value == "" {
|
||||
c.Writer.Header().Del(key)
|
||||
return
|
||||
|
||||
@ -3462,6 +3462,48 @@ func TestContextSetCookieData(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestParallelHeaderAccess(t *testing.T) {
|
||||
t.Parallel()
|
||||
const iterations = 1000
|
||||
const goroutines = 8
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
writerCount int
|
||||
readerCount int
|
||||
}{
|
||||
{"parallel_write_only", goroutines, 0},
|
||||
{"parallel_write_and_read", goroutines / 2, goroutines / 2},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
|
||||
wg := sync.WaitGroup{}
|
||||
for i := 0; i < tc.writerCount; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for range iterations {
|
||||
c.Header("key", "value")
|
||||
}
|
||||
}()
|
||||
}
|
||||
for i := 0; i < tc.readerCount; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for range iterations {
|
||||
_ = c.GetHeader("key")
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMapFromFormData(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
||||
4
debug.go
4
debug.go
@ -15,6 +15,8 @@ import (
|
||||
|
||||
const ginSupportMinGoVer = 24
|
||||
|
||||
var runtimeVersion = runtime.Version()
|
||||
|
||||
// IsDebugging returns true if the framework is running in debug mode.
|
||||
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
||||
func IsDebugging() bool {
|
||||
@ -77,7 +79,7 @@ func getMinVer(v string) (uint64, error) {
|
||||
}
|
||||
|
||||
func debugPrintWARNINGDefault() {
|
||||
if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {
|
||||
if v, e := getMinVer(runtimeVersion); e == nil && v < ginSupportMinGoVer {
|
||||
debugPrint(`[WARNING] Now Gin requires Go 1.24+.
|
||||
|
||||
`)
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@ -21,10 +20,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TODO
|
||||
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||
// func debugPrint(format string, values ...any) {
|
||||
|
||||
func TestIsDebugging(t *testing.T) {
|
||||
SetMode(DebugMode)
|
||||
assert.True(t, IsDebugging())
|
||||
@ -48,6 +43,18 @@ func TestDebugPrint(t *testing.T) {
|
||||
assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintFunc(t *testing.T) {
|
||||
DebugPrintFunc = func(format string, values ...any) {
|
||||
fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
|
||||
}
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrint("debug print func test: %d", 123)
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Regexp(t, `^\[GIN-debug\] debug print func test: 123`, re)
|
||||
}
|
||||
|
||||
func TestDebugPrintError(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
@ -104,12 +111,17 @@ func TestDebugPrintWARNINGDefault(t *testing.T) {
|
||||
debugPrintWARNINGDefault()
|
||||
SetMode(TestMode)
|
||||
})
|
||||
m, e := getMinVer(runtime.Version())
|
||||
if e == nil && m < ginSupportMinGoVer {
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.24+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
} else {
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
}
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintWARNINGDefaultWithUnsupportedVersion(t *testing.T) {
|
||||
runtimeVersion = "go1.23.12"
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintWARNINGDefault()
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.24+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintWARNINGNew(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user