config_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. )
  8. const testconf = "testconfig"
  9. const invalidFileName = "**" + string(0x0)
  10. func TestConfig(t *testing.T) {
  11. Config = nil
  12. ioutil.WriteFile(testconf, []byte(`{
  13. "EnableReadOnly": true
  14. }`), 0644)
  15. defer func() {
  16. if err := os.Remove(testconf); err != nil {
  17. fmt.Print("Could not remove test config file:", err.Error())
  18. }
  19. }()
  20. if err := LoadConfigFile(testconf); err != nil {
  21. t.Error(err)
  22. return
  23. }
  24. if res := Str("EnableReadOnly"); res != "true" {
  25. t.Error("Unexpected result:", res)
  26. return
  27. }
  28. if res := Bool("EnableReadOnly"); !res {
  29. t.Error("Unexpected result:", res)
  30. return
  31. }
  32. if res := Int("HTTPSPort"); fmt.Sprint(res) != DefaultConfig[HTTPSPort] {
  33. t.Error("Unexpected result:", res)
  34. return
  35. }
  36. LoadDefaultConfig()
  37. if res := Str("EnableReadOnly"); res != "false" {
  38. t.Error("Unexpected result:", res)
  39. return
  40. }
  41. Config[HTTPSPort] = "123"
  42. if res := Int("HTTPSPort"); fmt.Sprint(res) == DefaultConfig[HTTPSPort] {
  43. t.Error("Unexpected result:", res)
  44. return
  45. }
  46. if res := WebPath("123", "456"); res != "web/123/456" {
  47. t.Error("Unexpected result:", res)
  48. return
  49. }
  50. }