|
| 1 | +package helpers |
| 2 | + |
| 3 | +import ( |
| 4 | +"testing" |
| 5 | + |
| 6 | +"github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +funcTestValidateURL(t*testing.T) { |
| 10 | +tests:= []struct { |
| 11 | +namestring |
| 12 | +valueany |
| 13 | +labelstring |
| 14 | +expectErrorbool |
| 15 | +errorContainsstring |
| 16 | +}{ |
| 17 | +// Valid cases |
| 18 | +{ |
| 19 | +name:"empty string", |
| 20 | +value:"", |
| 21 | +label:"url", |
| 22 | +expectError:false, |
| 23 | +}, |
| 24 | +{ |
| 25 | +name:"valid http URL", |
| 26 | +value:"http://example.com", |
| 27 | +label:"url", |
| 28 | +expectError:false, |
| 29 | +}, |
| 30 | +{ |
| 31 | +name:"valid https URL", |
| 32 | +value:"https://example.com/path", |
| 33 | +label:"url", |
| 34 | +expectError:false, |
| 35 | +}, |
| 36 | +{ |
| 37 | +name:"absolute file path", |
| 38 | +value:"/path/to/file", |
| 39 | +label:"url", |
| 40 | +expectError:false, |
| 41 | +}, |
| 42 | +{ |
| 43 | +name:"relative file path", |
| 44 | +value:"./file.txt", |
| 45 | +label:"url", |
| 46 | +expectError:false, |
| 47 | +}, |
| 48 | +{ |
| 49 | +name:"relative path up directory", |
| 50 | +value:"../config.json", |
| 51 | +label:"url", |
| 52 | +expectError:false, |
| 53 | +}, |
| 54 | +{ |
| 55 | +name:"simple filename", |
| 56 | +value:"file.txt", |
| 57 | +label:"url", |
| 58 | +expectError:false, |
| 59 | +}, |
| 60 | +{ |
| 61 | +name:"URL with query params", |
| 62 | +value:"https://example.com/search?q=test", |
| 63 | +label:"url", |
| 64 | +expectError:false, |
| 65 | +}, |
| 66 | +{ |
| 67 | +name:"URL with fragment", |
| 68 | +value:"https://example.com/page#section", |
| 69 | +label:"url", |
| 70 | +expectError:false, |
| 71 | +}, |
| 72 | + |
| 73 | +// Various URL schemes that url.Parse accepts |
| 74 | +{ |
| 75 | +name:"file URL scheme", |
| 76 | +value:"file:///path/to/file", |
| 77 | +label:"url", |
| 78 | +expectError:false, |
| 79 | +}, |
| 80 | +{ |
| 81 | +name:"ftp scheme", |
| 82 | +value:"ftp://files.example.com/file.txt", |
| 83 | +label:"url", |
| 84 | +expectError:false, |
| 85 | +}, |
| 86 | +{ |
| 87 | +name:"mailto scheme", |
| 88 | +value:"mailto:user@example.com", |
| 89 | +label:"url", |
| 90 | +expectError:false, |
| 91 | +}, |
| 92 | +{ |
| 93 | +name:"tel scheme", |
| 94 | +value:"tel:+1234567890", |
| 95 | +label:"url", |
| 96 | +expectError:false, |
| 97 | +}, |
| 98 | +{ |
| 99 | +name:"data scheme", |
| 100 | +value:"data:text/plain;base64,SGVsbG8=", |
| 101 | +label:"url", |
| 102 | +expectError:false, |
| 103 | +}, |
| 104 | + |
| 105 | +// Invalid cases |
| 106 | +{ |
| 107 | +name:"non-string type - int", |
| 108 | +value:123, |
| 109 | +label:"url", |
| 110 | +expectError:true, |
| 111 | +errorContains:"expected\"url\" to be a string", |
| 112 | +}, |
| 113 | +{ |
| 114 | +name:"non-string type - nil", |
| 115 | +value:nil, |
| 116 | +label:"config_url", |
| 117 | +expectError:true, |
| 118 | +errorContains:"expected\"config_url\" to be a string", |
| 119 | +}, |
| 120 | +{ |
| 121 | +name:"invalid URL with spaces", |
| 122 | +value:"http://example .com", |
| 123 | +label:"url", |
| 124 | +expectError:true, |
| 125 | +errorContains:"invalid character", |
| 126 | +}, |
| 127 | +{ |
| 128 | +name:"malformed URL", |
| 129 | +value:"http://[::1:80", |
| 130 | +label:"endpoint", |
| 131 | +expectError:true, |
| 132 | +errorContains:"missing ']'", |
| 133 | +}, |
| 134 | +} |
| 135 | + |
| 136 | +for_,tt:=rangetests { |
| 137 | +t.Run(tt.name,func(t*testing.T) { |
| 138 | +warnings,errors:=ValidateURL(tt.value,tt.label) |
| 139 | + |
| 140 | +iftt.expectError { |
| 141 | +require.Len(t,errors,1,"expected an error but got none") |
| 142 | +require.Contains(t,errors[0].Error(),tt.errorContains) |
| 143 | +}else { |
| 144 | +require.Empty(t,errors,"expected no errors but got: %v",errors) |
| 145 | +} |
| 146 | + |
| 147 | +// Should always return nil for warnings |
| 148 | +require.Nil(t,warnings,"expected warnings to be nil but got: %v",warnings) |
| 149 | +}) |
| 150 | +} |
| 151 | +} |