config_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package config
  5. import (
  6. "os"
  7. "testing"
  8. )
  9. func TestDebugModeOn(t *testing.T) {
  10. os.Clearenv()
  11. os.Setenv("DEBUG", "1")
  12. cfg := NewConfig()
  13. if !cfg.HasDebugMode() {
  14. t.Fatalf(`Unexpected debug mode value, got "%v"`, cfg.HasDebugMode())
  15. }
  16. }
  17. func TestDebugModeOff(t *testing.T) {
  18. os.Clearenv()
  19. cfg := NewConfig()
  20. if cfg.HasDebugMode() {
  21. t.Fatalf(`Unexpected debug mode value, got "%v"`, cfg.HasDebugMode())
  22. }
  23. }
  24. func TestCustomBaseURL(t *testing.T) {
  25. os.Clearenv()
  26. os.Setenv("BASE_URL", "http://example.org")
  27. cfg := NewConfig()
  28. if cfg.BaseURL() != "http://example.org" {
  29. t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
  30. }
  31. if cfg.RootURL() != "http://example.org" {
  32. t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
  33. }
  34. if cfg.BasePath() != "" {
  35. t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
  36. }
  37. }
  38. func TestCustomBaseURLWithTrailingSlash(t *testing.T) {
  39. os.Clearenv()
  40. os.Setenv("BASE_URL", "http://example.org/folder/")
  41. cfg := NewConfig()
  42. if cfg.BaseURL() != "http://example.org/folder" {
  43. t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
  44. }
  45. if cfg.RootURL() != "http://example.org" {
  46. t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
  47. }
  48. if cfg.BasePath() != "/folder" {
  49. t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
  50. }
  51. }
  52. func TestBaseURLWithoutScheme(t *testing.T) {
  53. os.Clearenv()
  54. os.Setenv("BASE_URL", "example.org/folder/")
  55. cfg := NewConfig()
  56. if cfg.BaseURL() != "http://localhost" {
  57. t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
  58. }
  59. if cfg.RootURL() != "http://localhost" {
  60. t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
  61. }
  62. if cfg.BasePath() != "" {
  63. t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
  64. }
  65. }
  66. func TestBaseURLWithInvalidScheme(t *testing.T) {
  67. os.Clearenv()
  68. os.Setenv("BASE_URL", "ftp://example.org/folder/")
  69. cfg := NewConfig()
  70. if cfg.BaseURL() != "http://localhost" {
  71. t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
  72. }
  73. if cfg.RootURL() != "http://localhost" {
  74. t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
  75. }
  76. if cfg.BasePath() != "" {
  77. t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
  78. }
  79. }
  80. func TestDefaultBaseURL(t *testing.T) {
  81. os.Clearenv()
  82. cfg := NewConfig()
  83. if cfg.BaseURL() != "http://localhost" {
  84. t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
  85. }
  86. if cfg.RootURL() != "http://localhost" {
  87. t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
  88. }
  89. if cfg.BasePath() != "" {
  90. t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
  91. }
  92. }
  93. func TestHSTSOn(t *testing.T) {
  94. os.Clearenv()
  95. cfg := NewConfig()
  96. if !cfg.HasHSTS() {
  97. t.Fatalf(`Unexpected HSTS value, got "%v"`, cfg.HasHSTS())
  98. }
  99. }
  100. func TestHSTSOff(t *testing.T) {
  101. os.Clearenv()
  102. os.Setenv("DISABLE_HSTS", "1")
  103. cfg := NewConfig()
  104. if cfg.HasHSTS() {
  105. t.Fatalf(`Unexpected HSTS value, got "%v"`, cfg.HasHSTS())
  106. }
  107. }