|
4 | 4 | "errors" |
5 | 5 | "math/big" |
6 | 6 | "net" |
| 7 | + "net/url" |
7 | 8 | "reflect" |
8 | 9 | "testing" |
9 | 10 | "time" |
@@ -361,6 +362,45 @@ func TestStringToIPNetHookFunc(t *testing.T) { |
361 | 362 | } |
362 | 363 | } |
363 | 364 |
|
| 365 | +func TestStringToURLHookFunc(t *testing.T) { |
| 366 | + f := StringToURLHookFunc() |
| 367 | + |
| 368 | + urlValue := reflect.ValueOf(&url.URL{}) |
| 369 | + strValue := reflect.ValueOf("") |
| 370 | + |
| 371 | + cases := []struct { |
| 372 | + f, t reflect.Value |
| 373 | + result interface{} |
| 374 | + err bool |
| 375 | + }{ |
| 376 | + {reflect.ValueOf("https://u:p@example.com?p=1"), urlValue, |
| 377 | + &url.URL{ |
| 378 | + Scheme: "https", |
| 379 | + Host: "example.com", |
| 380 | + User: url.UserPassword("u", "p"), |
| 381 | + RawQuery: "p=1", |
| 382 | + }, false}, |
| 383 | + {reflect.ValueOf("https://example.com"), strValue, "https://example.com", false}, |
| 384 | + {strValue, urlValue, &url.URL{}, false}, |
| 385 | + {reflect.ValueOf("example"), urlValue, |
| 386 | + &url.URL{ |
| 387 | + Path: "example", |
| 388 | + }, false}, |
| 389 | + } |
| 390 | + |
| 391 | + for i, tc := range cases { |
| 392 | + actual, err := DecodeHookExec(f, tc.f, tc.t) |
| 393 | + if tc.err != (err != nil) { |
| 394 | + t.Fatalf("case %d: expected err %#v", i, tc.err) |
| 395 | + } |
| 396 | + if !reflect.DeepEqual(actual, tc.result) { |
| 397 | + t.Fatalf( |
| 398 | + "case %d: expected %#v, got %#v", |
| 399 | + i, tc.result, actual) |
| 400 | + } |
| 401 | + } |
| 402 | +} |
| 403 | + |
364 | 404 | func TestWeaklyTypedHook(t *testing.T) { |
365 | 405 | var f DecodeHookFunc = WeaklyTypedHook |
366 | 406 |
|
|
0 commit comments