2015-04-08 03:58:35 +03:00
// 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 (
2015-05-09 04:34:43 +03:00
"bytes"
"errors"
2017-07-02 10:29:29 +03:00
"html/template"
2015-05-09 04:34:43 +03:00
"io"
"log"
2015-05-24 02:00:17 +03:00
"os"
2015-04-08 03:58:35 +03:00
"testing"
"github.com/stretchr/testify/assert"
)
2015-04-09 13:15:02 +03:00
// TODO
2015-05-07 12:30:01 +03:00
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
2015-04-09 13:15:02 +03:00
// func debugPrint(format string, values ...interface{}) {
2015-04-08 03:58:35 +03: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 04:34:43 +03:00
func TestDebugPrint ( t * testing . T ) {
var w bytes . Buffer
setup ( & w )
defer teardown ( )
SetMode ( ReleaseMode )
debugPrint ( "DEBUG this!" )
SetMode ( TestMode )
debugPrint ( "DEBUG this!" )
assert . Empty ( t , w . String ( ) )
SetMode ( DebugMode )
debugPrint ( "these are %d %s\n" , 2 , "error messages" )
2017-08-04 08:45:59 +03:00
assert . Equal ( t , "[GIN-debug] these are 2 error messages\n" , w . String ( ) )
2015-05-09 04:34:43 +03:00
}
func TestDebugPrintError ( t * testing . T ) {
var w bytes . Buffer
setup ( & w )
defer teardown ( )
SetMode ( DebugMode )
debugPrintError ( nil )
assert . Empty ( t , w . String ( ) )
debugPrintError ( errors . New ( "this is an error" ) )
2017-08-04 08:45:59 +03:00
assert . Equal ( t , "[GIN-debug] [ERROR] this is an error\n" , w . String ( ) )
2015-05-09 04:34:43 +03:00
}
2015-07-12 17:35:15 +03:00
func TestDebugPrintRoutes ( t * testing . T ) {
var w bytes . Buffer
setup ( & w )
defer teardown ( )
debugPrintRoute ( "GET" , "/path/to/route/:param" , HandlersChain { func ( c * Context ) { } , handlerNameTest } )
2016-01-27 05:40:52 +03:00
assert . Regexp ( t , ` ^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$ ` , w . String ( ) )
2015-07-12 17:35:15 +03:00
}
2017-07-02 10:29:29 +03:00
func TestDebugPrintLoadTemplate ( t * testing . T ) {
var w bytes . Buffer
setup ( & w )
defer teardown ( )
2018-08-12 18:38:31 +03:00
templ := template . Must ( template . New ( "" ) . Delims ( "{[{" , "}]}" ) . ParseGlob ( "./testdata/template/hello.tmpl" ) )
2017-07-02 10:29:29 +03:00
debugPrintLoadTemplate ( templ )
2017-07-04 11:36:12 +03:00
assert . Regexp ( t , ` ^\[GIN-debug\] Loaded HTML Templates \(2\): \n(\t- \n|\t- hello\.tmpl\n) { 2}\n ` , w . String ( ) )
2017-07-02 10:29:29 +03:00
}
func TestDebugPrintWARNINGSetHTMLTemplate ( t * testing . T ) {
var w bytes . Buffer
setup ( & w )
defer teardown ( )
debugPrintWARNINGSetHTMLTemplate ( )
2017-08-04 08:45:59 +03:00
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" , w . String ( ) )
2017-07-02 10:29:29 +03:00
}
2017-09-29 06:58:57 +03:00
func TestDebugPrintWARNINGDefault ( t * testing . T ) {
var w bytes . Buffer
setup ( & w )
defer teardown ( )
debugPrintWARNINGDefault ( )
2018-06-26 13:56:43 +03:00
assert . Equal ( t , "[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n" , w . String ( ) )
2017-09-29 06:58:57 +03:00
}
func TestDebugPrintWARNINGNew ( t * testing . T ) {
var w bytes . Buffer
setup ( & w )
defer teardown ( )
debugPrintWARNINGNew ( )
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" , w . String ( ) )
}
2015-05-09 04:34:43 +03:00
func setup ( w io . Writer ) {
SetMode ( DebugMode )
2015-05-24 02:00:17 +03:00
log . SetOutput ( w )
2015-05-09 04:34:43 +03:00
}
func teardown ( ) {
SetMode ( TestMode )
2015-05-24 02:00:17 +03:00
log . SetOutput ( os . Stdout )
2015-05-09 04:34:43 +03:00
}