config_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package beego
  15. import (
  16. "encoding/json"
  17. "reflect"
  18. "testing"
  19. "github.com/astaxie/beego/config"
  20. )
  21. func TestDefaults(t *testing.T) {
  22. if BConfig.WebConfig.FlashName != "BEEGO_FLASH" {
  23. t.Errorf("FlashName was not set to default.")
  24. }
  25. if BConfig.WebConfig.FlashSeparator != "BEEGOFLASH" {
  26. t.Errorf("FlashName was not set to default.")
  27. }
  28. }
  29. func TestAssignConfig_01(t *testing.T) {
  30. _BConfig := &Config{}
  31. _BConfig.AppName = "beego_test"
  32. jcf := &config.JSONConfig{}
  33. ac, _ := jcf.ParseData([]byte(`{"AppName":"beego_json"}`))
  34. assignSingleConfig(_BConfig, ac)
  35. if _BConfig.AppName != "beego_json" {
  36. t.Log(_BConfig)
  37. t.FailNow()
  38. }
  39. }
  40. func TestAssignConfig_02(t *testing.T) {
  41. _BConfig := &Config{}
  42. bs, _ := json.Marshal(newBConfig())
  43. jsonMap := map[string]interface{}{}
  44. json.Unmarshal(bs, &jsonMap)
  45. configMap := map[string]interface{}{}
  46. for k, v := range jsonMap {
  47. if reflect.TypeOf(v).Kind() == reflect.Map {
  48. for k1, v1 := range v.(map[string]interface{}) {
  49. if reflect.TypeOf(v1).Kind() == reflect.Map {
  50. for k2, v2 := range v1.(map[string]interface{}) {
  51. configMap[k2] = v2
  52. }
  53. } else {
  54. configMap[k1] = v1
  55. }
  56. }
  57. } else {
  58. configMap[k] = v
  59. }
  60. }
  61. configMap["MaxMemory"] = 1024
  62. configMap["Graceful"] = true
  63. configMap["XSRFExpire"] = 32
  64. configMap["SessionProviderConfig"] = "file"
  65. configMap["FileLineNum"] = true
  66. jcf := &config.JSONConfig{}
  67. bs, _ = json.Marshal(configMap)
  68. ac, _ := jcf.ParseData([]byte(bs))
  69. for _, i := range []interface{}{_BConfig, &_BConfig.Listen, &_BConfig.WebConfig, &_BConfig.Log, &_BConfig.WebConfig.Session} {
  70. assignSingleConfig(i, ac)
  71. }
  72. if _BConfig.MaxMemory != 1024 {
  73. t.Log(_BConfig.MaxMemory)
  74. t.FailNow()
  75. }
  76. if !_BConfig.Listen.Graceful {
  77. t.Log(_BConfig.Listen.Graceful)
  78. t.FailNow()
  79. }
  80. if _BConfig.WebConfig.XSRFExpire != 32 {
  81. t.Log(_BConfig.WebConfig.XSRFExpire)
  82. t.FailNow()
  83. }
  84. if _BConfig.WebConfig.Session.SessionProviderConfig != "file" {
  85. t.Log(_BConfig.WebConfig.Session.SessionProviderConfig)
  86. t.FailNow()
  87. }
  88. if !_BConfig.Log.FileLineNum {
  89. t.Log(_BConfig.Log.FileLineNum)
  90. t.FailNow()
  91. }
  92. }
  93. func TestAssignConfig_03(t *testing.T) {
  94. jcf := &config.JSONConfig{}
  95. ac, _ := jcf.ParseData([]byte(`{"AppName":"beego"}`))
  96. ac.Set("AppName", "test_app")
  97. ac.Set("RunMode", "online")
  98. ac.Set("StaticDir", "download:down download2:down2")
  99. ac.Set("StaticExtensionsToGzip", ".css,.js,.html,.jpg,.png")
  100. assignConfig(ac)
  101. t.Logf("%#v", BConfig)
  102. if BConfig.AppName != "test_app" {
  103. t.FailNow()
  104. }
  105. if BConfig.RunMode != "online" {
  106. t.FailNow()
  107. }
  108. if BConfig.WebConfig.StaticDir["/download"] != "down" {
  109. t.FailNow()
  110. }
  111. if BConfig.WebConfig.StaticDir["/download2"] != "down2" {
  112. t.FailNow()
  113. }
  114. if len(BConfig.WebConfig.StaticExtensionsToGzip) != 5 {
  115. t.FailNow()
  116. }
  117. }