Skip to content

Commit 90a7c66

Browse files
authored
test(cors): enhance CORS wildcard handling tests (#145)
- Import the `reflect` package in `cors_test.go` - Add new test cases for parsing wildcard rules in CORS configuration - Implement tests to check for panic on multiple wildcards and validate expected results for various wildcard scenarios ref: #106 Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent d5002f2 commit 90a7c66

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

cors_test.go

+91
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"net/http"
66
"net/http/httptest"
7+
"reflect"
78
"strings"
89
"testing"
910
"time"
@@ -462,3 +463,93 @@ func TestParseWildcardRules_InvalidWildcard(t *testing.T) {
462463
config.parseWildcardRules()
463464
})
464465
}
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

Comments
 (0)