...
1
2
3
4
5 package gin
6
7 import (
8 "flag"
9 "os"
10 "testing"
11
12 "github.com/gin-gonic/gin/binding"
13 "github.com/stretchr/testify/assert"
14 )
15
16 func init() {
17 os.Setenv(EnvGinMode, TestMode)
18 }
19
20 func TestSetMode(t *testing.T) {
21 assert.Equal(t, testCode, ginMode)
22 assert.Equal(t, TestMode, Mode())
23 os.Unsetenv(EnvGinMode)
24
25 SetMode("")
26 assert.Equal(t, testCode, ginMode)
27 assert.Equal(t, TestMode, Mode())
28
29 tmp := flag.CommandLine
30 flag.CommandLine = flag.NewFlagSet("", flag.ContinueOnError)
31 SetMode("")
32 assert.Equal(t, debugCode, ginMode)
33 assert.Equal(t, DebugMode, Mode())
34 flag.CommandLine = tmp
35
36 SetMode(DebugMode)
37 assert.Equal(t, debugCode, ginMode)
38 assert.Equal(t, DebugMode, Mode())
39
40 SetMode(ReleaseMode)
41 assert.Equal(t, releaseCode, ginMode)
42 assert.Equal(t, ReleaseMode, Mode())
43
44 SetMode(TestMode)
45 assert.Equal(t, testCode, ginMode)
46 assert.Equal(t, TestMode, Mode())
47
48 assert.Panics(t, func() { SetMode("unknown") })
49 }
50
51 func TestDisableBindValidation(t *testing.T) {
52 v := binding.Validator
53 assert.NotNil(t, binding.Validator)
54 DisableBindValidation()
55 assert.Nil(t, binding.Validator)
56 binding.Validator = v
57 }
58
59 func TestEnableJsonDecoderUseNumber(t *testing.T) {
60 assert.False(t, binding.EnableDecoderUseNumber)
61 EnableJsonDecoderUseNumber()
62 assert.True(t, binding.EnableDecoderUseNumber)
63 }
64
65 func TestEnableJsonDecoderDisallowUnknownFields(t *testing.T) {
66 assert.False(t, binding.EnableDecoderDisallowUnknownFields)
67 EnableJsonDecoderDisallowUnknownFields()
68 assert.True(t, binding.EnableDecoderDisallowUnknownFields)
69 }
70
View as plain text