|
1 | 1 | package getenv_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net/url" |
4 | 5 | "testing" |
5 | 6 | "time" |
6 | 7 |
|
7 | 8 | "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
8 | 10 |
|
9 | 11 | "github.com/obalunenko/getenv" |
10 | 12 | "github.com/obalunenko/getenv/option" |
@@ -2454,3 +2456,88 @@ func TestInt16SliceOrDefault(t *testing.T) { |
2454 | 2456 | }) |
2455 | 2457 | } |
2456 | 2458 | } |
| 2459 | + |
| 2460 | +func getURL(tb testing.TB, rawURL string) url.URL { |
| 2461 | + val, err := url.Parse(rawURL) |
| 2462 | + require.NoError(tb, err) |
| 2463 | + |
| 2464 | + return *val |
| 2465 | +} |
| 2466 | + |
| 2467 | +func TestURLOrDefault(t *testing.T) { |
| 2468 | + const rawDefault = "https://test:abcd123@golangbyexample.com:8000/tutorials/intro?type=advance&compact=false#history" |
| 2469 | + |
| 2470 | + type args struct { |
| 2471 | + key string |
| 2472 | + defaultVal url.URL |
| 2473 | + } |
| 2474 | + |
| 2475 | + type expected struct { |
| 2476 | + val url.URL |
| 2477 | + } |
| 2478 | + |
| 2479 | + var tests = []struct { |
| 2480 | + name string |
| 2481 | + precond precondition |
| 2482 | + args args |
| 2483 | + expected expected |
| 2484 | + }{ |
| 2485 | + { |
| 2486 | + name: "env not set - default returned", |
| 2487 | + precond: precondition{ |
| 2488 | + setenv: setenv{ |
| 2489 | + isSet: false, |
| 2490 | + val: "postgres://user:pass@host.com:5432/path?k=v#f", |
| 2491 | + }, |
| 2492 | + }, |
| 2493 | + args: args{ |
| 2494 | + key: testEnvKey, |
| 2495 | + defaultVal: getURL(t, rawDefault), |
| 2496 | + }, |
| 2497 | + expected: expected{ |
| 2498 | + val: getURL(t, rawDefault), |
| 2499 | + }, |
| 2500 | + }, |
| 2501 | + { |
| 2502 | + name: "env set - env value returned", |
| 2503 | + precond: precondition{ |
| 2504 | + setenv: setenv{ |
| 2505 | + isSet: true, |
| 2506 | + val: "postgres://user:pass@host.com:5432/path?k=v#f", |
| 2507 | + }, |
| 2508 | + }, |
| 2509 | + args: args{ |
| 2510 | + key: testEnvKey, |
| 2511 | + defaultVal: getURL(t, rawDefault), |
| 2512 | + }, |
| 2513 | + expected: expected{ |
| 2514 | + val: getURL(t, "postgres://user:pass@host.com:5432/path?k=v#f"), |
| 2515 | + }, |
| 2516 | + }, |
| 2517 | + { |
| 2518 | + name: "empty env value set - default returned", |
| 2519 | + precond: precondition{ |
| 2520 | + setenv: setenv{ |
| 2521 | + isSet: true, |
| 2522 | + val: "", |
| 2523 | + }, |
| 2524 | + }, |
| 2525 | + args: args{ |
| 2526 | + key: testEnvKey, |
| 2527 | + defaultVal: getURL(t, rawDefault), |
| 2528 | + }, |
| 2529 | + expected: expected{ |
| 2530 | + val: getURL(t, rawDefault), |
| 2531 | + }, |
| 2532 | + }, |
| 2533 | + } |
| 2534 | + |
| 2535 | + for _, tt := range tests { |
| 2536 | + t.Run(tt.name, func(t *testing.T) { |
| 2537 | + tt.precond.maybeSetEnv(t, tt.args.key) |
| 2538 | + |
| 2539 | + got := getenv.EnvOrDefault(tt.args.key, tt.args.defaultVal) |
| 2540 | + assert.Equal(t, tt.expected.val, got) |
| 2541 | + }) |
| 2542 | + } |
| 2543 | +} |
0 commit comments