| 1 |
// Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package helpers |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 11 |
"github.com/pb33f/libopenapi/orderedmap" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 15 |
) |
| 16 |
|
| 17 |
// Test ExtractParamsForOperation with various HTTP methods |
| 18 |
func TestExtractParamsForOperation(t *testing.T) { |
| 19 |
pathItem := &v3.PathItem{ |
| 20 |
// Parameters: []*v3.Parameter{{Name: "param"}}, |
| 21 |
Get: &v3.Operation{Parameters: []*v3.Parameter{{Name: "getParam"}}}, |
| 22 |
Post: &v3.Operation{Parameters: []*v3.Parameter{{Name: "postParam"}}}, |
| 23 |
Put: &v3.Operation{Parameters: []*v3.Parameter{{Name: "putParam"}}}, |
| 24 |
Delete: &v3.Operation{Parameters: []*v3.Parameter{{Name: "deleteParam"}}}, |
| 25 |
Options: &v3.Operation{Parameters: []*v3.Parameter{{Name: "optionsParam"}}}, |
| 26 |
Head: &v3.Operation{Parameters: []*v3.Parameter{{Name: "headParam"}}}, |
| 27 |
Patch: &v3.Operation{Parameters: []*v3.Parameter{{Name: "patchParam"}}}, |
| 28 |
Trace: &v3.Operation{Parameters: []*v3.Parameter{{Name: "traceParam"}}}, |
| 29 |
} |
| 30 |
|
| 31 |
// Test all HTTP methods |
| 32 |
tests := []struct { |
| 33 |
method string |
| 34 |
expected []string // Expected parameter names |
| 35 |
}{ |
| 36 |
{http.MethodGet, []string{"getParam"}}, |
| 37 |
{http.MethodPost, []string{"postParam"}}, |
| 38 |
{http.MethodPut, []string{"putParam"}}, |
| 39 |
{http.MethodDelete, []string{"deleteParam"}}, |
| 40 |
{http.MethodOptions, []string{"optionsParam"}}, |
| 41 |
{http.MethodHead, []string{"headParam"}}, |
| 42 |
{http.MethodPatch, []string{"patchParam"}}, |
| 43 |
{http.MethodTrace, []string{"traceParam"}}, |
| 44 |
} |
| 45 |
|
| 46 |
for _, tt := range tests { |
| 47 |
// Create a new request with the specified method |
| 48 |
request, _ := http.NewRequest(tt.method, "/", nil) |
| 49 |
|
| 50 |
// Extract the parameters for the current request method |
| 51 |
params := ExtractParamsForOperation(request, pathItem) |
| 52 |
|
| 53 |
// Check if the number of parameters matches the expected count |
| 54 |
require.Len(t, params, len(tt.expected)) |
| 55 |
|
| 56 |
// Verify that the extracted parameter names match the expected ones |
| 57 |
for i, param := range params { |
| 58 |
require.Equal(t, tt.expected[i], param.Name) |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Test cast with different values (bool, int, float, string) |
| 64 |
func TestCast(t *testing.T) { |
| 65 |
require.Equal(t, true, cast("true")) |
| 66 |
require.Equal(t, int64(123), cast("123")) |
| 67 |
require.Equal(t, 123.45, cast("123.45")) |
| 68 |
require.Equal(t, "test", cast("test")) |
| 69 |
} |
| 70 |
|
| 71 |
// Test ExtractSecurityForOperation with various HTTP methods |
| 72 |
func TestExtractSecurityForOperation(t *testing.T) { |
| 73 |
// Create a PathItem with security requirements for each method |
| 74 |
pathItem := &v3.PathItem{ |
| 75 |
Get: &v3.Operation{Security: []*base.SecurityRequirement{{}}}, |
| 76 |
Post: &v3.Operation{Security: []*base.SecurityRequirement{{}}}, |
| 77 |
Put: &v3.Operation{Security: []*base.SecurityRequirement{{}}}, |
| 78 |
Delete: &v3.Operation{Security: []*base.SecurityRequirement{{}}}, |
| 79 |
Options: &v3.Operation{ |
| 80 |
Security: []*base.SecurityRequirement{{}}, |
| 81 |
}, |
| 82 |
Head: &v3.Operation{ |
| 83 |
Security: []*base.SecurityRequirement{{}}, |
| 84 |
}, |
| 85 |
Patch: &v3.Operation{ |
| 86 |
Security: []*base.SecurityRequirement{{}}, |
| 87 |
}, |
| 88 |
Trace: &v3.Operation{ |
| 89 |
Security: []*base.SecurityRequirement{{}}, |
| 90 |
}, |
| 91 |
} |
| 92 |
|
| 93 |
// Test all HTTP methods |
| 94 |
tests := []struct { |
| 95 |
method string |
| 96 |
}{ |
| 97 |
{http.MethodGet}, |
| 98 |
{http.MethodPost}, |
| 99 |
{http.MethodPut}, |
| 100 |
{http.MethodDelete}, |
| 101 |
{http.MethodOptions}, |
| 102 |
{http.MethodHead}, |
| 103 |
{http.MethodPatch}, |
| 104 |
{http.MethodTrace}, |
| 105 |
} |
| 106 |
|
| 107 |
for _, tt := range tests { |
| 108 |
// Create a new request with the specified method |
| 109 |
request, _ := http.NewRequest(tt.method, "/", nil) |
| 110 |
|
| 111 |
// Extract the security requirements for the current request method |
| 112 |
security := ExtractSecurityForOperation(request, pathItem) |
| 113 |
|
| 114 |
// Check if the number of security requirements matches the expected count (1 in all cases) |
| 115 |
require.Len(t, security, 1, "Failed for method: "+tt.method) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Test ExtractSecurityHeaderNames with various security scheme types |
| 120 |
func TestExtractSecurityHeaderNames(t *testing.T) { |
| 121 |
t.Run("nil inputs", func(t *testing.T) { |
| 122 |
require.Nil(t, ExtractSecurityHeaderNames(nil, nil)) |
| 123 |
require.Nil(t, ExtractSecurityHeaderNames([]*base.SecurityRequirement{}, nil)) |
| 124 |
require.Nil(t, ExtractSecurityHeaderNames(nil, map[string]*v3.SecurityScheme{})) |
| 125 |
}) |
| 126 |
|
| 127 |
t.Run("apiKey with in:header", func(t *testing.T) { |
| 128 |
schemes := map[string]*v3.SecurityScheme{ |
| 129 |
"ApiKeyAuth": { |
| 130 |
Type: "apiKey", |
| 131 |
In: "header", |
| 132 |
Name: "X-API-Key", |
| 133 |
}, |
| 134 |
} |
| 135 |
security := []*base.SecurityRequirement{ |
| 136 |
{ |
| 137 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 138 |
"ApiKeyAuth": {"read"}, |
| 139 |
}), |
| 140 |
}, |
| 141 |
} |
| 142 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 143 |
require.Equal(t, []string{"X-API-Key"}, headers) |
| 144 |
}) |
| 145 |
|
| 146 |
t.Run("apiKey with in:query should not add header", func(t *testing.T) { |
| 147 |
schemes := map[string]*v3.SecurityScheme{ |
| 148 |
"ApiKeyQuery": { |
| 149 |
Type: "apiKey", |
| 150 |
In: "query", |
| 151 |
Name: "api_key", |
| 152 |
}, |
| 153 |
} |
| 154 |
security := []*base.SecurityRequirement{ |
| 155 |
{ |
| 156 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 157 |
"ApiKeyQuery": {}, |
| 158 |
}), |
| 159 |
}, |
| 160 |
} |
| 161 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 162 |
require.Nil(t, headers) |
| 163 |
}) |
| 164 |
|
| 165 |
t.Run("apiKey with in:cookie should not add header", func(t *testing.T) { |
| 166 |
schemes := map[string]*v3.SecurityScheme{ |
| 167 |
"ApiKeyCookie": { |
| 168 |
Type: "apiKey", |
| 169 |
In: "cookie", |
| 170 |
Name: "session_id", |
| 171 |
}, |
| 172 |
} |
| 173 |
security := []*base.SecurityRequirement{ |
| 174 |
{ |
| 175 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 176 |
"ApiKeyCookie": {}, |
| 177 |
}), |
| 178 |
}, |
| 179 |
} |
| 180 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 181 |
require.Nil(t, headers) |
| 182 |
}) |
| 183 |
|
| 184 |
t.Run("http bearer scheme", func(t *testing.T) { |
| 185 |
schemes := map[string]*v3.SecurityScheme{ |
| 186 |
"BearerAuth": { |
| 187 |
Type: "http", |
| 188 |
Scheme: "bearer", |
| 189 |
}, |
| 190 |
} |
| 191 |
security := []*base.SecurityRequirement{ |
| 192 |
{ |
| 193 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 194 |
"BearerAuth": {}, |
| 195 |
}), |
| 196 |
}, |
| 197 |
} |
| 198 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 199 |
require.Equal(t, []string{"Authorization"}, headers) |
| 200 |
}) |
| 201 |
|
| 202 |
t.Run("http basic scheme", func(t *testing.T) { |
| 203 |
schemes := map[string]*v3.SecurityScheme{ |
| 204 |
"BasicAuth": { |
| 205 |
Type: "http", |
| 206 |
Scheme: "basic", |
| 207 |
}, |
| 208 |
} |
| 209 |
security := []*base.SecurityRequirement{ |
| 210 |
{ |
| 211 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 212 |
"BasicAuth": {}, |
| 213 |
}), |
| 214 |
}, |
| 215 |
} |
| 216 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 217 |
require.Equal(t, []string{"Authorization"}, headers) |
| 218 |
}) |
| 219 |
|
| 220 |
t.Run("oauth2 scheme", func(t *testing.T) { |
| 221 |
schemes := map[string]*v3.SecurityScheme{ |
| 222 |
"OAuth2": { |
| 223 |
Type: "oauth2", |
| 224 |
}, |
| 225 |
} |
| 226 |
security := []*base.SecurityRequirement{ |
| 227 |
{ |
| 228 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 229 |
"OAuth2": {"read:users"}, |
| 230 |
}), |
| 231 |
}, |
| 232 |
} |
| 233 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 234 |
require.Equal(t, []string{"Authorization"}, headers) |
| 235 |
}) |
| 236 |
|
| 237 |
t.Run("openIdConnect scheme", func(t *testing.T) { |
| 238 |
schemes := map[string]*v3.SecurityScheme{ |
| 239 |
"OpenID": { |
| 240 |
Type: "openIdConnect", |
| 241 |
OpenIdConnectUrl: "https://example.com/.well-known/openid-configuration", |
| 242 |
}, |
| 243 |
} |
| 244 |
security := []*base.SecurityRequirement{ |
| 245 |
{ |
| 246 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 247 |
"OpenID": {}, |
| 248 |
}), |
| 249 |
}, |
| 250 |
} |
| 251 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 252 |
require.Equal(t, []string{"Authorization"}, headers) |
| 253 |
}) |
| 254 |
|
| 255 |
t.Run("empty security requirement (ContainsEmptyRequirement)", func(t *testing.T) { |
| 256 |
schemes := map[string]*v3.SecurityScheme{ |
| 257 |
"ApiKeyAuth": { |
| 258 |
Type: "apiKey", |
| 259 |
In: "header", |
| 260 |
Name: "X-API-Key", |
| 261 |
}, |
| 262 |
} |
| 263 |
security := []*base.SecurityRequirement{ |
| 264 |
{ |
| 265 |
ContainsEmptyRequirement: true, |
| 266 |
}, |
| 267 |
} |
| 268 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 269 |
require.Nil(t, headers) |
| 270 |
}) |
| 271 |
|
| 272 |
t.Run("nil security requirement in slice", func(t *testing.T) { |
| 273 |
schemes := map[string]*v3.SecurityScheme{ |
| 274 |
"ApiKeyAuth": { |
| 275 |
Type: "apiKey", |
| 276 |
In: "header", |
| 277 |
Name: "X-API-Key", |
| 278 |
}, |
| 279 |
} |
| 280 |
security := []*base.SecurityRequirement{nil} |
| 281 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 282 |
require.Nil(t, headers) |
| 283 |
}) |
| 284 |
|
| 285 |
t.Run("security requirement with nil Requirements map", func(t *testing.T) { |
| 286 |
schemes := map[string]*v3.SecurityScheme{ |
| 287 |
"ApiKeyAuth": { |
| 288 |
Type: "apiKey", |
| 289 |
In: "header", |
| 290 |
Name: "X-API-Key", |
| 291 |
}, |
| 292 |
} |
| 293 |
security := []*base.SecurityRequirement{ |
| 294 |
{ |
| 295 |
Requirements: nil, |
| 296 |
}, |
| 297 |
} |
| 298 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 299 |
require.Nil(t, headers) |
| 300 |
}) |
| 301 |
|
| 302 |
t.Run("multiple security options OR - different headers", func(t *testing.T) { |
| 303 |
schemes := map[string]*v3.SecurityScheme{ |
| 304 |
"ApiKeyAuth": { |
| 305 |
Type: "apiKey", |
| 306 |
In: "header", |
| 307 |
Name: "X-API-Key", |
| 308 |
}, |
| 309 |
"BearerAuth": { |
| 310 |
Type: "http", |
| 311 |
Scheme: "bearer", |
| 312 |
}, |
| 313 |
} |
| 314 |
// OR logic: separate security requirements |
| 315 |
security := []*base.SecurityRequirement{ |
| 316 |
{ |
| 317 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 318 |
"ApiKeyAuth": {}, |
| 319 |
}), |
| 320 |
}, |
| 321 |
{ |
| 322 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 323 |
"BearerAuth": {}, |
| 324 |
}), |
| 325 |
}, |
| 326 |
} |
| 327 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 328 |
require.Len(t, headers, 2) |
| 329 |
require.Contains(t, headers, "X-API-Key") |
| 330 |
require.Contains(t, headers, "Authorization") |
| 331 |
}) |
| 332 |
|
| 333 |
t.Run("combined requirements AND - both headers", func(t *testing.T) { |
| 334 |
schemes := map[string]*v3.SecurityScheme{ |
| 335 |
"ApiKeyAuth": { |
| 336 |
Type: "apiKey", |
| 337 |
In: "header", |
| 338 |
Name: "X-API-Key", |
| 339 |
}, |
| 340 |
"BearerAuth": { |
| 341 |
Type: "http", |
| 342 |
Scheme: "bearer", |
| 343 |
}, |
| 344 |
} |
| 345 |
// AND logic: multiple schemes in one requirement |
| 346 |
security := []*base.SecurityRequirement{ |
| 347 |
{ |
| 348 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 349 |
"ApiKeyAuth": {}, |
| 350 |
"BearerAuth": {}, |
| 351 |
}), |
| 352 |
}, |
| 353 |
} |
| 354 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 355 |
require.Len(t, headers, 2) |
| 356 |
require.Contains(t, headers, "X-API-Key") |
| 357 |
require.Contains(t, headers, "Authorization") |
| 358 |
}) |
| 359 |
|
| 360 |
t.Run("security scheme not found in schemes map", func(t *testing.T) { |
| 361 |
schemes := map[string]*v3.SecurityScheme{ |
| 362 |
"SomeOtherScheme": { |
| 363 |
Type: "apiKey", |
| 364 |
In: "header", |
| 365 |
Name: "X-Other", |
| 366 |
}, |
| 367 |
} |
| 368 |
security := []*base.SecurityRequirement{ |
| 369 |
{ |
| 370 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 371 |
"NonExistent": {}, |
| 372 |
}), |
| 373 |
}, |
| 374 |
} |
| 375 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 376 |
require.Nil(t, headers) |
| 377 |
}) |
| 378 |
|
| 379 |
t.Run("nil scheme in schemes map", func(t *testing.T) { |
| 380 |
schemes := map[string]*v3.SecurityScheme{ |
| 381 |
"NilScheme": nil, |
| 382 |
} |
| 383 |
security := []*base.SecurityRequirement{ |
| 384 |
{ |
| 385 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 386 |
"NilScheme": {}, |
| 387 |
}), |
| 388 |
}, |
| 389 |
} |
| 390 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 391 |
require.Nil(t, headers) |
| 392 |
}) |
| 393 |
|
| 394 |
t.Run("deduplication of Authorization header", func(t *testing.T) { |
| 395 |
schemes := map[string]*v3.SecurityScheme{ |
| 396 |
"BearerAuth": { |
| 397 |
Type: "http", |
| 398 |
Scheme: "bearer", |
| 399 |
}, |
| 400 |
"OAuth2": { |
| 401 |
Type: "oauth2", |
| 402 |
}, |
| 403 |
} |
| 404 |
// Both use Authorization header |
| 405 |
security := []*base.SecurityRequirement{ |
| 406 |
{ |
| 407 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 408 |
"BearerAuth": {}, |
| 409 |
}), |
| 410 |
}, |
| 411 |
{ |
| 412 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 413 |
"OAuth2": {"read"}, |
| 414 |
}), |
| 415 |
}, |
| 416 |
} |
| 417 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 418 |
require.Equal(t, []string{"Authorization"}, headers) |
| 419 |
}) |
| 420 |
|
| 421 |
t.Run("case insensitive type matching", func(t *testing.T) { |
| 422 |
schemes := map[string]*v3.SecurityScheme{ |
| 423 |
"ApiKeyAuth": { |
| 424 |
Type: "APIKEY", // uppercase |
| 425 |
In: "HEADER", // uppercase |
| 426 |
Name: "X-API-Key", |
| 427 |
}, |
| 428 |
} |
| 429 |
security := []*base.SecurityRequirement{ |
| 430 |
{ |
| 431 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 432 |
"ApiKeyAuth": {}, |
| 433 |
}), |
| 434 |
}, |
| 435 |
} |
| 436 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 437 |
require.Equal(t, []string{"X-API-Key"}, headers) |
| 438 |
}) |
| 439 |
|
| 440 |
t.Run("unknown security type is ignored", func(t *testing.T) { |
| 441 |
schemes := map[string]*v3.SecurityScheme{ |
| 442 |
"Unknown": { |
| 443 |
Type: "mutualTLS", // valid OpenAPI type but doesn't use headers |
| 444 |
}, |
| 445 |
} |
| 446 |
security := []*base.SecurityRequirement{ |
| 447 |
{ |
| 448 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 449 |
"Unknown": {}, |
| 450 |
}), |
| 451 |
}, |
| 452 |
} |
| 453 |
headers := ExtractSecurityHeaderNames(security, schemes) |
| 454 |
require.Nil(t, headers) |
| 455 |
}) |
| 456 |
} |
| 457 |
|
| 458 |
func TestConstructParamMapFromDeepObjectEncoding(t *testing.T) { |
| 459 |
// Define mock values for testing |
| 460 |
values := []*QueryParam{ |
| 461 |
{Key: "key1", Values: []string{"value1"}, Property: "prop1"}, |
| 462 |
{Key: "key2", Values: []string{"123"}, Property: "prop2"}, |
| 463 |
{Key: "key3", Values: []string{"456", "789"}, Property: "prop3"}, |
| 464 |
} |
| 465 |
|
| 466 |
// Test case 1: Schema is nil |
| 467 |
decoded := ConstructParamMapFromDeepObjectEncoding(values, nil) |
| 468 |
require.NotNil(t, decoded) |
| 469 |
require.Equal(t, "value1", decoded["key1"].(map[string]interface{})["prop1"]) |
| 470 |
require.Equal(t, int64(123), decoded["key2"].(map[string]interface{})["prop2"]) |
| 471 |
require.Equal(t, int64(456), decoded["key3"].(map[string]interface{})["prop3"]) |
| 472 |
|
| 473 |
// Test case 2: Schema type contains array for the first param (Array handling) |
| 474 |
schema := &base.Schema{Type: []string{"array"}} |
| 475 |
decoded = ConstructParamMapFromDeepObjectEncoding(values, schema) |
| 476 |
require.NotNil(t, decoded) |
| 477 |
require.Equal(t, []interface{}{"value1"}, decoded["key1"].(map[string]interface{})["prop1"]) |
| 478 |
require.Equal(t, []interface{}{int64(123)}, decoded["key2"].(map[string]interface{})["prop2"]) |
| 479 |
require.Equal(t, []interface{}{int64(456), int64(789)}, decoded["key3"].(map[string]interface{})["prop3"]) |
| 480 |
|
| 481 |
// Test case 3: Schema with additional properties that is an array |
| 482 |
proxy := base.CreateSchemaProxy(&base.Schema{ |
| 483 |
Type: []string{"array"}, |
| 484 |
}) |
| 485 |
|
| 486 |
schema = &base.Schema{ |
| 487 |
AdditionalProperties: &base.DynamicValue[*base.SchemaProxy, bool]{ |
| 488 |
A: proxy, |
| 489 |
}, |
| 490 |
} |
| 491 |
decoded = ConstructParamMapFromDeepObjectEncoding(values, schema) |
| 492 |
require.NotNil(t, decoded) |
| 493 |
require.Equal(t, []interface{}{"value1"}, decoded["key1"].(map[string]interface{})["prop1"]) |
| 494 |
require.Equal(t, []interface{}{int64(123)}, decoded["key2"].(map[string]interface{})["prop2"]) |
| 495 |
require.Equal(t, []interface{}{int64(456), int64(789)}, decoded["key3"].(map[string]interface{})["prop3"]) |
| 496 |
|
| 497 |
// Test case 4: Adding a value to an existing key in the decoded map |
| 498 |
valuesWithDup := []*QueryParam{ |
| 499 |
{Key: "key1", Values: []string{"value2"}, Property: "prop1"}, |
| 500 |
{Key: "key2", Values: []string{"456"}, Property: "prop2"}, |
| 501 |
} |
| 502 |
decoded = ConstructParamMapFromDeepObjectEncoding(valuesWithDup, nil) |
| 503 |
require.NotNil(t, decoded) |
| 504 |
require.Equal(t, "value2", decoded["key1"].(map[string]interface{})["prop1"]) |
| 505 |
require.Equal(t, int64(456), decoded["key2"].(map[string]interface{})["prop2"]) |
| 506 |
|
| 507 |
// Test case 5: Schema is not an array (standard object) |
| 508 |
nonArraySchema := &base.Schema{Type: []string{"object"}} |
| 509 |
decoded = ConstructParamMapFromDeepObjectEncoding(values, nonArraySchema) |
| 510 |
require.NotNil(t, decoded) |
| 511 |
require.Equal(t, "value1", decoded["key1"].(map[string]interface{})["prop1"]) |
| 512 |
require.Equal(t, int64(123), decoded["key2"].(map[string]interface{})["prop2"]) |
| 513 |
require.Equal(t, int64(456), decoded["key3"].(map[string]interface{})["prop3"]) |
| 514 |
} |
| 515 |
|
| 516 |
func TestConstructParamMapFromDeepObjectEncoding_ElseCase(t *testing.T) { |
| 517 |
arraySchema := &base.Schema{Type: []string{"array"}, AdditionalProperties: &base.DynamicValue[*base.SchemaProxy, bool]{ |
| 518 |
A: base.CreateSchemaProxy(&base.Schema{Type: []string{"array"}}), |
| 519 |
}} |
| 520 |
newValues := []*QueryParam{ |
| 521 |
{Key: "key1", Values: []string{"456", "789"}, Property: "prop3"}, |
| 522 |
{Key: "key1", Values: []string{"999", "888"}, Property: "prop3"}, |
| 523 |
} |
| 524 |
decoded := ConstructParamMapFromDeepObjectEncoding(newValues, arraySchema) |
| 525 |
require.Equal(t, []interface{}{int64(999), int64(888)}, decoded["key1"].(map[string]interface{})["prop3"]) |
| 526 |
|
| 527 |
arraySchema = &base.Schema{Type: []string{"integer"}, AdditionalProperties: &base.DynamicValue[*base.SchemaProxy, bool]{ |
| 528 |
A: base.CreateSchemaProxy(&base.Schema{Type: []string{"integer"}}), |
| 529 |
}} |
| 530 |
newValues = []*QueryParam{ |
| 531 |
{Key: "key1", Values: []string{"456", "789"}, Property: "prop3"}, |
| 532 |
{Key: "key1", Values: []string{"999", "888"}, Property: "prop3"}, |
| 533 |
} |
| 534 |
decoded = ConstructParamMapFromDeepObjectEncoding(newValues, arraySchema) |
| 535 |
require.Equal(t, int64(999), decoded["key1"].(map[string]interface{})["prop3"]) |
| 536 |
} |
| 537 |
|
| 538 |
func TestParseDeepObjectKey(t *testing.T) { |
| 539 |
tests := []struct { |
| 540 |
name string |
| 541 |
key string |
| 542 |
expectedBase string |
| 543 |
expectedPath []string |
| 544 |
expectedOK bool |
| 545 |
}{ |
| 546 |
{ |
| 547 |
name: "flat", |
| 548 |
key: "obj[root]", |
| 549 |
expectedBase: "obj", |
| 550 |
expectedPath: []string{"root"}, |
| 551 |
expectedOK: true, |
| 552 |
}, |
| 553 |
{ |
| 554 |
name: "nested", |
| 555 |
key: "obj[nested][child]", |
| 556 |
expectedBase: "obj", |
| 557 |
expectedPath: []string{"nested", "child"}, |
| 558 |
expectedOK: true, |
| 559 |
}, |
| 560 |
{ |
| 561 |
name: "plain key", |
| 562 |
key: "obj", |
| 563 |
expectedOK: false, |
| 564 |
}, |
| 565 |
{ |
| 566 |
name: "empty segment", |
| 567 |
key: "obj[]", |
| 568 |
expectedOK: false, |
| 569 |
}, |
| 570 |
{ |
| 571 |
name: "trailing text", |
| 572 |
key: "obj[root]extra", |
| 573 |
expectedOK: false, |
| 574 |
}, |
| 575 |
{ |
| 576 |
name: "missing closing bracket", |
| 577 |
key: "obj[root", |
| 578 |
expectedOK: false, |
| 579 |
}, |
| 580 |
} |
| 581 |
|
| 582 |
for _, tc := range tests { |
| 583 |
t.Run(tc.name, func(t *testing.T) { |
| 584 |
baseName, propertyPath, ok := ParseDeepObjectKey(tc.key) |
| 585 |
require.Equal(t, tc.expectedOK, ok) |
| 586 |
require.Equal(t, tc.expectedBase, baseName) |
| 587 |
require.Equal(t, tc.expectedPath, propertyPath) |
| 588 |
}) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
func TestConstructParamMapFromDeepObjectEncoding_NestedObject(t *testing.T) { |
| 593 |
sch := &base.Schema{ |
| 594 |
Type: []string{"object"}, |
| 595 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 596 |
"root": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 597 |
"nested": base.CreateSchemaProxy(&base.Schema{ |
| 598 |
Type: []string{"object"}, |
| 599 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 600 |
"child": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 601 |
"count": base.CreateSchemaProxy(&base.Schema{Type: []string{"integer"}}), |
| 602 |
}), |
| 603 |
}), |
| 604 |
}), |
| 605 |
} |
| 606 |
values := []*QueryParam{ |
| 607 |
{Key: "obj", Values: []string{"test1"}, Property: "root", PropertyPath: []string{"root"}}, |
| 608 |
{Key: "obj", Values: []string{"10"}, Property: "nested", PropertyPath: []string{"nested", "child"}}, |
| 609 |
{Key: "obj", Values: []string{"42"}, Property: "nested", PropertyPath: []string{"nested", "count"}}, |
| 610 |
} |
| 611 |
|
| 612 |
decoded := ConstructParamMapFromDeepObjectEncoding(values, sch) |
| 613 |
obj := decoded["obj"].(map[string]interface{}) |
| 614 |
nested := obj["nested"].(map[string]interface{}) |
| 615 |
|
| 616 |
require.Equal(t, "test1", obj["root"]) |
| 617 |
require.Equal(t, "10", nested["child"]) |
| 618 |
require.Equal(t, int64(42), nested["count"]) |
| 619 |
} |
| 620 |
|
| 621 |
func TestConstructParamMapFromDeepObjectEncoding_NestedArray(t *testing.T) { |
| 622 |
sch := &base.Schema{ |
| 623 |
Type: []string{"object"}, |
| 624 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 625 |
"nested": base.CreateSchemaProxy(&base.Schema{ |
| 626 |
Type: []string{"object"}, |
| 627 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 628 |
"tags": base.CreateSchemaProxy(&base.Schema{ |
| 629 |
Type: []string{"array"}, |
| 630 |
Items: &base.DynamicValue[*base.SchemaProxy, bool]{ |
| 631 |
A: base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 632 |
}, |
| 633 |
}), |
| 634 |
}), |
| 635 |
}), |
| 636 |
}), |
| 637 |
} |
| 638 |
values := []*QueryParam{ |
| 639 |
{Key: "obj", Values: []string{"123", "456"}, Property: "nested", PropertyPath: []string{"nested", "tags"}}, |
| 640 |
} |
| 641 |
|
| 642 |
decoded := ConstructParamMapFromDeepObjectEncoding(values, sch) |
| 643 |
obj := decoded["obj"].(map[string]interface{}) |
| 644 |
nested := obj["nested"].(map[string]interface{}) |
| 645 |
|
| 646 |
require.Equal(t, []interface{}{"123", "456"}, nested["tags"]) |
| 647 |
require.True(t, DeepObjectAllowsMultipleValues(sch, values[0])) |
| 648 |
} |
| 649 |
|
| 650 |
func TestConstructParamMapFromDeepObjectEncoding_NestedAdditionalPropertiesArray(t *testing.T) { |
| 651 |
sch := &base.Schema{ |
| 652 |
Type: []string{"object"}, |
| 653 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 654 |
"filters": base.CreateSchemaProxy(&base.Schema{ |
| 655 |
Type: []string{"object"}, |
| 656 |
AdditionalProperties: &base.DynamicValue[*base.SchemaProxy, bool]{ |
| 657 |
A: base.CreateSchemaProxy(&base.Schema{ |
| 658 |
Type: []string{"array"}, |
| 659 |
Items: &base.DynamicValue[*base.SchemaProxy, bool]{ |
| 660 |
A: base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 661 |
}, |
| 662 |
}), |
| 663 |
}, |
| 664 |
}), |
| 665 |
}), |
| 666 |
} |
| 667 |
values := []*QueryParam{ |
| 668 |
{Key: "obj", Values: []string{"123", "456"}, Property: "filters", PropertyPath: []string{"filters", "tag"}}, |
| 669 |
} |
| 670 |
|
| 671 |
decoded := ConstructParamMapFromDeepObjectEncoding(values, sch) |
| 672 |
obj := decoded["obj"].(map[string]interface{}) |
| 673 |
filters := obj["filters"].(map[string]interface{}) |
| 674 |
|
| 675 |
require.Equal(t, []interface{}{"123", "456"}, filters["tag"]) |
| 676 |
require.True(t, DeepObjectAllowsMultipleValues(sch, values[0])) |
| 677 |
} |
| 678 |
|
| 679 |
func TestDeepObjectPathConflict(t *testing.T) { |
| 680 |
tests := []struct { |
| 681 |
name string |
| 682 |
values []*QueryParam |
| 683 |
expect bool |
| 684 |
prefixPath []string |
| 685 |
nestedPath []string |
| 686 |
}{ |
| 687 |
{ |
| 688 |
name: "scalar before nested", |
| 689 |
values: []*QueryParam{ |
| 690 |
{Key: "obj", Values: []string{"bad"}, Property: "nested", PropertyPath: []string{"nested"}}, |
| 691 |
{Key: "obj", Values: []string{"ok"}, Property: "nested", PropertyPath: []string{"nested", "child"}}, |
| 692 |
}, |
| 693 |
expect: true, |
| 694 |
prefixPath: []string{"nested"}, |
| 695 |
nestedPath: []string{"nested", "child"}, |
| 696 |
}, |
| 697 |
{ |
| 698 |
name: "nested before scalar", |
| 699 |
values: []*QueryParam{ |
| 700 |
{Key: "obj", Values: []string{"ok"}, Property: "nested", PropertyPath: []string{"nested", "child"}}, |
| 701 |
{Key: "obj", Values: []string{"bad"}, Property: "nested", PropertyPath: []string{"nested"}}, |
| 702 |
}, |
| 703 |
expect: true, |
| 704 |
prefixPath: []string{"nested"}, |
| 705 |
nestedPath: []string{"nested", "child"}, |
| 706 |
}, |
| 707 |
{ |
| 708 |
name: "same array path", |
| 709 |
values: []*QueryParam{ |
| 710 |
{Key: "obj", Values: []string{"alpha"}, Property: "nested", PropertyPath: []string{"nested", "tags"}}, |
| 711 |
{Key: "obj", Values: []string{"beta"}, Property: "nested", PropertyPath: []string{"nested", "tags"}}, |
| 712 |
}, |
| 713 |
}, |
| 714 |
{ |
| 715 |
name: "sibling nested paths", |
| 716 |
values: []*QueryParam{ |
| 717 |
{Key: "obj", Values: []string{"ok"}, Property: "nested", PropertyPath: []string{"nested", "child"}}, |
| 718 |
{Key: "obj", Values: []string{"ok"}, Property: "nested", PropertyPath: []string{"nested", "other"}}, |
| 719 |
}, |
| 720 |
}, |
| 721 |
{ |
| 722 |
name: "different roots", |
| 723 |
values: []*QueryParam{ |
| 724 |
{Key: "obj", Values: []string{"ok"}, Property: "nested", PropertyPath: []string{"nested"}}, |
| 725 |
{Key: "obj", Values: []string{"ok"}, Property: "other", PropertyPath: []string{"other", "child"}}, |
| 726 |
}, |
| 727 |
}, |
| 728 |
{ |
| 729 |
name: "empty path is ignored", |
| 730 |
values: []*QueryParam{ |
| 731 |
{Key: "obj", Values: []string{"ignored"}}, |
| 732 |
{Key: "obj", Values: []string{"ok"}, Property: "nested", PropertyPath: []string{"nested", "child"}}, |
| 733 |
}, |
| 734 |
}, |
| 735 |
} |
| 736 |
|
| 737 |
for _, tc := range tests { |
| 738 |
t.Run(tc.name, func(t *testing.T) { |
| 739 |
prefixParam, nestedParam, ok := DeepObjectPathConflict(tc.values) |
| 740 |
require.Equal(t, tc.expect, ok) |
| 741 |
if !tc.expect { |
| 742 |
return |
| 743 |
} |
| 744 |
require.Equal(t, tc.prefixPath, prefixParam.PropertyPath) |
| 745 |
require.Equal(t, tc.nestedPath, nestedParam.PropertyPath) |
| 746 |
}) |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
func TestQueryParamDeepObjectPath(t *testing.T) { |
| 751 |
require.Nil(t, queryParamDeepObjectPath(nil)) |
| 752 |
require.Nil(t, queryParamDeepObjectPath(&QueryParam{})) |
| 753 |
require.Equal(t, []string{"root"}, queryParamDeepObjectPath(&QueryParam{Property: "root"})) |
| 754 |
require.Equal(t, []string{"root", "child"}, queryParamDeepObjectPath(&QueryParam{ |
| 755 |
Property: "root", |
| 756 |
PropertyPath: []string{"root", "child"}, |
| 757 |
})) |
| 758 |
} |
| 759 |
|
| 760 |
func TestSetNestedDeepObjectValue_PreservesConflicts(t *testing.T) { |
| 761 |
t.Run("empty path", func(t *testing.T) { |
| 762 |
target := make(map[string]interface{}) |
| 763 |
|
| 764 |
require.True(t, setNestedDeepObjectValue(target, nil, "root")) |
| 765 |
require.Equal(t, "root", target[""]) |
| 766 |
}) |
| 767 |
|
| 768 |
t.Run("scalar before nested", func(t *testing.T) { |
| 769 |
target := make(map[string]interface{}) |
| 770 |
|
| 771 |
require.True(t, setNestedDeepObjectValue(target, []string{"nested"}, "bad")) |
| 772 |
require.False(t, setNestedDeepObjectValue(target, []string{"nested", "child"}, "ok")) |
| 773 |
require.IsType(t, []interface{}{}, target["nested"]) |
| 774 |
}) |
| 775 |
|
| 776 |
t.Run("nested before scalar", func(t *testing.T) { |
| 777 |
target := make(map[string]interface{}) |
| 778 |
|
| 779 |
require.True(t, setNestedDeepObjectValue(target, []string{"nested", "child"}, "ok")) |
| 780 |
require.False(t, setNestedDeepObjectValue(target, []string{"nested"}, "bad")) |
| 781 |
require.IsType(t, []interface{}{}, target["nested"]) |
| 782 |
}) |
| 783 |
} |
| 784 |
|
| 785 |
func TestConstructParamMapFromDeepObjectEncoding_NestedPathConflict(t *testing.T) { |
| 786 |
tests := []struct { |
| 787 |
name string |
| 788 |
values []*QueryParam |
| 789 |
}{ |
| 790 |
{ |
| 791 |
name: "scalar before nested", |
| 792 |
values: []*QueryParam{ |
| 793 |
{Key: "obj", Values: []string{"bad"}, Property: "nested", PropertyPath: []string{"nested"}}, |
| 794 |
{Key: "obj", Values: []string{"ok"}, Property: "nested", PropertyPath: []string{"nested", "child"}}, |
| 795 |
}, |
| 796 |
}, |
| 797 |
{ |
| 798 |
name: "nested before scalar", |
| 799 |
values: []*QueryParam{ |
| 800 |
{Key: "obj", Values: []string{"ok"}, Property: "nested", PropertyPath: []string{"nested", "child"}}, |
| 801 |
{Key: "obj", Values: []string{"bad"}, Property: "nested", PropertyPath: []string{"nested"}}, |
| 802 |
}, |
| 803 |
}, |
| 804 |
} |
| 805 |
|
| 806 |
for _, tc := range tests { |
| 807 |
t.Run(tc.name, func(t *testing.T) { |
| 808 |
decoded := ConstructParamMapFromDeepObjectEncoding(tc.values, nil) |
| 809 |
obj := decoded["obj"].(map[string]interface{}) |
| 810 |
|
| 811 |
require.IsType(t, []interface{}{}, obj["nested"]) |
| 812 |
}) |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
func TestConstructKVFromLabelEncoding(t *testing.T) { |
| 817 |
// Test case 1: Empty input string |
| 818 |
values := "" |
| 819 |
props := ConstructKVFromLabelEncoding(values) |
| 820 |
require.NotNil(t, props) |
| 821 |
require.Empty(t, props) |
| 822 |
|
| 823 |
// Test case 2: Single valid key-value pair |
| 824 |
values = "key1=value1" |
| 825 |
props = ConstructKVFromLabelEncoding(values) |
| 826 |
require.NotNil(t, props) |
| 827 |
require.Equal(t, "value1", props["key1"]) |
| 828 |
|
| 829 |
// Test case 3: Multiple valid key-value pairs |
| 830 |
values = "key1=value1.key2=value2" |
| 831 |
props = ConstructKVFromLabelEncoding(values) |
| 832 |
require.NotNil(t, props) |
| 833 |
require.Equal(t, "value1", props["key1"]) |
| 834 |
require.Equal(t, "value2", props["key2"]) |
| 835 |
|
| 836 |
// Test case 4: Invalid key-value pair (missing equals) |
| 837 |
values = "key1=value1.key2" |
| 838 |
props = ConstructKVFromLabelEncoding(values) |
| 839 |
require.NotNil(t, props) |
| 840 |
require.Equal(t, "value1", props["key1"]) |
| 841 |
require.NotContains(t, props, "key2") // key2 should be ignored due to invalid format |
| 842 |
|
| 843 |
// Test case 5: Key-value pair where value needs to be cast to int and bool |
| 844 |
values = "key1=123.key2=true" |
| 845 |
props = ConstructKVFromLabelEncoding(values) |
| 846 |
require.NotNil(t, props) |
| 847 |
require.Equal(t, int64(123), props["key1"]) // cast to int |
| 848 |
require.Equal(t, true, props["key2"]) // cast to bool |
| 849 |
|
| 850 |
// Test case 6: Handle multiple valid and invalid key-value pairs |
| 851 |
values = "key1=value1.key2.key3=123.key4=true" |
| 852 |
props = ConstructKVFromLabelEncoding(values) |
| 853 |
require.NotNil(t, props) |
| 854 |
require.Equal(t, "value1", props["key1"]) // valid |
| 855 |
require.Equal(t, int64(123), props["key3"]) // valid |
| 856 |
require.Equal(t, true, props["key4"]) // valid |
| 857 |
require.NotContains(t, props, "key2") // invalid, missing value |
| 858 |
} |
| 859 |
|
| 860 |
func TestConstructParamMapFromQueryParamInput(t *testing.T) { |
| 861 |
// Test case 1: Empty input map |
| 862 |
values := map[string][]*QueryParam{} |
| 863 |
decoded := ConstructParamMapFromQueryParamInput(values) |
| 864 |
require.NotNil(t, decoded) |
| 865 |
require.Empty(t, decoded) |
| 866 |
|
| 867 |
// Test case 2: Single entry in the input map |
| 868 |
values = map[string][]*QueryParam{ |
| 869 |
"param1": { |
| 870 |
{Key: "param1", Values: []string{"value1"}}, |
| 871 |
}, |
| 872 |
} |
| 873 |
decoded = ConstructParamMapFromQueryParamInput(values) |
| 874 |
require.NotNil(t, decoded) |
| 875 |
require.Equal(t, "value1", decoded["param1"]) |
| 876 |
|
| 877 |
// Test case 3: Multiple entries in the input map |
| 878 |
values = map[string][]*QueryParam{ |
| 879 |
"param1": { |
| 880 |
{Key: "param1", Values: []string{"value1"}}, |
| 881 |
}, |
| 882 |
"param2": { |
| 883 |
{Key: "param2", Values: []string{"123"}}, |
| 884 |
}, |
| 885 |
"param3": { |
| 886 |
{Key: "param3", Values: []string{"true"}}, |
| 887 |
}, |
| 888 |
} |
| 889 |
decoded = ConstructParamMapFromQueryParamInput(values) |
| 890 |
require.NotNil(t, decoded) |
| 891 |
require.Equal(t, "value1", decoded["param1"]) |
| 892 |
require.Equal(t, int64(123), decoded["param2"]) // cast to int |
| 893 |
require.Equal(t, true, decoded["param3"]) // cast to bool |
| 894 |
|
| 895 |
// Test case 4: Handle multiple values but only the first value is used |
| 896 |
values = map[string][]*QueryParam{ |
| 897 |
"param1": { |
| 898 |
{Key: "param1", Values: []string{"first", "second"}}, |
| 899 |
}, |
| 900 |
} |
| 901 |
decoded = ConstructParamMapFromQueryParamInput(values) |
| 902 |
require.NotNil(t, decoded) |
| 903 |
require.Equal(t, "first", decoded["param1"]) // Only the first value is used |
| 904 |
|
| 905 |
// Test case 5: Handle different types of values |
| 906 |
values = map[string][]*QueryParam{ |
| 907 |
"intParam": { |
| 908 |
{Key: "intParam", Values: []string{"42"}}, |
| 909 |
}, |
| 910 |
"boolParam": { |
| 911 |
{Key: "boolParam", Values: []string{"false"}}, |
| 912 |
}, |
| 913 |
"stringParam": { |
| 914 |
{Key: "stringParam", Values: []string{"hello"}}, |
| 915 |
}, |
| 916 |
} |
| 917 |
decoded = ConstructParamMapFromQueryParamInput(values) |
| 918 |
require.NotNil(t, decoded) |
| 919 |
require.Equal(t, int64(42), decoded["intParam"]) |
| 920 |
require.Equal(t, false, decoded["boolParam"]) |
| 921 |
require.Equal(t, "hello", decoded["stringParam"]) |
| 922 |
} |
| 923 |
|
| 924 |
// Test ConstructParamMapFromPipeEncoding |
| 925 |
func TestConstructParamMapFromPipeEncoding(t *testing.T) { |
| 926 |
params := []*QueryParam{ |
| 927 |
{Key: "key1", Values: []string{"name|value"}}, |
| 928 |
} |
| 929 |
result := ConstructParamMapFromPipeEncoding(params) |
| 930 |
require.Equal(t, "value", result["key1"].(map[string]interface{})["name"]) |
| 931 |
} |
| 932 |
|
| 933 |
// Test ConstructParamMapFromSpaceEncoding |
| 934 |
func TestConstructParamMapFromSpaceEncoding(t *testing.T) { |
| 935 |
params := []*QueryParam{ |
| 936 |
{Key: "key1", Values: []string{"name value"}}, |
| 937 |
} |
| 938 |
result := ConstructParamMapFromSpaceEncoding(params) |
| 939 |
require.Equal(t, "value", result["key1"].(map[string]interface{})["name"]) |
| 940 |
} |
| 941 |
|
| 942 |
// Test ConstructMapFromCSV |
| 943 |
func TestConstructMapFromCSV(t *testing.T) { |
| 944 |
result := ConstructMapFromCSV("key1,value1,key2,value2") |
| 945 |
require.Equal(t, "value1", result["key1"]) |
| 946 |
require.Equal(t, "value2", result["key2"]) |
| 947 |
|
| 948 |
// add odd number of keys/values |
| 949 |
result = ConstructMapFromCSV("key1,value1,key2") |
| 950 |
require.Equal(t, "value1", result["key1"]) |
| 951 |
} |
| 952 |
|
| 953 |
// Test ConstructKVFromCSV |
| 954 |
func TestConstructKVFromCSV(t *testing.T) { |
| 955 |
result := ConstructKVFromCSV("key1=value1,key2=value2") |
| 956 |
require.Equal(t, "value1", result["key1"]) |
| 957 |
require.Equal(t, "value2", result["key2"]) |
| 958 |
} |
| 959 |
|
| 960 |
// Test CollapseCSVIntoFormStyle |
| 961 |
func TestCollapseCSVIntoFormStyle(t *testing.T) { |
| 962 |
result := CollapseCSVIntoFormStyle("key", "value1,value2") |
| 963 |
require.Equal(t, "&key=value1&key=value2", result) |
| 964 |
} |
| 965 |
|
| 966 |
// Test CollapseCSVIntoSpaceDelimitedStyle |
| 967 |
func TestCollapseCSVIntoSpaceDelimitedStyle(t *testing.T) { |
| 968 |
result := CollapseCSVIntoSpaceDelimitedStyle("key", []string{"value1", "value2"}) |
| 969 |
require.Equal(t, "key=value1%20value2", result) |
| 970 |
} |
| 971 |
|
| 972 |
// Test CollapseCSVIntoPipeDelimitedStyle |
| 973 |
func TestCollapseCSVIntoPipeDelimitedStyle(t *testing.T) { |
| 974 |
result := CollapseCSVIntoPipeDelimitedStyle("key", []string{"value1", "value2"}) |
| 975 |
require.Equal(t, "key=value1|value2", result) |
| 976 |
} |
| 977 |
|
| 978 |
// Test DoesFormParamContainDelimiter |
| 979 |
func TestDoesFormParamContainDelimiter(t *testing.T) { |
| 980 |
require.True(t, DoesFormParamContainDelimiter("value1,value2", "")) |
| 981 |
require.False(t, DoesFormParamContainDelimiter("value1 value2", "")) |
| 982 |
} |
| 983 |
|
| 984 |
// Test ExplodeQueryValue |
| 985 |
func TestExplodeQueryValue(t *testing.T) { |
| 986 |
require.Equal(t, []string{"value1", "value2"}, ExplodeQueryValue("value1,value2", "")) |
| 987 |
require.Equal(t, []string{"value1", "value2"}, ExplodeQueryValue("value1 value2", "spaceDelimited")) |
| 988 |
require.Equal(t, []string{"value1", "value2"}, ExplodeQueryValue("value1|value2", "pipeDelimited")) |
| 989 |
} |
| 990 |
|
| 991 |
func TestConstructKVFromMatrixCSV(t *testing.T) { |
| 992 |
// Test case 1: Empty input string |
| 993 |
values := "" |
| 994 |
props := ConstructKVFromMatrixCSV(values) |
| 995 |
require.NotNil(t, props) |
| 996 |
require.Empty(t, props) |
| 997 |
|
| 998 |
// Test case 2: Single valid key-value pair |
| 999 |
values = "key1=value1" |
| 1000 |
props = ConstructKVFromMatrixCSV(values) |
| 1001 |
require.NotNil(t, props) |
| 1002 |
require.Equal(t, "value1", props["key1"]) |
| 1003 |
|
| 1004 |
// Test case 3: Multiple valid key-value pairs |
| 1005 |
values = "key1=value1;key2=value2" |
| 1006 |
props = ConstructKVFromMatrixCSV(values) |
| 1007 |
require.NotNil(t, props) |
| 1008 |
require.Equal(t, "value1", props["key1"]) |
| 1009 |
require.Equal(t, "value2", props["key2"]) |
| 1010 |
|
| 1011 |
// Test case 4: Invalid key-value pair (missing equals) |
| 1012 |
values = "key1=value1;key2" |
| 1013 |
props = ConstructKVFromMatrixCSV(values) |
| 1014 |
require.NotNil(t, props) |
| 1015 |
require.Equal(t, "value1", props["key1"]) |
| 1016 |
require.NotContains(t, props, "key2") // key2 should be ignored due to invalid format |
| 1017 |
|
| 1018 |
// Test case 5: Key-value pair where value needs to be cast to int and bool |
| 1019 |
values = "key1=123;key2=true" |
| 1020 |
props = ConstructKVFromMatrixCSV(values) |
| 1021 |
require.NotNil(t, props) |
| 1022 |
require.Equal(t, int64(123), props["key1"]) // cast to int |
| 1023 |
require.Equal(t, true, props["key2"]) // cast to bool |
| 1024 |
|
| 1025 |
// Test case 6: Handle multiple valid and invalid key-value pairs |
| 1026 |
values = "key1=value1;key2;key3=456;key4=false" |
| 1027 |
props = ConstructKVFromMatrixCSV(values) |
| 1028 |
require.NotNil(t, props) |
| 1029 |
require.Equal(t, "value1", props["key1"]) // valid |
| 1030 |
require.Equal(t, int64(456), props["key3"]) // valid |
| 1031 |
require.Equal(t, false, props["key4"]) // valid |
| 1032 |
require.NotContains(t, props, "key2") // invalid, missing value |
| 1033 |
} |
| 1034 |
|
| 1035 |
func TestConstructParamMapFromFormEncodingArray(t *testing.T) { |
| 1036 |
// Test case 1: Empty input |
| 1037 |
values := []*QueryParam{} |
| 1038 |
decoded := ConstructParamMapFromFormEncodingArray(values) |
| 1039 |
require.NotNil(t, decoded) |
| 1040 |
require.Empty(t, decoded) |
| 1041 |
|
| 1042 |
// Test case 2: Single QueryParam with valid key-value pairs |
| 1043 |
values = []*QueryParam{ |
| 1044 |
{ |
| 1045 |
Key: "param1", |
| 1046 |
Values: []string{"key1,value1,key2,value2"}, |
| 1047 |
}, |
| 1048 |
} |
| 1049 |
decoded = ConstructParamMapFromFormEncodingArray(values) |
| 1050 |
require.NotNil(t, decoded) |
| 1051 |
require.Contains(t, decoded, "param1") |
| 1052 |
require.Equal(t, "value1", decoded["param1"].(map[string]interface{})["key1"]) |
| 1053 |
require.Equal(t, "value2", decoded["param1"].(map[string]interface{})["key2"]) |
| 1054 |
|
| 1055 |
// Test case 3: Multiple QueryParam entries |
| 1056 |
values = []*QueryParam{ |
| 1057 |
{ |
| 1058 |
Key: "param1", |
| 1059 |
Values: []string{"key1,value1"}, |
| 1060 |
}, |
| 1061 |
{ |
| 1062 |
Key: "param2", |
| 1063 |
Values: []string{"key3,value3,key4,value4"}, |
| 1064 |
}, |
| 1065 |
} |
| 1066 |
decoded = ConstructParamMapFromFormEncodingArray(values) |
| 1067 |
require.NotNil(t, decoded) |
| 1068 |
require.Contains(t, decoded, "param1") |
| 1069 |
require.Equal(t, "value1", decoded["param1"].(map[string]interface{})["key1"]) |
| 1070 |
require.Equal(t, "value3", decoded["param2"].(map[string]interface{})["key3"]) |
| 1071 |
require.Equal(t, "value4", decoded["param2"].(map[string]interface{})["key4"]) |
| 1072 |
|
| 1073 |
// Test case 4: Odd number of values (incomplete key-value pair) |
| 1074 |
values = []*QueryParam{ |
| 1075 |
{ |
| 1076 |
Key: "param1", |
| 1077 |
Values: []string{"key1,value1,key2"}, |
| 1078 |
}, |
| 1079 |
} |
| 1080 |
decoded = ConstructParamMapFromFormEncodingArray(values) |
| 1081 |
require.NotNil(t, decoded) |
| 1082 |
require.Contains(t, decoded, "param1") |
| 1083 |
require.Equal(t, "value1", decoded["param1"].(map[string]interface{})["key1"]) |
| 1084 |
require.NotContains(t, decoded["param1"].(map[string]interface{}), "key2") // Invalid, no value for key2 |
| 1085 |
|
| 1086 |
// Test case 5: Casting different types (int, bool, string) |
| 1087 |
values = []*QueryParam{ |
| 1088 |
{ |
| 1089 |
Key: "param1", |
| 1090 |
Values: []string{"key1,123,key2,true,key3,hello"}, |
| 1091 |
}, |
| 1092 |
} |
| 1093 |
decoded = ConstructParamMapFromFormEncodingArray(values) |
| 1094 |
require.NotNil(t, decoded) |
| 1095 |
require.Contains(t, decoded, "param1") |
| 1096 |
require.Equal(t, int64(123), decoded["param1"].(map[string]interface{})["key1"]) // cast to int |
| 1097 |
require.Equal(t, true, decoded["param1"].(map[string]interface{})["key2"]) // cast to bool |
| 1098 |
require.Equal(t, "hello", decoded["param1"].(map[string]interface{})["key3"]) // string remains string |
| 1099 |
} |
| 1100 |
|
| 1101 |
func TestCastWithSchema(t *testing.T) { |
| 1102 |
t.Run("returns string unchanged when schema property type is string", func(t *testing.T) { |
| 1103 |
sch := &base.Schema{ |
| 1104 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1105 |
"item_count": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1106 |
}), |
| 1107 |
} |
| 1108 |
result := castWithSchema("10", sch, "item_count") |
| 1109 |
require.Equal(t, "10", result) |
| 1110 |
}) |
| 1111 |
|
| 1112 |
t.Run("casts to int64 when schema property type is integer", func(t *testing.T) { |
| 1113 |
sch := &base.Schema{ |
| 1114 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1115 |
"count": base.CreateSchemaProxy(&base.Schema{Type: []string{"integer"}}), |
| 1116 |
}), |
| 1117 |
} |
| 1118 |
result := castWithSchema("10", sch, "count") |
| 1119 |
require.Equal(t, int64(10), result) |
| 1120 |
}) |
| 1121 |
|
| 1122 |
t.Run("falls back to cast when no schema provided", func(t *testing.T) { |
| 1123 |
result := castWithSchema("10", nil, "anything") |
| 1124 |
require.Equal(t, int64(10), result) |
| 1125 |
}) |
| 1126 |
|
| 1127 |
t.Run("falls back to cast when property not found in schema", func(t *testing.T) { |
| 1128 |
sch := &base.Schema{ |
| 1129 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1130 |
"other": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1131 |
}), |
| 1132 |
} |
| 1133 |
result := castWithSchema("10", sch, "missing") |
| 1134 |
require.Equal(t, int64(10), result) |
| 1135 |
}) |
| 1136 |
|
| 1137 |
t.Run("falls back to cast when schema has no properties", func(t *testing.T) { |
| 1138 |
sch := &base.Schema{Type: []string{"object"}} |
| 1139 |
result := castWithSchema("10", sch, "anything") |
| 1140 |
require.Equal(t, int64(10), result) |
| 1141 |
}) |
| 1142 |
} |
| 1143 |
|
| 1144 |
func TestConstructParamMapFromQueryParamInputWithSchema(t *testing.T) { |
| 1145 |
t.Run("preserves string '10' when schema says string", func(t *testing.T) { |
| 1146 |
sch := &base.Schema{ |
| 1147 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1148 |
"item_count": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1149 |
"search_term": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1150 |
}), |
| 1151 |
} |
| 1152 |
values := map[string][]*QueryParam{ |
| 1153 |
"item_count": { |
| 1154 |
{Key: "item_count", Values: []string{"10"}}, |
| 1155 |
}, |
| 1156 |
"search_term": { |
| 1157 |
{Key: "search_term", Values: []string{"foo"}}, |
| 1158 |
}, |
| 1159 |
} |
| 1160 |
decoded := ConstructParamMapFromQueryParamInputWithSchema(values, sch) |
| 1161 |
require.Equal(t, "10", decoded["item_count"]) |
| 1162 |
require.Equal(t, "foo", decoded["search_term"]) |
| 1163 |
}) |
| 1164 |
|
| 1165 |
t.Run("casts numeric values when schema says integer", func(t *testing.T) { |
| 1166 |
sch := &base.Schema{ |
| 1167 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1168 |
"count": base.CreateSchemaProxy(&base.Schema{Type: []string{"integer"}}), |
| 1169 |
}), |
| 1170 |
} |
| 1171 |
values := map[string][]*QueryParam{ |
| 1172 |
"count": { |
| 1173 |
{Key: "count", Values: []string{"42"}}, |
| 1174 |
}, |
| 1175 |
} |
| 1176 |
decoded := ConstructParamMapFromQueryParamInputWithSchema(values, sch) |
| 1177 |
require.Equal(t, int64(42), decoded["count"]) |
| 1178 |
}) |
| 1179 |
|
| 1180 |
t.Run("falls back to heuristic when no schema", func(t *testing.T) { |
| 1181 |
values := map[string][]*QueryParam{ |
| 1182 |
"count": { |
| 1183 |
{Key: "count", Values: []string{"42"}}, |
| 1184 |
}, |
| 1185 |
} |
| 1186 |
decoded := ConstructParamMapFromQueryParamInputWithSchema(values, nil) |
| 1187 |
require.Equal(t, int64(42), decoded["count"]) |
| 1188 |
}) |
| 1189 |
} |
| 1190 |
|
| 1191 |
func TestConstructParamMapFromDeepObjectEncoding_WithSchema(t *testing.T) { |
| 1192 |
t.Run("preserves string values when schema property type is string", func(t *testing.T) { |
| 1193 |
sch := &base.Schema{ |
| 1194 |
Type: []string{"object"}, |
| 1195 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1196 |
"prop1": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1197 |
}), |
| 1198 |
} |
| 1199 |
values := []*QueryParam{ |
| 1200 |
{Key: "key1", Values: []string{"123"}, Property: "prop1"}, |
| 1201 |
} |
| 1202 |
decoded := ConstructParamMapFromDeepObjectEncoding(values, sch) |
| 1203 |
require.Equal(t, "123", decoded["key1"].(map[string]interface{})["prop1"]) |
| 1204 |
}) |
| 1205 |
|
| 1206 |
t.Run("preserves string values when root array items are strings", func(t *testing.T) { |
| 1207 |
sch := &base.Schema{ |
| 1208 |
Type: []string{"array"}, |
| 1209 |
Items: &base.DynamicValue[*base.SchemaProxy, bool]{ |
| 1210 |
A: base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1211 |
}, |
| 1212 |
} |
| 1213 |
|
| 1214 |
require.Equal(t, "123", castWithSchema("123", sch, "unknown")) |
| 1215 |
}) |
| 1216 |
} |
| 1217 |
|
| 1218 |
func TestConstructParamMapFromPipeEncodingWithSchema(t *testing.T) { |
| 1219 |
sch := &base.Schema{ |
| 1220 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1221 |
"name": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1222 |
"count": base.CreateSchemaProxy(&base.Schema{Type: []string{"integer"}}), |
| 1223 |
}), |
| 1224 |
} |
| 1225 |
params := []*QueryParam{ |
| 1226 |
{Key: "key1", Values: []string{"name|123|count|42"}}, |
| 1227 |
} |
| 1228 |
result := ConstructParamMapFromPipeEncodingWithSchema(params, sch) |
| 1229 |
props := result["key1"].(map[string]interface{}) |
| 1230 |
require.Equal(t, "123", props["name"]) // string because schema says string |
| 1231 |
require.Equal(t, int64(42), props["count"]) // int because schema says integer |
| 1232 |
} |
| 1233 |
|
| 1234 |
func TestConstructParamMapFromSpaceEncodingWithSchema(t *testing.T) { |
| 1235 |
sch := &base.Schema{ |
| 1236 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1237 |
"name": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1238 |
}), |
| 1239 |
} |
| 1240 |
params := []*QueryParam{ |
| 1241 |
{Key: "key1", Values: []string{"name 456"}}, |
| 1242 |
} |
| 1243 |
result := ConstructParamMapFromSpaceEncodingWithSchema(params, sch) |
| 1244 |
props := result["key1"].(map[string]interface{}) |
| 1245 |
require.Equal(t, "456", props["name"]) // string because schema says string |
| 1246 |
} |
| 1247 |
|
| 1248 |
func TestConstructMapFromCSVWithSchema(t *testing.T) { |
| 1249 |
sch := &base.Schema{ |
| 1250 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1251 |
"id": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1252 |
"rank": base.CreateSchemaProxy(&base.Schema{Type: []string{"number"}}), |
| 1253 |
}), |
| 1254 |
} |
| 1255 |
result := ConstructMapFromCSVWithSchema("id,99,rank,3.5", sch) |
| 1256 |
require.Equal(t, "99", result["id"]) // string |
| 1257 |
require.Equal(t, 3.5, result["rank"]) // number |
| 1258 |
|
| 1259 |
// odd number of values |
| 1260 |
result = ConstructMapFromCSVWithSchema("id,99,rank", sch) |
| 1261 |
require.Equal(t, "99", result["id"]) |
| 1262 |
require.NotContains(t, result, "rank") |
| 1263 |
} |
| 1264 |
|
| 1265 |
func TestConstructKVFromCSVWithSchema(t *testing.T) { |
| 1266 |
sch := &base.Schema{ |
| 1267 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1268 |
"key1": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1269 |
"key2": base.CreateSchemaProxy(&base.Schema{Type: []string{"integer"}}), |
| 1270 |
}), |
| 1271 |
} |
| 1272 |
result := ConstructKVFromCSVWithSchema("key1=100,key2=200", sch) |
| 1273 |
require.Equal(t, "100", result["key1"]) // string |
| 1274 |
require.Equal(t, int64(200), result["key2"]) // integer |
| 1275 |
} |
| 1276 |
|
| 1277 |
func TestConstructKVFromLabelEncodingWithSchema(t *testing.T) { |
| 1278 |
sch := &base.Schema{ |
| 1279 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1280 |
"key1": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1281 |
"key2": base.CreateSchemaProxy(&base.Schema{Type: []string{"boolean"}}), |
| 1282 |
}), |
| 1283 |
} |
| 1284 |
result := ConstructKVFromLabelEncodingWithSchema("key1=true.key2=true", sch) |
| 1285 |
require.Equal(t, "true", result["key1"]) // string because schema says string |
| 1286 |
require.Equal(t, true, result["key2"]) // bool because schema says boolean |
| 1287 |
|
| 1288 |
// invalid pair (missing equals) is ignored |
| 1289 |
result = ConstructKVFromLabelEncodingWithSchema("key1=val.key2", sch) |
| 1290 |
require.Equal(t, "val", result["key1"]) |
| 1291 |
require.NotContains(t, result, "key2") |
| 1292 |
} |
| 1293 |
|
| 1294 |
func TestConstructKVFromMatrixCSVWithSchema(t *testing.T) { |
| 1295 |
sch := &base.Schema{ |
| 1296 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1297 |
"key1": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1298 |
}), |
| 1299 |
} |
| 1300 |
result := ConstructKVFromMatrixCSVWithSchema("key1=456;key2=789", sch) |
| 1301 |
require.Equal(t, "456", result["key1"]) // string |
| 1302 |
require.Equal(t, int64(789), result["key2"]) // no schema for key2, falls back to cast |
| 1303 |
|
| 1304 |
// invalid pair |
| 1305 |
result = ConstructKVFromMatrixCSVWithSchema("key1=val;key2", sch) |
| 1306 |
require.Equal(t, "val", result["key1"]) |
| 1307 |
require.NotContains(t, result, "key2") |
| 1308 |
} |
| 1309 |
|
| 1310 |
func TestConstructParamMapFromFormEncodingArrayWithSchema(t *testing.T) { |
| 1311 |
sch := &base.Schema{ |
| 1312 |
Properties: orderedmap.ToOrderedMap(map[string]*base.SchemaProxy{ |
| 1313 |
"key1": base.CreateSchemaProxy(&base.Schema{Type: []string{"string"}}), |
| 1314 |
"key2": base.CreateSchemaProxy(&base.Schema{Type: []string{"integer"}}), |
| 1315 |
}), |
| 1316 |
} |
| 1317 |
values := []*QueryParam{ |
| 1318 |
{Key: "param1", Values: []string{"key1,123,key2,456"}}, |
| 1319 |
} |
| 1320 |
decoded := ConstructParamMapFromFormEncodingArrayWithSchema(values, sch) |
| 1321 |
props := decoded["param1"].(map[string]interface{}) |
| 1322 |
require.Equal(t, "123", props["key1"]) // string |
| 1323 |
require.Equal(t, int64(456), props["key2"]) // integer |
| 1324 |
|
| 1325 |
// odd number of values — incomplete pair ignored |
| 1326 |
values = []*QueryParam{ |
| 1327 |
{Key: "param1", Values: []string{"key1,val,key2"}}, |
| 1328 |
} |
| 1329 |
decoded = ConstructParamMapFromFormEncodingArrayWithSchema(values, sch) |
| 1330 |
props = decoded["param1"].(map[string]interface{}) |
| 1331 |
require.Equal(t, "val", props["key1"]) |
| 1332 |
require.NotContains(t, props, "key2") |
| 1333 |
} |
| 1334 |
|
| 1335 |
func TestEffectiveSecurityForOperation(t *testing.T) { |
| 1336 |
globalSecurity := []*base.SecurityRequirement{ |
| 1337 |
{ |
| 1338 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 1339 |
"GlobalAuth": {}, |
| 1340 |
}), |
| 1341 |
}, |
| 1342 |
} |
| 1343 |
|
| 1344 |
opSecurity := []*base.SecurityRequirement{ |
| 1345 |
{ |
| 1346 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 1347 |
"OpAuth": {}, |
| 1348 |
}), |
| 1349 |
}, |
| 1350 |
} |
| 1351 |
|
| 1352 |
t.Run("operation-level security wins over global", func(t *testing.T) { |
| 1353 |
pathItem := &v3.PathItem{ |
| 1354 |
Get: &v3.Operation{Security: opSecurity}, |
| 1355 |
} |
| 1356 |
request, _ := http.NewRequest(http.MethodGet, "/", nil) |
| 1357 |
result := EffectiveSecurityForOperation(request, pathItem, globalSecurity) |
| 1358 |
require.Equal(t, opSecurity, result) |
| 1359 |
}) |
| 1360 |
|
| 1361 |
t.Run("nil operation security falls back to global", func(t *testing.T) { |
| 1362 |
pathItem := &v3.PathItem{ |
| 1363 |
Get: &v3.Operation{}, // Security is nil |
| 1364 |
} |
| 1365 |
request, _ := http.NewRequest(http.MethodGet, "/", nil) |
| 1366 |
result := EffectiveSecurityForOperation(request, pathItem, globalSecurity) |
| 1367 |
require.Equal(t, globalSecurity, result) |
| 1368 |
}) |
| 1369 |
|
| 1370 |
t.Run("empty operation security means no security (opt-out)", func(t *testing.T) { |
| 1371 |
pathItem := &v3.PathItem{ |
| 1372 |
Get: &v3.Operation{Security: []*base.SecurityRequirement{}}, |
| 1373 |
} |
| 1374 |
request, _ := http.NewRequest(http.MethodGet, "/", nil) |
| 1375 |
result := EffectiveSecurityForOperation(request, pathItem, globalSecurity) |
| 1376 |
require.NotNil(t, result) |
| 1377 |
require.Len(t, result, 0) |
| 1378 |
}) |
| 1379 |
|
| 1380 |
t.Run("both nil returns nil", func(t *testing.T) { |
| 1381 |
pathItem := &v3.PathItem{ |
| 1382 |
Get: &v3.Operation{}, |
| 1383 |
} |
| 1384 |
request, _ := http.NewRequest(http.MethodGet, "/", nil) |
| 1385 |
result := EffectiveSecurityForOperation(request, pathItem, nil) |
| 1386 |
require.Nil(t, result) |
| 1387 |
}) |
| 1388 |
|
| 1389 |
t.Run("nil operation falls back to global", func(t *testing.T) { |
| 1390 |
pathItem := &v3.PathItem{} // no Get operation |
| 1391 |
request, _ := http.NewRequest(http.MethodGet, "/", nil) |
| 1392 |
result := EffectiveSecurityForOperation(request, pathItem, globalSecurity) |
| 1393 |
require.Equal(t, globalSecurity, result) |
| 1394 |
}) |
| 1395 |
|
| 1396 |
t.Run("HEAD falls back to GET operation security", func(t *testing.T) { |
| 1397 |
pathItem := &v3.PathItem{ |
| 1398 |
Get: &v3.Operation{Security: opSecurity}, |
| 1399 |
} |
| 1400 |
request, _ := http.NewRequest(http.MethodHead, "/", nil) |
| 1401 |
result := EffectiveSecurityForOperation(request, pathItem, globalSecurity) |
| 1402 |
require.Equal(t, opSecurity, result) |
| 1403 |
}) |
| 1404 |
|
| 1405 |
t.Run("HEAD with explicit Head security uses Head", func(t *testing.T) { |
| 1406 |
headSecurity := []*base.SecurityRequirement{ |
| 1407 |
{ |
| 1408 |
Requirements: orderedmap.ToOrderedMap(map[string][]string{ |
| 1409 |
"HeadAuth": {}, |
| 1410 |
}), |
| 1411 |
}, |
| 1412 |
} |
| 1413 |
pathItem := &v3.PathItem{ |
| 1414 |
Get: &v3.Operation{Security: opSecurity}, |
| 1415 |
Head: &v3.Operation{Security: headSecurity}, |
| 1416 |
} |
| 1417 |
request, _ := http.NewRequest(http.MethodHead, "/", nil) |
| 1418 |
result := EffectiveSecurityForOperation(request, pathItem, globalSecurity) |
| 1419 |
require.Equal(t, headSecurity, result) |
| 1420 |
}) |
| 1421 |
} |