mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-24 22:55:45 +08:00
Compare commits
5 Commits
08b95dd0c0
...
f271fd835b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f271fd835b | ||
|
|
19b877fa50 | ||
|
|
7dbe3741fc | ||
|
|
8ef5a66294 | ||
|
|
670895b7b2 |
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) {
|
||||
|
||||
@ -223,6 +223,10 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
|
||||
}
|
||||
|
||||
file := c.Param("filepath")
|
||||
file = path.Clean("/" + file)[1:]
|
||||
if file == "" {
|
||||
file = "."
|
||||
}
|
||||
// Check if file exists and/or if we have permission to access it
|
||||
f, err := fs.Open(file)
|
||||
if err != nil {
|
||||
|
||||
@ -5,7 +5,9 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@ -646,6 +648,43 @@ func TestRouterStaticFSNotFound(t *testing.T) {
|
||||
assert.Equal(t, "non existent", w.Body.String())
|
||||
}
|
||||
|
||||
//go:embed testdata/embed
|
||||
var embeddedFolder embed.FS
|
||||
|
||||
const embeddedPath = "testdata/embed"
|
||||
|
||||
func TestRouteStaticFSCleansPath(t *testing.T) {
|
||||
router := New()
|
||||
subFS, err := fs.Sub(embeddedFolder, embeddedPath)
|
||||
require.NoError(t, err)
|
||||
fs := &OnlyFilesFS{
|
||||
FileSystem: http.FS(subFS),
|
||||
}
|
||||
router.StaticFS("/", fs)
|
||||
router.NoRoute(func(c *Context) {
|
||||
c.String(http.StatusNotFound, "non existent")
|
||||
})
|
||||
|
||||
w := PerformRequest(router, http.MethodGet, "/tutorials/making-gin/")
|
||||
assert.Contains(t, w.Body.String(), "This is another simple embedded page.")
|
||||
}
|
||||
|
||||
func TestRouteStaticFSNestedJsFile(t *testing.T) {
|
||||
router := New()
|
||||
subFS, err := fs.Sub(embeddedFolder, embeddedPath)
|
||||
require.NoError(t, err)
|
||||
fs := &OnlyFilesFS{
|
||||
FileSystem: http.FS(subFS),
|
||||
}
|
||||
router.StaticFS("/", fs)
|
||||
router.NoRoute(func(c *Context) {
|
||||
c.String(http.StatusNotFound, "non existent")
|
||||
})
|
||||
|
||||
w := PerformRequest(router, http.MethodGet, "/tutorials/making-gin/main.js")
|
||||
assert.Contains(t, w.Body.String(), "console.log(\"This is a simple embedded JavaScript file.\");")
|
||||
}
|
||||
|
||||
func TestRouterStaticFSFileNotFound(t *testing.T) {
|
||||
router := New()
|
||||
|
||||
|
||||
2
testdata/embed/index.html
vendored
Normal file
2
testdata/embed/index.html
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
<!DOCTYPE html>
|
||||
Hello embedded world!
|
||||
2
testdata/embed/tutorials/making-gin/index.html
vendored
Normal file
2
testdata/embed/tutorials/making-gin/index.html
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
<!DOCTYPE html>
|
||||
This is another simple embedded page.
|
||||
1
testdata/embed/tutorials/making-gin/main.js
vendored
Normal file
1
testdata/embed/tutorials/making-gin/main.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
console.log("This is a simple embedded JavaScript file.");
|
||||
Loading…
x
Reference in New Issue
Block a user