|
| 1 | +package configs |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestSanitizeURL(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name, input, want, errMsg string |
| 11 | + wantErr bool |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "Valid URL with http", |
| 15 | + input: "http://example.com/path?query=123", |
| 16 | + want: "http://example.com", |
| 17 | + wantErr: false, |
| 18 | + }, |
| 19 | + { |
| 20 | + name: "Valid URL with https", |
| 21 | + input: "https://example.com", |
| 22 | + want: "https://example.com", |
| 23 | + wantErr: false, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "URL with no scheme", |
| 27 | + input: "example.com", |
| 28 | + want: "", |
| 29 | + wantErr: true, |
| 30 | + errMsg: "scheme was empty", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "URL with no scheme", |
| 34 | + input: "http://example.com/", |
| 35 | + want: "http://example.com", |
| 36 | + wantErr: false, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "URL with no host", |
| 40 | + input: "http://", |
| 41 | + want: "", |
| 42 | + wantErr: true, |
| 43 | + errMsg: "host was empty", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Invalid URL format", |
| 47 | + input: "://example.com", |
| 48 | + want: "", |
| 49 | + wantErr: true, |
| 50 | + errMsg: "missing protocol scheme", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "URL with user info", |
| 54 | + input: "https://user:pass@example.com", |
| 55 | + want: "https://example.com", |
| 56 | + wantErr: false, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Empty URL", |
| 60 | + input: "", |
| 61 | + want: "", |
| 62 | + wantErr: true, |
| 63 | + errMsg: "scheme was empty", |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + got, err := sanitizeURL(tt.input) |
| 70 | + if (err != nil) != tt.wantErr { |
| 71 | + t.Errorf("sanitizeURL() error = %v, wantErr %v", err, tt.wantErr) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + if tt.wantErr && err != nil { |
| 76 | + if !strings.Contains(err.Error(), tt.errMsg) { |
| 77 | + t.Errorf("actual error = '%v', wantErrMsg: '%v'", err, tt.errMsg) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if got != tt.want { |
| 82 | + t.Errorf("sanitizeURL() URL got = %v, want %v", got, tt.want) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
0 commit comments