github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/earl/url_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package earl 16 17 import ( 18 "net/url" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestParse(t *testing.T) { 25 tests := []struct { 26 urlStr string 27 expectedUrl url.URL 28 expectErr bool 29 }{ 30 { 31 "", 32 url.URL{ 33 Path: "/", 34 }, 35 false, 36 }, 37 { 38 "http://test.com", 39 url.URL{ 40 Scheme: "http", 41 Host: "test.com", 42 Path: "/", 43 }, 44 false, 45 }, 46 { 47 "test.com", 48 url.URL{ 49 Host: "test.com", 50 Path: "/", 51 }, 52 false, 53 }, 54 { 55 "http://127.0.0.1/", 56 url.URL{ 57 Scheme: "http", 58 Host: "127.0.0.1", 59 Path: "/", 60 }, 61 false, 62 }, 63 { 64 "127.0.0.1", 65 url.URL{ 66 Host: "127.0.0.1", 67 Path: "/", 68 }, 69 false, 70 }, 71 { 72 "ftp://test.com/path1/file.name", 73 url.URL{ 74 Scheme: "ftp", 75 Host: "test.com", 76 Path: "/path1/file.name", 77 }, 78 false, 79 }, 80 { 81 "test.com/path1/file.name", 82 url.URL{ 83 Host: "test.com", 84 Path: "/path1/file.name", 85 }, 86 false, 87 }, 88 { 89 "path1/path2", 90 url.URL{ 91 Path: "/path1/path2", 92 }, 93 false, 94 }, 95 { 96 "localhost/path1/path2", 97 url.URL{ 98 Host: "localhost", 99 Path: "/path1/path2", 100 }, 101 false, 102 }, 103 { 104 "http://localhost/path1/path2", 105 url.URL{ 106 Scheme: "http", 107 Host: "localhost", 108 Path: "/path1/path2", 109 }, 110 false, 111 }, 112 { 113 "http://localhost:50051/path1/path2", 114 url.URL{ 115 Scheme: "http", 116 Host: "localhost:50051", 117 Path: "/path1/path2", 118 }, 119 false, 120 }, 121 { 122 "localhost:50051/path1/path2", 123 url.URL{ 124 Host: "localhost:50051", 125 Path: "/path1/path2", 126 }, 127 false, 128 }, 129 { 130 "user:pass@place.org/path1/path2", 131 url.URL{ 132 Host: "place.org", 133 Path: "/path1/path2", 134 User: url.UserPassword("user", "pass"), 135 }, 136 false, 137 }, 138 { 139 "https://user:pass@place.org/path1/path2", 140 url.URL{ 141 Scheme: "https", 142 Host: "place.org", 143 Path: "/path1/path2", 144 User: url.UserPassword("user", "pass"), 145 }, 146 false, 147 }, 148 { 149 "user:pass@localhost/path1/path2", 150 url.URL{ 151 Host: "localhost", 152 Path: "/path1/path2", 153 User: url.UserPassword("user", "pass"), 154 }, 155 false, 156 }, 157 { 158 "https://user:pass@localhost/path1/path2", 159 url.URL{ 160 Scheme: "https", 161 Host: "localhost", 162 Path: "/path1/path2", 163 User: url.UserPassword("user", "pass"), 164 }, 165 false, 166 }, 167 { 168 "http://test.com:8080", 169 url.URL{ 170 Scheme: "http", 171 Host: "test.com:8080", 172 Path: "/", 173 }, 174 false, 175 }, 176 { 177 "test.com:8080/", 178 url.URL{ 179 Host: "test.com:8080", 180 Path: "/", 181 }, 182 false, 183 }, 184 { 185 "ftp://user:pass@test.com:8080/path/file.name", 186 url.URL{ 187 Scheme: "ftp", 188 Host: "test.com:8080", 189 Path: "/path/file.name", 190 User: url.UserPassword("user", "pass"), 191 }, 192 false, 193 }, 194 { 195 "user:pass@test.com:8080/path/file.name", 196 url.URL{ 197 Host: "test.com:8080", 198 Path: "/path/file.name", 199 User: url.UserPassword("user", "pass"), 200 }, 201 false, 202 }, 203 { 204 "file:///C:/Users/name/datasets", 205 url.URL{ 206 Scheme: "file", 207 Path: "C:/Users/name/datasets", 208 }, 209 false, 210 }, 211 { 212 `file:///C:\Users\name\datasets`, 213 url.URL{ 214 Scheme: "file", 215 Path: "C:/Users/name/datasets", 216 }, 217 false, 218 }, 219 { 220 "file://localhost/C$/Users/name/datasets", 221 url.URL{ 222 Scheme: "file", 223 Host: "localhost", 224 Path: "C:/Users/name/datasets", 225 }, 226 false, 227 }, 228 { 229 FileUrlFromPath(`C:\Users\name\datasets`, '\\'), 230 url.URL{ 231 Scheme: "file", 232 Path: "C:/Users/name/datasets", 233 }, 234 false, 235 }, 236 { 237 FileUrlFromPath(`./.dolt/noms`, '/'), 238 url.URL{ 239 Scheme: "file", 240 Path: "./.dolt/noms", 241 }, 242 false, 243 }, 244 { 245 FileUrlFromPath(`./.dolt\noms`, '\\'), 246 url.URL{ 247 Scheme: "file", 248 Path: "./.dolt/noms", 249 }, 250 false, 251 }, 252 { 253 FileUrlFromPath(`.dolt/noms`, '/'), 254 url.URL{ 255 Scheme: "file", 256 Path: ".dolt/noms", 257 }, 258 false, 259 }, 260 { 261 FileUrlFromPath(`.dolt\noms`, '\\'), 262 url.URL{ 263 Scheme: "file", 264 Path: ".dolt/noms", 265 }, 266 false, 267 }, 268 { 269 FileUrlFromPath(`./test/.dolt/noms`, '/'), 270 url.URL{ 271 Scheme: "file", 272 Path: "./test/.dolt/noms", 273 }, 274 false, 275 }, 276 { 277 FileUrlFromPath(`./test\.dolt\noms`, '\\'), 278 url.URL{ 279 Scheme: "file", 280 Path: "./test/.dolt/noms", 281 }, 282 false, 283 }, 284 } 285 286 for _, test := range tests { 287 actualUrl, err := Parse(test.urlStr) 288 289 if (err != nil) != test.expectErr { 290 t.Error("input:", test.urlStr, "got error:", err != nil, "expected error:", test.expectErr, "result:", actualUrl, "err:", err) 291 } else if err == nil { 292 assert.Equal(t, &test.expectedUrl, actualUrl) 293 } 294 } 295 }