| 1 |
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package config |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"log/slog" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/santhosh-tekuri/jsonschema/v6" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
) |
| 15 |
|
| 16 |
func TestNewValidationOptions_Defaults(t *testing.T) { |
| 17 |
opts := NewValidationOptions() |
| 18 |
|
| 19 |
assert.NotNil(t, opts) |
| 20 |
assert.False(t, opts.FormatAssertions) |
| 21 |
assert.False(t, opts.ContentAssertions) |
| 22 |
assert.True(t, opts.SecurityValidation) |
| 23 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 24 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 25 |
assert.False(t, opts.AllowXMLBodyValidation) // Default is false |
| 26 |
assert.False(t, opts.AllowURLEncodedBodyValidation) // Default is false |
| 27 |
assert.Nil(t, opts.RegexEngine) |
| 28 |
assert.Nil(t, opts.RegexCache) |
| 29 |
} |
| 30 |
|
| 31 |
func TestNewValidationOptions_WithNilOption(t *testing.T) { |
| 32 |
opts := NewValidationOptions(nil) |
| 33 |
|
| 34 |
assert.NotNil(t, opts) |
| 35 |
assert.False(t, opts.FormatAssertions) |
| 36 |
assert.False(t, opts.ContentAssertions) |
| 37 |
assert.True(t, opts.SecurityValidation) |
| 38 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 39 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 40 |
assert.False(t, opts.AllowXMLBodyValidation) // Default is false |
| 41 |
assert.Nil(t, opts.RegexEngine) |
| 42 |
assert.Nil(t, opts.RegexCache) |
| 43 |
} |
| 44 |
|
| 45 |
func TestWithFormatAssertions(t *testing.T) { |
| 46 |
opts := NewValidationOptions(WithFormatAssertions()) |
| 47 |
|
| 48 |
assert.True(t, opts.FormatAssertions) |
| 49 |
assert.False(t, opts.ContentAssertions) |
| 50 |
assert.True(t, opts.SecurityValidation) |
| 51 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 52 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 53 |
assert.False(t, opts.AllowXMLBodyValidation) // Default is false |
| 54 |
assert.Nil(t, opts.RegexEngine) |
| 55 |
assert.Nil(t, opts.RegexCache) |
| 56 |
} |
| 57 |
|
| 58 |
func TestWithContentAssertions(t *testing.T) { |
| 59 |
opts := NewValidationOptions(WithContentAssertions()) |
| 60 |
|
| 61 |
assert.False(t, opts.FormatAssertions) |
| 62 |
assert.True(t, opts.ContentAssertions) |
| 63 |
assert.True(t, opts.SecurityValidation) |
| 64 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 65 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 66 |
assert.False(t, opts.AllowXMLBodyValidation) // Default is false |
| 67 |
assert.Nil(t, opts.RegexEngine) |
| 68 |
assert.Nil(t, opts.RegexCache) |
| 69 |
} |
| 70 |
|
| 71 |
func TestWithoutSecurityValidation(t *testing.T) { |
| 72 |
opts := NewValidationOptions(WithoutSecurityValidation()) |
| 73 |
|
| 74 |
assert.False(t, opts.FormatAssertions) |
| 75 |
assert.False(t, opts.ContentAssertions) |
| 76 |
assert.False(t, opts.SecurityValidation) |
| 77 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 78 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 79 |
assert.Nil(t, opts.RegexEngine) |
| 80 |
assert.Nil(t, opts.RegexCache) |
| 81 |
} |
| 82 |
|
| 83 |
func TestWithAuthenticationFunc(t *testing.T) { |
| 84 |
called := false |
| 85 |
authFn := func(ctx context.Context, input *AuthenticationInput) error { |
| 86 |
called = true |
| 87 |
assert.NotNil(t, ctx) |
| 88 |
assert.Equal(t, "ApiKeyAuth", input.SecuritySchemeName) |
| 89 |
return nil |
| 90 |
} |
| 91 |
|
| 92 |
opts := NewValidationOptions(WithAuthenticationFunc(authFn)) |
| 93 |
|
| 94 |
assert.True(t, opts.SecurityValidation) |
| 95 |
assert.NotNil(t, opts.AuthenticationFunc) |
| 96 |
assert.NoError(t, opts.AuthenticationFunc(context.Background(), &AuthenticationInput{ |
| 97 |
SecuritySchemeName: "ApiKeyAuth", |
| 98 |
})) |
| 99 |
assert.True(t, called) |
| 100 |
} |
| 101 |
|
| 102 |
func TestWithRegexEngine(t *testing.T) { |
| 103 |
// Test with nil regex engine (valid) |
| 104 |
var mockEngine jsonschema.RegexpEngine = nil |
| 105 |
|
| 106 |
opts := NewValidationOptions(WithRegexEngine(mockEngine)) |
| 107 |
|
| 108 |
assert.False(t, opts.FormatAssertions) |
| 109 |
assert.False(t, opts.ContentAssertions) |
| 110 |
assert.True(t, opts.SecurityValidation) |
| 111 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 112 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 113 |
assert.Nil(t, opts.RegexEngine) |
| 114 |
assert.Nil(t, opts.RegexCache) |
| 115 |
} |
| 116 |
|
| 117 |
func TestWithExistingOpts(t *testing.T) { |
| 118 |
// Create original options with all settings enabled |
| 119 |
var testEngine jsonschema.RegexpEngine = nil |
| 120 |
original := &ValidationOptions{ |
| 121 |
RegexEngine: testEngine, |
| 122 |
RegexCache: &sync.Map{}, |
| 123 |
FormatAssertions: true, |
| 124 |
AllowXMLBodyValidation: true, |
| 125 |
AllowURLEncodedBodyValidation: true, |
| 126 |
ContentAssertions: true, |
| 127 |
SecurityValidation: false, |
| 128 |
} |
| 129 |
|
| 130 |
// Create new options using existing options |
| 131 |
opts := NewValidationOptions(WithExistingOpts(original)) |
| 132 |
|
| 133 |
assert.Nil(t, opts.RegexEngine) // Both should be nil |
| 134 |
assert.NotNil(t, opts.RegexCache) |
| 135 |
assert.Equal(t, original.AllowXMLBodyValidation, opts.AllowXMLBodyValidation) |
| 136 |
assert.Equal(t, original.AllowURLEncodedBodyValidation, opts.AllowURLEncodedBodyValidation) |
| 137 |
assert.Equal(t, original.FormatAssertions, opts.FormatAssertions) |
| 138 |
assert.Equal(t, original.ContentAssertions, opts.ContentAssertions) |
| 139 |
assert.Equal(t, original.SecurityValidation, opts.SecurityValidation) |
| 140 |
} |
| 141 |
|
| 142 |
func TestWithExistingOpts_NilSource(t *testing.T) { |
| 143 |
// Test with nil source options |
| 144 |
opts := NewValidationOptions(WithExistingOpts(nil)) |
| 145 |
|
| 146 |
assert.NotNil(t, opts) |
| 147 |
// Should not panic and should have default values |
| 148 |
assert.False(t, opts.FormatAssertions) |
| 149 |
assert.False(t, opts.ContentAssertions) |
| 150 |
assert.True(t, opts.SecurityValidation) |
| 151 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 152 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 153 |
assert.False(t, opts.AllowXMLBodyValidation) // Default is false |
| 154 |
assert.Nil(t, opts.RegexEngine) |
| 155 |
assert.Nil(t, opts.RegexCache) |
| 156 |
} |
| 157 |
|
| 158 |
func TestMultipleOptions(t *testing.T) { |
| 159 |
opts := NewValidationOptions( |
| 160 |
WithFormatAssertions(), |
| 161 |
WithContentAssertions(), |
| 162 |
WithXmlBodyValidation(), |
| 163 |
) |
| 164 |
|
| 165 |
assert.True(t, opts.FormatAssertions) |
| 166 |
assert.True(t, opts.ContentAssertions) |
| 167 |
assert.True(t, opts.SecurityValidation) |
| 168 |
assert.True(t, opts.AllowXMLBodyValidation) |
| 169 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 170 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 171 |
assert.Nil(t, opts.RegexEngine) |
| 172 |
assert.Nil(t, opts.RegexCache) |
| 173 |
} |
| 174 |
|
| 175 |
func TestOptionOverride(t *testing.T) { |
| 176 |
// Test that later options override earlier ones |
| 177 |
// First set format assertions, then turn them off by not setting them again |
| 178 |
opts := NewValidationOptions( |
| 179 |
WithFormatAssertions(), |
| 180 |
WithContentAssertions(), |
| 181 |
) |
| 182 |
|
| 183 |
assert.True(t, opts.FormatAssertions) |
| 184 |
assert.True(t, opts.ContentAssertions) |
| 185 |
assert.True(t, opts.SecurityValidation) |
| 186 |
assert.True(t, opts.OpenAPIMode) // Default is true |
| 187 |
assert.False(t, opts.AllowScalarCoercion) // Default is false |
| 188 |
assert.Nil(t, opts.RegexEngine) |
| 189 |
assert.Nil(t, opts.RegexCache) |
| 190 |
} |
| 191 |
|
| 192 |
func TestWithExistingOpts_PartialOverride(t *testing.T) { |
| 193 |
// Create original options |
| 194 |
var testEngine jsonschema.RegexpEngine = nil |
| 195 |
original := &ValidationOptions{ |
| 196 |
RegexEngine: testEngine, |
| 197 |
FormatAssertions: true, |
| 198 |
ContentAssertions: true, |
| 199 |
SecurityValidation: false, |
| 200 |
} |
| 201 |
|
| 202 |
// Create new options using existing options, then override one setting |
| 203 |
opts := NewValidationOptions( |
| 204 |
WithExistingOpts(original), |
| 205 |
WithContentAssertions(), // This should still be true (no change) |
| 206 |
) |
| 207 |
|
| 208 |
assert.Nil(t, opts.RegexEngine) // Both should be nil |
| 209 |
assert.Nil(t, opts.RegexCache) |
| 210 |
assert.True(t, opts.FormatAssertions) // From original |
| 211 |
assert.True(t, opts.ContentAssertions) // Reapplied, but same value |
| 212 |
assert.False(t, opts.SecurityValidation) // From original |
| 213 |
} |
| 214 |
|
| 215 |
func TestWithUrlEncodedBodyValidation(t *testing.T) { |
| 216 |
opts := NewValidationOptions( |
| 217 |
WithURLEncodedBodyValidation(), |
| 218 |
) |
| 219 |
|
| 220 |
assert.True(t, opts.AllowURLEncodedBodyValidation) |
| 221 |
} |
| 222 |
|
| 223 |
func TestComplexScenario(t *testing.T) { |
| 224 |
// Test a complex real-world scenario |
| 225 |
var mockEngine jsonschema.RegexpEngine = nil |
| 226 |
|
| 227 |
// Start with some base options |
| 228 |
baseOpts := &ValidationOptions{ |
| 229 |
FormatAssertions: true, |
| 230 |
SecurityValidation: false, |
| 231 |
// RegexEngine and ContentAssertions are defaults (nil/false) |
| 232 |
} |
| 233 |
|
| 234 |
// Create new options building on the base |
| 235 |
opts := NewValidationOptions( |
| 236 |
WithExistingOpts(baseOpts), |
| 237 |
WithContentAssertions(), |
| 238 |
WithRegexEngine(mockEngine), |
| 239 |
) |
| 240 |
|
| 241 |
// Verify all settings are as expected |
| 242 |
assert.True(t, opts.FormatAssertions) // From base |
| 243 |
assert.True(t, opts.ContentAssertions) // Added |
| 244 |
assert.False(t, opts.SecurityValidation) // From base |
| 245 |
assert.Nil(t, opts.RegexEngine) // Should be nil |
| 246 |
assert.Nil(t, opts.RegexCache) |
| 247 |
} |
| 248 |
|
| 249 |
func TestMultipleOptionsWithSecurityDisabled(t *testing.T) { |
| 250 |
opts := NewValidationOptions( |
| 251 |
WithFormatAssertions(), |
| 252 |
WithContentAssertions(), |
| 253 |
WithoutSecurityValidation(), |
| 254 |
) |
| 255 |
|
| 256 |
assert.True(t, opts.FormatAssertions) |
| 257 |
assert.True(t, opts.ContentAssertions) |
| 258 |
assert.False(t, opts.SecurityValidation) |
| 259 |
assert.Nil(t, opts.RegexEngine) |
| 260 |
assert.Nil(t, opts.RegexCache) |
| 261 |
} |
| 262 |
|
| 263 |
func TestWithExistingOpts_SecurityValidationCopied(t *testing.T) { |
| 264 |
// Test that SecurityValidation is properly copied |
| 265 |
original := &ValidationOptions{ |
| 266 |
SecurityValidation: false, |
| 267 |
} |
| 268 |
|
| 269 |
opts := NewValidationOptions(WithExistingOpts(original)) |
| 270 |
|
| 271 |
assert.False(t, opts.SecurityValidation) |
| 272 |
|
| 273 |
// Test the opposite |
| 274 |
original2 := &ValidationOptions{ |
| 275 |
SecurityValidation: true, |
| 276 |
} |
| 277 |
|
| 278 |
opts2 := NewValidationOptions(WithExistingOpts(original2)) |
| 279 |
|
| 280 |
assert.True(t, opts2.SecurityValidation) |
| 281 |
} |
| 282 |
|
| 283 |
func TestWithExistingOpts_AuthenticationFuncCopied(t *testing.T) { |
| 284 |
called := false |
| 285 |
authFn := func(context.Context, *AuthenticationInput) error { |
| 286 |
called = true |
| 287 |
return nil |
| 288 |
} |
| 289 |
|
| 290 |
original := &ValidationOptions{ |
| 291 |
AuthenticationFunc: authFn, |
| 292 |
} |
| 293 |
|
| 294 |
opts := NewValidationOptions(WithExistingOpts(original)) |
| 295 |
|
| 296 |
assert.NotNil(t, opts.AuthenticationFunc) |
| 297 |
assert.NoError(t, opts.AuthenticationFunc(context.Background(), &AuthenticationInput{})) |
| 298 |
assert.True(t, called) |
| 299 |
} |
| 300 |
|
| 301 |
// Tests for new OpenAPI and scalar coercion configuration options |
| 302 |
|
| 303 |
func TestWithOpenAPIMode(t *testing.T) { |
| 304 |
opts := NewValidationOptions(WithOpenAPIMode()) |
| 305 |
|
| 306 |
assert.True(t, opts.OpenAPIMode) |
| 307 |
assert.False(t, opts.AllowScalarCoercion) // Should be default false |
| 308 |
assert.False(t, opts.FormatAssertions) // Should be default false |
| 309 |
assert.False(t, opts.ContentAssertions) // Should be default false |
| 310 |
assert.True(t, opts.SecurityValidation) // Should be default true |
| 311 |
} |
| 312 |
|
| 313 |
func TestWithoutOpenAPIMode(t *testing.T) { |
| 314 |
opts := NewValidationOptions(WithoutOpenAPIMode()) |
| 315 |
|
| 316 |
assert.False(t, opts.OpenAPIMode) |
| 317 |
assert.False(t, opts.AllowScalarCoercion) // Should be default false |
| 318 |
assert.False(t, opts.FormatAssertions) // Should be default false |
| 319 |
assert.False(t, opts.ContentAssertions) // Should be default false |
| 320 |
assert.True(t, opts.SecurityValidation) // Should be default true |
| 321 |
} |
| 322 |
|
| 323 |
func TestWithScalarCoercion(t *testing.T) { |
| 324 |
opts := NewValidationOptions(WithScalarCoercion()) |
| 325 |
|
| 326 |
assert.True(t, opts.AllowScalarCoercion) |
| 327 |
assert.True(t, opts.OpenAPIMode) // Should be default true |
| 328 |
assert.False(t, opts.FormatAssertions) // Should be default false |
| 329 |
assert.False(t, opts.ContentAssertions) // Should be default false |
| 330 |
assert.True(t, opts.SecurityValidation) // Should be default true |
| 331 |
} |
| 332 |
|
| 333 |
func TestWithOpenAPIModeAndScalarCoercion(t *testing.T) { |
| 334 |
opts := NewValidationOptions( |
| 335 |
WithOpenAPIMode(), |
| 336 |
WithScalarCoercion(), |
| 337 |
) |
| 338 |
|
| 339 |
assert.True(t, opts.OpenAPIMode) |
| 340 |
assert.True(t, opts.AllowScalarCoercion) |
| 341 |
assert.False(t, opts.FormatAssertions) // Should be default false |
| 342 |
assert.False(t, opts.ContentAssertions) // Should be default false |
| 343 |
assert.True(t, opts.SecurityValidation) // Should be default true |
| 344 |
} |
| 345 |
|
| 346 |
func TestWithOpenAPIModeOverride(t *testing.T) { |
| 347 |
// Test that WithoutOpenAPIMode can override WithOpenAPIMode |
| 348 |
opts := NewValidationOptions( |
| 349 |
WithOpenAPIMode(), |
| 350 |
WithoutOpenAPIMode(), |
| 351 |
) |
| 352 |
|
| 353 |
assert.False(t, opts.OpenAPIMode) // Should be false (last option wins) |
| 354 |
assert.False(t, opts.AllowScalarCoercion) |
| 355 |
} |
| 356 |
|
| 357 |
func TestComplexOpenAPIScenario(t *testing.T) { |
| 358 |
// Test a complex scenario with OpenAPI mode and other options |
| 359 |
opts := NewValidationOptions( |
| 360 |
WithFormatAssertions(), |
| 361 |
WithOpenAPIMode(), |
| 362 |
WithScalarCoercion(), |
| 363 |
WithContentAssertions(), |
| 364 |
WithoutSecurityValidation(), |
| 365 |
) |
| 366 |
|
| 367 |
assert.True(t, opts.OpenAPIMode) |
| 368 |
assert.True(t, opts.AllowScalarCoercion) |
| 369 |
assert.True(t, opts.FormatAssertions) |
| 370 |
assert.True(t, opts.ContentAssertions) |
| 371 |
assert.False(t, opts.SecurityValidation) |
| 372 |
assert.Nil(t, opts.RegexEngine) |
| 373 |
assert.Nil(t, opts.RegexCache) |
| 374 |
} |
| 375 |
|
| 376 |
func TestWithExistingOpts_OpenAPIFields(t *testing.T) { |
| 377 |
// Test that OpenAPI fields are properly copied from existing options |
| 378 |
original := &ValidationOptions{ |
| 379 |
OpenAPIMode: true, |
| 380 |
AllowScalarCoercion: true, |
| 381 |
FormatAssertions: false, |
| 382 |
ContentAssertions: false, |
| 383 |
SecurityValidation: true, |
| 384 |
} |
| 385 |
|
| 386 |
opts := NewValidationOptions(WithExistingOpts(original)) |
| 387 |
|
| 388 |
assert.True(t, opts.OpenAPIMode) |
| 389 |
assert.True(t, opts.AllowScalarCoercion) |
| 390 |
assert.False(t, opts.FormatAssertions) |
| 391 |
assert.False(t, opts.ContentAssertions) |
| 392 |
assert.True(t, opts.SecurityValidation) |
| 393 |
} |
| 394 |
|
| 395 |
func TestWithCustomFormat(t *testing.T) { |
| 396 |
// Test WithCustomFormat option |
| 397 |
testFormatFunc := func(v any) error { |
| 398 |
return nil // Simple test format function |
| 399 |
} |
| 400 |
|
| 401 |
opts := NewValidationOptions(WithCustomFormat("test-format", testFormatFunc)) |
| 402 |
|
| 403 |
assert.NotNil(t, opts.Formats) |
| 404 |
assert.Contains(t, opts.Formats, "test-format") |
| 405 |
assert.NotNil(t, opts.Formats["test-format"]) |
| 406 |
} |
| 407 |
|
| 408 |
func TestWithSchemaCache(t *testing.T) { |
| 409 |
// Test with nil cache (disables caching) |
| 410 |
opts := NewValidationOptions(WithSchemaCache(nil)) |
| 411 |
assert.Nil(t, opts.SchemaCache) |
| 412 |
|
| 413 |
// Test with default cache by creating a new options object |
| 414 |
optsDefault := NewValidationOptions() |
| 415 |
assert.NotNil(t, optsDefault.SchemaCache, "Default options should have a cache") |
| 416 |
|
| 417 |
// Test setting a custom cache |
| 418 |
customCache := optsDefault.SchemaCache // Use default cache as custom |
| 419 |
optsCustom := NewValidationOptions(WithSchemaCache(customCache)) |
| 420 |
assert.Equal(t, customCache, optsCustom.SchemaCache) |
| 421 |
} |
| 422 |
|
| 423 |
func TestWithRegexpCache(t *testing.T) { |
| 424 |
syncMap := &sync.Map{} |
| 425 |
|
| 426 |
opts := NewValidationOptions(WithRegexCache(syncMap)) |
| 427 |
|
| 428 |
assert.NotNil(t, opts.RegexCache) |
| 429 |
} |
| 430 |
|
| 431 |
// Tests for strict mode configuration options |
| 432 |
|
| 433 |
func TestWithStrictMode(t *testing.T) { |
| 434 |
opts := NewValidationOptions(WithStrictMode()) |
| 435 |
|
| 436 |
assert.True(t, opts.StrictMode) |
| 437 |
assert.Nil(t, opts.StrictIgnorePaths) |
| 438 |
assert.Nil(t, opts.StrictIgnoredHeaders) |
| 439 |
} |
| 440 |
|
| 441 |
func TestWithStrictIgnorePaths(t *testing.T) { |
| 442 |
paths := []string{"$.body.metadata.*", "$.headers.X-*"} |
| 443 |
opts := NewValidationOptions(WithStrictIgnorePaths(paths...)) |
| 444 |
|
| 445 |
assert.Equal(t, paths, opts.StrictIgnorePaths) |
| 446 |
assert.False(t, opts.StrictMode) // Not enabled by default |
| 447 |
} |
| 448 |
|
| 449 |
func TestWithStrictIgnoredHeaders(t *testing.T) { |
| 450 |
headers := []string{"x-custom-header", "x-another-header"} |
| 451 |
opts := NewValidationOptions(WithStrictIgnoredHeaders(headers...)) |
| 452 |
|
| 453 |
assert.Equal(t, headers, opts.StrictIgnoredHeaders) |
| 454 |
assert.False(t, opts.strictIgnoredHeadersMerge) |
| 455 |
} |
| 456 |
|
| 457 |
func TestWithStrictIgnoredHeadersExtra(t *testing.T) { |
| 458 |
headers := []string{"x-extra-header"} |
| 459 |
opts := NewValidationOptions(WithStrictIgnoredHeadersExtra(headers...)) |
| 460 |
|
| 461 |
assert.Equal(t, headers, opts.StrictIgnoredHeaders) |
| 462 |
assert.True(t, opts.strictIgnoredHeadersMerge) |
| 463 |
} |
| 464 |
|
| 465 |
func TestGetEffectiveStrictIgnoredHeaders_Default(t *testing.T) { |
| 466 |
opts := NewValidationOptions() |
| 467 |
|
| 468 |
headers := opts.GetEffectiveStrictIgnoredHeaders() |
| 469 |
|
| 470 |
assert.NotNil(t, headers) |
| 471 |
assert.Contains(t, headers, "content-type") |
| 472 |
assert.Contains(t, headers, "authorization") |
| 473 |
} |
| 474 |
|
| 475 |
func TestGetEffectiveStrictIgnoredHeaders_Replace(t *testing.T) { |
| 476 |
customHeaders := []string{"x-only-this"} |
| 477 |
opts := NewValidationOptions(WithStrictIgnoredHeaders(customHeaders...)) |
| 478 |
|
| 479 |
headers := opts.GetEffectiveStrictIgnoredHeaders() |
| 480 |
|
| 481 |
assert.Equal(t, customHeaders, headers) |
| 482 |
assert.NotContains(t, headers, "content-type") // Default headers are replaced |
| 483 |
} |
| 484 |
|
| 485 |
func TestGetEffectiveStrictIgnoredHeaders_Merge(t *testing.T) { |
| 486 |
extraHeaders := []string{"x-extra-header"} |
| 487 |
opts := NewValidationOptions(WithStrictIgnoredHeadersExtra(extraHeaders...)) |
| 488 |
|
| 489 |
headers := opts.GetEffectiveStrictIgnoredHeaders() |
| 490 |
|
| 491 |
// Should have both defaults and extras |
| 492 |
assert.Contains(t, headers, "content-type") // From defaults |
| 493 |
assert.Contains(t, headers, "x-extra-header") // From extras |
| 494 |
assert.Contains(t, headers, "authorization") // From defaults |
| 495 |
} |
| 496 |
|
| 497 |
func TestWithLogger(t *testing.T) { |
| 498 |
logger := slog.New(slog.NewTextHandler(nil, nil)) |
| 499 |
opts := NewValidationOptions(WithLogger(logger)) |
| 500 |
|
| 501 |
assert.Equal(t, logger, opts.Logger) |
| 502 |
} |
| 503 |
|
| 504 |
func TestWithExistingOpts_StrictFields(t *testing.T) { |
| 505 |
original := &ValidationOptions{ |
| 506 |
StrictMode: true, |
| 507 |
StrictRejectReadOnly: true, |
| 508 |
StrictRejectWriteOnly: true, |
| 509 |
StrictIgnorePaths: []string{"$.body.*"}, |
| 510 |
StrictIgnoredHeaders: []string{"x-custom"}, |
| 511 |
strictIgnoredHeadersMerge: true, |
| 512 |
Logger: slog.New(slog.NewTextHandler(nil, nil)), |
| 513 |
} |
| 514 |
|
| 515 |
opts := NewValidationOptions(WithExistingOpts(original)) |
| 516 |
|
| 517 |
assert.True(t, opts.StrictMode) |
| 518 |
assert.True(t, opts.StrictRejectReadOnly) |
| 519 |
assert.True(t, opts.StrictRejectWriteOnly) |
| 520 |
assert.Equal(t, original.StrictIgnorePaths, opts.StrictIgnorePaths) |
| 521 |
assert.Equal(t, original.StrictIgnoredHeaders, opts.StrictIgnoredHeaders) |
| 522 |
assert.True(t, opts.strictIgnoredHeadersMerge) |
| 523 |
assert.Equal(t, original.Logger, opts.Logger) |
| 524 |
} |
| 525 |
|
| 526 |
func TestWithStrictRejectReadOnly(t *testing.T) { |
| 527 |
opts := NewValidationOptions(WithStrictRejectReadOnly()) |
| 528 |
assert.True(t, opts.StrictRejectReadOnly) |
| 529 |
} |
| 530 |
|
| 531 |
func TestWithStrictRejectWriteOnly(t *testing.T) { |
| 532 |
opts := NewValidationOptions(WithStrictRejectWriteOnly()) |
| 533 |
assert.True(t, opts.StrictRejectWriteOnly) |
| 534 |
} |
| 535 |
|
| 536 |
func TestStrictModeWithIgnorePaths(t *testing.T) { |
| 537 |
paths := []string{"$.body.metadata.*"} |
| 538 |
opts := NewValidationOptions( |
| 539 |
WithStrictMode(), |
| 540 |
WithStrictIgnorePaths(paths...), |
| 541 |
) |
| 542 |
|
| 543 |
assert.True(t, opts.StrictMode) |
| 544 |
assert.Equal(t, paths, opts.StrictIgnorePaths) |
| 545 |
} |
| 546 |
|
| 547 |
func TestWithPathTree(t *testing.T) { |
| 548 |
// Use a mock/nil path tree — WithPathTree just sets the field |
| 549 |
opts := NewValidationOptions(WithPathTree(nil)) |
| 550 |
assert.Nil(t, opts.PathTree) |
| 551 |
|
| 552 |
// TestWithPathTree with a real value — use a custom implementation |
| 553 |
// We can verify the field is set using a simple check |
| 554 |
opts2 := &ValidationOptions{} |
| 555 |
WithPathTree(nil)(opts2) |
| 556 |
assert.Nil(t, opts2.PathTree) |
| 557 |
} |
| 558 |
|
| 559 |
func TestDisablePathTree(t *testing.T) { |
| 560 |
opts := NewValidationOptions(DisablePathTree()) |
| 561 |
assert.True(t, opts.IsPathTreeDisabled()) |
| 562 |
} |
| 563 |
|
| 564 |
func TestIsPathTreeDisabled_Default(t *testing.T) { |
| 565 |
opts := NewValidationOptions() |
| 566 |
assert.False(t, opts.IsPathTreeDisabled()) |
| 567 |
} |
| 568 |
|
| 569 |
func TestWithExistingOpts_PathTreeFields(t *testing.T) { |
| 570 |
original := NewValidationOptions(DisablePathTree()) |
| 571 |
|
| 572 |
opts := NewValidationOptions(WithExistingOpts(original)) |
| 573 |
|
| 574 |
assert.True(t, opts.IsPathTreeDisabled()) |
| 575 |
assert.Nil(t, opts.PathTree) |
| 576 |
} |