gin/mode_test.go

70 lines
1.7 KiB
Go
Raw Permalink Normal View History

// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
2015-04-08 15:24:49 +03:00
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"flag"
2017-07-05 15:37:28 +03:00
"os"
2015-04-08 15:24:49 +03:00
"testing"
"github.com/gin-gonic/gin/binding"
2015-04-08 15:24:49 +03:00
"github.com/stretchr/testify/assert"
)
func init() {
os.Setenv(EnvGinMode, TestMode)
2015-04-08 15:24:49 +03:00
}
func TestSetMode(t *testing.T) {
assert.Equal(t, testCode, ginMode)
assert.Equal(t, TestMode, Mode())
os.Unsetenv(EnvGinMode)
2017-07-05 16:05:02 +03:00
SetMode("")
assert.Equal(t, testCode, ginMode)
assert.Equal(t, TestMode, Mode())
tmp := flag.CommandLine
flag.CommandLine = flag.NewFlagSet("", flag.ContinueOnError)
SetMode("")
assert.Equal(t, debugCode, ginMode)
assert.Equal(t, DebugMode, Mode())
flag.CommandLine = tmp
2015-04-08 15:24:49 +03:00
SetMode(DebugMode)
assert.Equal(t, debugCode, ginMode)
assert.Equal(t, DebugMode, Mode())
2015-04-08 15:24:49 +03:00
SetMode(ReleaseMode)
assert.Equal(t, releaseCode, ginMode)
assert.Equal(t, ReleaseMode, Mode())
2015-04-08 15:24:49 +03:00
SetMode(TestMode)
assert.Equal(t, testCode, ginMode)
assert.Equal(t, TestMode, Mode())
2015-04-08 15:24:49 +03:00
assert.Panics(t, func() { SetMode("unknown") })
}
2019-09-24 16:44:15 +03:00
func TestDisableBindValidation(t *testing.T) {
v := binding.Validator
assert.NotNil(t, binding.Validator)
DisableBindValidation()
assert.Nil(t, binding.Validator)
binding.Validator = v
}
func TestEnableJsonDecoderUseNumber(t *testing.T) {
assert.False(t, binding.EnableDecoderUseNumber)
EnableJsonDecoderUseNumber()
assert.True(t, binding.EnableDecoderUseNumber)
}
func TestEnableJsonDecoderDisallowUnknownFields(t *testing.T) {
assert.False(t, binding.EnableDecoderDisallowUnknownFields)
EnableJsonDecoderDisallowUnknownFields()
assert.True(t, binding.EnableDecoderDisallowUnknownFields)
}