2022-05-28 05:42:28 +03:00
|
|
|
// 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 (
|
2017-07-05 15:37:28 +03:00
|
|
|
"os"
|
2024-05-08 04:13:36 +03:00
|
|
|
"sync/atomic"
|
2015-04-08 15:24:49 +03:00
|
|
|
"testing"
|
|
|
|
|
2017-07-10 11:33:35 +03:00
|
|
|
"github.com/gin-gonic/gin/binding"
|
2015-04-08 15:24:49 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-02-22 07:53:47 +03:00
|
|
|
os.Setenv(EnvGinMode, TestMode)
|
2015-04-08 15:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetMode(t *testing.T) {
|
2024-05-08 04:13:36 +03:00
|
|
|
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, TestMode, Mode())
|
2019-02-22 07:53:47 +03:00
|
|
|
os.Unsetenv(EnvGinMode)
|
2017-07-05 16:05:02 +03:00
|
|
|
|
2022-05-14 04:11:35 +03:00
|
|
|
SetMode("")
|
2024-05-08 04:13:36 +03:00
|
|
|
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
|
2022-05-14 04:11:35 +03:00
|
|
|
assert.Equal(t, TestMode, Mode())
|
|
|
|
|
2015-04-08 15:24:49 +03:00
|
|
|
SetMode(DebugMode)
|
2024-05-08 04:13:36 +03:00
|
|
|
assert.Equal(t, int32(debugCode), atomic.LoadInt32(&ginMode))
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, DebugMode, Mode())
|
2015-04-08 15:24:49 +03:00
|
|
|
|
|
|
|
SetMode(ReleaseMode)
|
2024-05-08 04:13:36 +03:00
|
|
|
assert.Equal(t, int32(releaseCode), atomic.LoadInt32(&ginMode))
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, ReleaseMode, Mode())
|
2015-04-08 15:24:49 +03:00
|
|
|
|
|
|
|
SetMode(TestMode)
|
2024-05-08 04:13:36 +03:00
|
|
|
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, TestMode, Mode())
|
2015-04-08 15:24:49 +03:00
|
|
|
|
|
|
|
assert.Panics(t, func() { SetMode("unknown") })
|
|
|
|
}
|
2017-07-10 11:33:35 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-10 11:33:35 +03:00
|
|
|
func TestEnableJsonDecoderUseNumber(t *testing.T) {
|
|
|
|
assert.False(t, binding.EnableDecoderUseNumber)
|
|
|
|
EnableJsonDecoderUseNumber()
|
|
|
|
assert.True(t, binding.EnableDecoderUseNumber)
|
|
|
|
}
|
2019-09-06 08:56:59 +03:00
|
|
|
|
|
|
|
func TestEnableJsonDecoderDisallowUnknownFields(t *testing.T) {
|
|
|
|
assert.False(t, binding.EnableDecoderDisallowUnknownFields)
|
|
|
|
EnableJsonDecoderDisallowUnknownFields()
|
|
|
|
assert.True(t, binding.EnableDecoderDisallowUnknownFields)
|
|
|
|
}
|