|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | +) |
| 11 | + |
| 12 | +func TestHeadersConfig_Loading(t *testing.T) { |
| 13 | + yamlConfig := ` |
| 14 | +server: |
| 15 | + port: 8080 |
| 16 | + host: "0.0.0.0" |
| 17 | + read_timeout: 30s |
| 18 | + write_timeout: 30s |
| 19 | + shutdown_timeout: 10s |
| 20 | + log_level: "info" |
| 21 | +
|
| 22 | +redis: |
| 23 | + address: "localhost:6379" |
| 24 | + password: "" |
| 25 | + db: 0 |
| 26 | + dial_timeout: 5s |
| 27 | + read_timeout: 3s |
| 28 | + write_timeout: 3s |
| 29 | + pool_size: 10 |
| 30 | +
|
| 31 | +leader: |
| 32 | + lock_key: "lab:leader:lock" |
| 33 | + lock_ttl: 30s |
| 34 | + renew_interval: 10s |
| 35 | + retry_interval: 5s |
| 36 | +
|
| 37 | +cartographoor: |
| 38 | + source_url: "https://example.com/networks.json" |
| 39 | + refresh_interval: 5m |
| 40 | + request_timeout: 30s |
| 41 | + networks_ttl: 0s |
| 42 | +
|
| 43 | +bounds: |
| 44 | + refresh_interval: 10s |
| 45 | + request_timeout: 30s |
| 46 | + bounds_ttl: 0s |
| 47 | +
|
| 48 | +rate_limiting: |
| 49 | + enabled: false |
| 50 | +
|
| 51 | +headers: |
| 52 | + policies: |
| 53 | + - name: "static_assets" |
| 54 | + path_pattern: "\\.(js|css|png)$" |
| 55 | + headers: |
| 56 | + Cache-Control: "public, max-age=31536000, immutable" |
| 57 | + Vary: "Accept-Encoding" |
| 58 | +
|
| 59 | + - name: "api_config" |
| 60 | + path_pattern: "^/api/v1/config$" |
| 61 | + headers: |
| 62 | + Cache-Control: "public, max-age=1, s-maxage=60" |
| 63 | +
|
| 64 | + - name: "default" |
| 65 | + path_pattern: ".*" |
| 66 | + headers: |
| 67 | + Cache-Control: "public, max-age=1" |
| 68 | +` |
| 69 | + |
| 70 | + var cfg Config |
| 71 | + |
| 72 | + err := yaml.Unmarshal([]byte(yamlConfig), &cfg) |
| 73 | + require.NoError(t, err) |
| 74 | + |
| 75 | + // Verify headers config loaded |
| 76 | + assert.Len(t, cfg.Headers.Policies, 3) |
| 77 | + |
| 78 | + // Verify first policy |
| 79 | + assert.Equal(t, "static_assets", cfg.Headers.Policies[0].Name) |
| 80 | + assert.Equal(t, `\.(js|css|png)$`, cfg.Headers.Policies[0].PathPattern) |
| 81 | + assert.Len(t, cfg.Headers.Policies[0].Headers, 2) |
| 82 | + assert.Equal(t, "public, max-age=31536000, immutable", cfg.Headers.Policies[0].Headers["Cache-Control"]) |
| 83 | + assert.Equal(t, "Accept-Encoding", cfg.Headers.Policies[0].Headers["Vary"]) |
| 84 | + |
| 85 | + // Verify second policy |
| 86 | + assert.Equal(t, "api_config", cfg.Headers.Policies[1].Name) |
| 87 | + assert.Equal(t, "^/api/v1/config$", cfg.Headers.Policies[1].PathPattern) |
| 88 | + assert.Len(t, cfg.Headers.Policies[1].Headers, 1) |
| 89 | + assert.Equal(t, "public, max-age=1, s-maxage=60", cfg.Headers.Policies[1].Headers["Cache-Control"]) |
| 90 | + |
| 91 | + // Verify third policy |
| 92 | + assert.Equal(t, "default", cfg.Headers.Policies[2].Name) |
| 93 | + assert.Equal(t, ".*", cfg.Headers.Policies[2].PathPattern) |
| 94 | +} |
| 95 | + |
| 96 | +func TestExampleConfig_LoadsSuccessfully(t *testing.T) { |
| 97 | + // Load the actual config.example.yaml file |
| 98 | + cfg, err := Load("../../config.example.yaml") |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + // Verify headers section exists and has policies |
| 102 | + assert.NotEmpty(t, cfg.Headers.Policies, "config.example.yaml should have header policies") |
| 103 | + |
| 104 | + // Log what we found |
| 105 | + t.Logf("Loaded %d header policies from config.example.yaml", len(cfg.Headers.Policies)) |
| 106 | + |
| 107 | + for i, policy := range cfg.Headers.Policies { |
| 108 | + t.Logf(" Policy %d: %s with %d headers", i+1, policy.Name, len(policy.Headers)) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestHeadersConfig_EmptyPolicies(t *testing.T) { |
| 113 | + yamlConfig := ` |
| 114 | +server: |
| 115 | + port: 8080 |
| 116 | + host: "0.0.0.0" |
| 117 | + read_timeout: 30s |
| 118 | + write_timeout: 30s |
| 119 | + shutdown_timeout: 10s |
| 120 | + log_level: "info" |
| 121 | +
|
| 122 | +redis: |
| 123 | + address: "localhost:6379" |
| 124 | + dial_timeout: 5s |
| 125 | + pool_size: 10 |
| 126 | +
|
| 127 | +leader: |
| 128 | + lock_key: "lab:leader:lock" |
| 129 | + lock_ttl: 30s |
| 130 | + renew_interval: 10s |
| 131 | + retry_interval: 5s |
| 132 | +
|
| 133 | +cartographoor: |
| 134 | + source_url: "https://example.com/networks.json" |
| 135 | + refresh_interval: 5m |
| 136 | + request_timeout: 30s |
| 137 | +
|
| 138 | +bounds: |
| 139 | + refresh_interval: 10s |
| 140 | + request_timeout: 30s |
| 141 | +
|
| 142 | +rate_limiting: |
| 143 | + enabled: false |
| 144 | +
|
| 145 | +headers: |
| 146 | + policies: [] |
| 147 | +` |
| 148 | + |
| 149 | + var cfg Config |
| 150 | + |
| 151 | + err := yaml.Unmarshal([]byte(yamlConfig), &cfg) |
| 152 | + require.NoError(t, err) |
| 153 | + |
| 154 | + // Empty policies should be fine |
| 155 | + assert.Empty(t, cfg.Headers.Policies) |
| 156 | +} |
| 157 | + |
| 158 | +func TestHeadersConfig_NoHeadersSection(t *testing.T) { |
| 159 | + // Create minimal config without headers section |
| 160 | + tmpFile, err := os.CreateTemp("", "config-*.yaml") |
| 161 | + require.NoError(t, err) |
| 162 | + |
| 163 | + defer os.Remove(tmpFile.Name()) |
| 164 | + |
| 165 | + yamlConfig := ` |
| 166 | +server: |
| 167 | + port: 8080 |
| 168 | + host: "0.0.0.0" |
| 169 | + read_timeout: 30s |
| 170 | + write_timeout: 30s |
| 171 | + shutdown_timeout: 10s |
| 172 | + log_level: "info" |
| 173 | +
|
| 174 | +redis: |
| 175 | + address: "localhost:6379" |
| 176 | + dial_timeout: 5s |
| 177 | + read_timeout: 3s |
| 178 | + write_timeout: 3s |
| 179 | + pool_size: 10 |
| 180 | +
|
| 181 | +leader: |
| 182 | + lock_key: "lab:leader:lock" |
| 183 | + lock_ttl: 30s |
| 184 | + renew_interval: 10s |
| 185 | + retry_interval: 5s |
| 186 | +
|
| 187 | +cartographoor: |
| 188 | + source_url: "https://example.com/networks.json" |
| 189 | + refresh_interval: 5m |
| 190 | + request_timeout: 30s |
| 191 | +
|
| 192 | +bounds: |
| 193 | + refresh_interval: 10s |
| 194 | + request_timeout: 30s |
| 195 | +
|
| 196 | +rate_limiting: |
| 197 | + enabled: false |
| 198 | +` |
| 199 | + |
| 200 | + _, err = tmpFile.WriteString(yamlConfig) |
| 201 | + require.NoError(t, err) |
| 202 | + tmpFile.Close() |
| 203 | + |
| 204 | + cfg, err := Load(tmpFile.Name()) |
| 205 | + require.NoError(t, err) |
| 206 | + |
| 207 | + // Missing headers section should result in empty policies |
| 208 | + assert.Empty(t, cfg.Headers.Policies) |
| 209 | +} |
0 commit comments