|
4 | 4 | "context"
|
5 | 5 | "net/http"
|
6 | 6 | "net/http/httptest"
|
| 7 | + "reflect" |
7 | 8 | "strings"
|
8 | 9 | "testing"
|
9 | 10 | "time"
|
@@ -462,3 +463,93 @@ func TestParseWildcardRules_InvalidWildcard(t *testing.T) {
|
462 | 463 | config.parseWildcardRules()
|
463 | 464 | })
|
464 | 465 | }
|
| 466 | + |
| 467 | +func TestParseWildcardRules(t *testing.T) { |
| 468 | + tests := []struct { |
| 469 | + name string |
| 470 | + config Config |
| 471 | + expectedResult [][]string |
| 472 | + expectPanic bool |
| 473 | + }{ |
| 474 | + { |
| 475 | + name: "Wildcard not allowed", |
| 476 | + config: Config{ |
| 477 | + AllowWildcard: false, |
| 478 | + AllowOrigins: []string{"http://example.com", "https://*.domain.com"}, |
| 479 | + }, |
| 480 | + expectedResult: nil, |
| 481 | + expectPanic: false, |
| 482 | + }, |
| 483 | + { |
| 484 | + name: "No wildcards", |
| 485 | + config: Config{ |
| 486 | + AllowWildcard: true, |
| 487 | + AllowOrigins: []string{"http://example.com", "https://example.com"}, |
| 488 | + }, |
| 489 | + expectedResult: nil, |
| 490 | + expectPanic: false, |
| 491 | + }, |
| 492 | + { |
| 493 | + name: "Single wildcard at the end", |
| 494 | + config: Config{ |
| 495 | + AllowWildcard: true, |
| 496 | + AllowOrigins: []string{"http://*.example.com"}, |
| 497 | + }, |
| 498 | + expectedResult: [][]string{{"http://", ".example.com"}}, |
| 499 | + expectPanic: false, |
| 500 | + }, |
| 501 | + { |
| 502 | + name: "Single wildcard at the beginning", |
| 503 | + config: Config{ |
| 504 | + AllowWildcard: true, |
| 505 | + AllowOrigins: []string{"*.example.com"}, |
| 506 | + }, |
| 507 | + expectedResult: [][]string{{"*", ".example.com"}}, |
| 508 | + expectPanic: false, |
| 509 | + }, |
| 510 | + { |
| 511 | + name: "Single wildcard in the middle", |
| 512 | + config: Config{ |
| 513 | + AllowWildcard: true, |
| 514 | + AllowOrigins: []string{"http://example.*.com"}, |
| 515 | + }, |
| 516 | + expectedResult: [][]string{{"http://example.", ".com"}}, |
| 517 | + expectPanic: false, |
| 518 | + }, |
| 519 | + { |
| 520 | + name: "Multiple wildcards should panic", |
| 521 | + config: Config{ |
| 522 | + AllowWildcard: true, |
| 523 | + AllowOrigins: []string{"http://*.*.com"}, |
| 524 | + }, |
| 525 | + expectedResult: nil, |
| 526 | + expectPanic: true, |
| 527 | + }, |
| 528 | + { |
| 529 | + name: "Single wildcard in the end", |
| 530 | + config: Config{ |
| 531 | + AllowWildcard: true, |
| 532 | + AllowOrigins: []string{"http://example.com/*"}, |
| 533 | + }, |
| 534 | + expectedResult: [][]string{{"http://example.com/", "*"}}, |
| 535 | + expectPanic: false, |
| 536 | + }, |
| 537 | + } |
| 538 | + |
| 539 | + for _, tt := range tests { |
| 540 | + t.Run(tt.name, func(t *testing.T) { |
| 541 | + if tt.expectPanic { |
| 542 | + defer func() { |
| 543 | + if r := recover(); r == nil { |
| 544 | + t.Errorf("The code did not panic") |
| 545 | + } |
| 546 | + }() |
| 547 | + } |
| 548 | + |
| 549 | + result := tt.config.parseWildcardRules() |
| 550 | + if !tt.expectPanic && !reflect.DeepEqual(result, tt.expectedResult) { |
| 551 | + t.Errorf("Name: %v, Expected %v, got %v", tt.name, tt.expectedResult, result) |
| 552 | + } |
| 553 | + }) |
| 554 | + } |
| 555 | +} |
0 commit comments