2022-05-28 02:42:28 +00:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
2015-04-08 00:58:35 +00:00
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
2015-05-09 01:34:43 +00:00
"errors"
2020-05-25 13:47:06 +00:00
"fmt"
2017-07-02 07:29:29 +00:00
"html/template"
2015-05-09 01:34:43 +00:00
"io"
"log"
2015-05-23 23:00:17 +00:00
"os"
2018-09-26 05:49:11 +00:00
"runtime"
2023-01-20 01:51:42 +00:00
"strings"
2018-09-19 05:57:00 +00:00
"sync"
2015-04-08 00:58:35 +00:00
"testing"
"github.com/stretchr/testify/assert"
)
2015-04-09 10:15:02 +00:00
// TODO
2015-05-07 09:30:01 +00:00
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
2023-03-01 02:03:48 +00:00
// func debugPrint(format string, values ...any) {
2015-04-09 10:15:02 +00:00
2015-04-08 00:58:35 +00:00
func TestIsDebugging ( t * testing . T ) {
SetMode ( DebugMode )
assert . True ( t , IsDebugging ( ) )
SetMode ( ReleaseMode )
assert . False ( t , IsDebugging ( ) )
SetMode ( TestMode )
assert . False ( t , IsDebugging ( ) )
}
2015-05-09 01:34:43 +00:00
func TestDebugPrint ( t * testing . T ) {
2019-01-18 01:32:53 +00:00
re := captureOutput ( t , func ( ) {
2018-09-19 05:57:00 +00:00
SetMode ( DebugMode )
SetMode ( ReleaseMode )
debugPrint ( "DEBUG this!" )
SetMode ( TestMode )
debugPrint ( "DEBUG this!" )
SetMode ( DebugMode )
2019-02-18 02:10:45 +00:00
debugPrint ( "these are %d %s" , 2 , "error messages" )
2018-09-19 05:57:00 +00:00
SetMode ( TestMode )
} )
assert . Equal ( t , "[GIN-debug] these are 2 error messages\n" , re )
2015-05-09 01:34:43 +00:00
}
func TestDebugPrintError ( t * testing . T ) {
2019-01-18 01:32:53 +00:00
re := captureOutput ( t , func ( ) {
2018-09-19 05:57:00 +00:00
SetMode ( DebugMode )
debugPrintError ( nil )
debugPrintError ( errors . New ( "this is an error" ) )
SetMode ( TestMode )
} )
assert . Equal ( t , "[GIN-debug] [ERROR] this is an error\n" , re )
2015-05-09 01:34:43 +00:00
}
2015-07-12 14:35:15 +00:00
func TestDebugPrintRoutes ( t * testing . T ) {
2019-01-18 01:32:53 +00:00
re := captureOutput ( t , func ( ) {
2018-09-19 05:57:00 +00:00
SetMode ( DebugMode )
debugPrintRoute ( "GET" , "/path/to/route/:param" , HandlersChain { func ( c * Context ) { } , handlerNameTest } )
SetMode ( TestMode )
} )
assert . Regexp ( t , ` ^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$ ` , re )
2015-07-12 14:35:15 +00:00
}
2020-05-25 13:47:06 +00:00
func TestDebugPrintRouteFunc ( t * testing . T ) {
DebugPrintRouteFunc = func ( httpMethod , absolutePath , handlerName string , nuHandlers int ) {
fmt . Fprintf ( DefaultWriter , "[GIN-debug] %-6s %-40s --> %s (%d handlers)\n" , httpMethod , absolutePath , handlerName , nuHandlers )
}
re := captureOutput ( t , func ( ) {
SetMode ( DebugMode )
debugPrintRoute ( "GET" , "/path/to/route/:param1/:param2" , HandlersChain { func ( c * Context ) { } , handlerNameTest } )
SetMode ( TestMode )
} )
assert . Regexp ( t , ` ^\[GIN-debug\] GET /path/to/route/:param1/:param2 --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$ ` , re )
}
2017-07-02 07:29:29 +00:00
func TestDebugPrintLoadTemplate ( t * testing . T ) {
2019-01-18 01:32:53 +00:00
re := captureOutput ( t , func ( ) {
2018-09-19 05:57:00 +00:00
SetMode ( DebugMode )
templ := template . Must ( template . New ( "" ) . Delims ( "{[{" , "}]}" ) . ParseGlob ( "./testdata/template/hello.tmpl" ) )
debugPrintLoadTemplate ( templ )
SetMode ( TestMode )
} )
assert . Regexp ( t , ` ^\[GIN-debug\] Loaded HTML Templates \(2\): \n(\t- \n|\t- hello\.tmpl\n) { 2}\n ` , re )
2017-07-02 07:29:29 +00:00
}
func TestDebugPrintWARNINGSetHTMLTemplate ( t * testing . T ) {
2019-01-18 01:32:53 +00:00
re := captureOutput ( t , func ( ) {
2018-09-19 05:57:00 +00:00
SetMode ( DebugMode )
debugPrintWARNINGSetHTMLTemplate ( )
SetMode ( TestMode )
} )
assert . Equal ( t , "[GIN-debug] [WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called\nat initialization. ie. before any route is registered or the router is listening in a socket:\n\n\trouter := gin.Default()\n\trouter.SetHTMLTemplate(template) // << good place\n\n" , re )
2017-07-02 07:29:29 +00:00
}
2017-09-29 03:58:57 +00:00
func TestDebugPrintWARNINGDefault ( t * testing . T ) {
2019-01-18 01:32:53 +00:00
re := captureOutput ( t , func ( ) {
2018-09-19 05:57:00 +00:00
SetMode ( DebugMode )
debugPrintWARNINGDefault ( )
SetMode ( TestMode )
} )
2018-09-26 05:49:11 +00:00
m , e := getMinVer ( runtime . Version ( ) )
2022-07-01 02:31:31 +00:00
if e == nil && m < ginSupportMinGoVer {
2023-03-01 02:03:48 +00:00
assert . Equal ( t , "[GIN-debug] [WARNING] Now Gin requires Go 1.18+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n" , re )
2018-09-26 05:49:11 +00:00
} else {
assert . Equal ( t , "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n" , re )
}
2017-09-29 03:58:57 +00:00
}
func TestDebugPrintWARNINGNew ( t * testing . T ) {
2019-01-18 01:32:53 +00:00
re := captureOutput ( t , func ( ) {
2018-09-19 05:57:00 +00:00
SetMode ( DebugMode )
debugPrintWARNINGNew ( )
SetMode ( TestMode )
} )
assert . Equal ( t , "[GIN-debug] [WARNING] Running in \"debug\" mode. Switch to \"release\" mode in production.\n - using env:\texport GIN_MODE=release\n - using code:\tgin.SetMode(gin.ReleaseMode)\n\n" , re )
2017-09-29 03:58:57 +00:00
}
2019-01-18 01:32:53 +00:00
func captureOutput ( t * testing . T , f func ( ) ) string {
2018-09-19 05:57:00 +00:00
reader , writer , err := os . Pipe ( )
if err != nil {
panic ( err )
}
2019-05-10 06:03:25 +00:00
defaultWriter := DefaultWriter
defaultErrorWriter := DefaultErrorWriter
2018-09-19 05:57:00 +00:00
defer func ( ) {
2019-05-10 06:03:25 +00:00
DefaultWriter = defaultWriter
DefaultErrorWriter = defaultErrorWriter
2018-09-19 05:57:00 +00:00
log . SetOutput ( os . Stderr )
} ( )
2019-05-10 06:03:25 +00:00
DefaultWriter = writer
DefaultErrorWriter = writer
2018-09-19 05:57:00 +00:00
log . SetOutput ( writer )
out := make ( chan string )
wg := new ( sync . WaitGroup )
wg . Add ( 1 )
go func ( ) {
2023-01-20 01:51:42 +00:00
var buf strings . Builder
2018-09-19 05:57:00 +00:00
wg . Done ( )
2019-01-18 01:32:53 +00:00
_ , err := io . Copy ( & buf , reader )
assert . NoError ( t , err )
2018-09-19 05:57:00 +00:00
out <- buf . String ( )
} ( )
wg . Wait ( )
f ( )
writer . Close ( )
return <- out
2015-05-09 01:34:43 +00:00
}
2018-09-26 05:49:11 +00:00
func TestGetMinVer ( t * testing . T ) {
var m uint64
var e error
_ , e = getMinVer ( "go1" )
assert . NotNil ( t , e )
m , e = getMinVer ( "go1.1" )
assert . Equal ( t , uint64 ( 1 ) , m )
assert . Nil ( t , e )
m , e = getMinVer ( "go1.1.1" )
assert . Nil ( t , e )
assert . Equal ( t , uint64 ( 1 ) , m )
_ , e = getMinVer ( "go1.1.1.1" )
assert . NotNil ( t , e )
}