mode_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "os"
  7. "testing"
  8. "github.com/gin-gonic/gin/binding"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func init() {
  12. os.Setenv(EnvGinMode, TestMode)
  13. }
  14. func TestSetMode(t *testing.T) {
  15. assert.Equal(t, testCode, ginMode)
  16. assert.Equal(t, TestMode, Mode())
  17. os.Unsetenv(EnvGinMode)
  18. SetMode("")
  19. assert.Equal(t, debugCode, ginMode)
  20. assert.Equal(t, DebugMode, Mode())
  21. SetMode(DebugMode)
  22. assert.Equal(t, debugCode, ginMode)
  23. assert.Equal(t, DebugMode, Mode())
  24. SetMode(ReleaseMode)
  25. assert.Equal(t, releaseCode, ginMode)
  26. assert.Equal(t, ReleaseMode, Mode())
  27. SetMode(TestMode)
  28. assert.Equal(t, testCode, ginMode)
  29. assert.Equal(t, TestMode, Mode())
  30. assert.Panics(t, func() { SetMode("unknown") })
  31. }
  32. func TestDisableBindValidation(t *testing.T) {
  33. v := binding.Validator
  34. assert.NotNil(t, binding.Validator)
  35. DisableBindValidation()
  36. assert.Nil(t, binding.Validator)
  37. binding.Validator = v
  38. }
  39. func TestEnableJsonDecoderUseNumber(t *testing.T) {
  40. assert.False(t, binding.EnableDecoderUseNumber)
  41. EnableJsonDecoderUseNumber()
  42. assert.True(t, binding.EnableDecoderUseNumber)
  43. }
  44. func TestEnableJsonDecoderDisallowUnknownFields(t *testing.T) {
  45. assert.False(t, binding.EnableDecoderDisallowUnknownFields)
  46. EnableJsonDecoderDisallowUnknownFields()
  47. assert.True(t, binding.EnableDecoderDisallowUnknownFields)
  48. }