|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestValidateSubdomain(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + subdomain string |
| 11 | + wantErr bool |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "valid subdomain", |
| 15 | + subdomain: "test", |
| 16 | + wantErr: false, |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "subdomain with dash", |
| 20 | + subdomain: "test-subdomain", |
| 21 | + wantErr: false, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "subdomain with underscore", |
| 25 | + subdomain: "test_subdomain", |
| 26 | + wantErr: false, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "subdomain with uppercase letters", |
| 30 | + subdomain: "TestSubdomain", |
| 31 | + wantErr: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "subdomain with leading dash", |
| 35 | + subdomain: "-test", |
| 36 | + wantErr: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "subdomain with trailing dash", |
| 40 | + subdomain: "test-", |
| 41 | + wantErr: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "subdomain with leading underscore", |
| 45 | + subdomain: "_test", |
| 46 | + wantErr: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "subdomain with trailing underscore", |
| 50 | + subdomain: "test_", |
| 51 | + wantErr: true, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "subdomain with special characters", |
| 55 | + subdomain: "test@subdomain", |
| 56 | + wantErr: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "subdomain with dot", |
| 60 | + subdomain: "test.subdomain", |
| 61 | + wantErr: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "subdomain with multiple dots", |
| 65 | + subdomain: "test.subdomain.com", |
| 66 | + wantErr: true, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, test := range tests { |
| 71 | + t.Run(test.name, func(t *testing.T) { |
| 72 | + err := ValidateSubdomain(test.subdomain) |
| 73 | + if (err != nil) != test.wantErr { |
| 74 | + t.Errorf("ValidateSubdomain(%q) = %v, wantErr %v", test.subdomain, err, test.wantErr) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments