config_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.BaseURL())
  47. }
  48. if cfg.BasePath() != "/folder" {
  49. t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
  50. }
  51. }
  52. func TestDefaultBaseURL(t *testing.T) {
  53. os.Clearenv()
  54. cfg := NewConfig()
  55. if cfg.BaseURL() != "http://localhost" {
  56. t.Fatalf(`Unexpected base URL, got "%s"`, cfg.BaseURL())
  57. }
  58. if cfg.RootURL() != "http://localhost" {
  59. t.Fatalf(`Unexpected root URL, got "%s"`, cfg.RootURL())
  60. }
  61. if cfg.BasePath() != "" {
  62. t.Fatalf(`Unexpected base path, got "%s"`, cfg.BasePath())
  63. }
  64. }
  65. func TestHSTSOn(t *testing.T) {
  66. os.Clearenv()
  67. cfg := NewConfig()
  68. if !cfg.HasHSTS() {
  69. t.Fatalf(`Unexpected HSTS value, got "%v"`, cfg.HasHSTS())
  70. }
  71. }
  72. func TestHSTSOff(t *testing.T) {
  73. os.Clearenv()
  74. os.Setenv("DISABLE_HSTS", "1")
  75. cfg := NewConfig()
  76. if cfg.HasHSTS() {
  77. t.Fatalf(`Unexpected HSTS value, got "%v"`, cfg.HasHSTS())
  78. }
  79. }