Debian Codegraph @

example/server/config/config_test.go

raw
1 package config
2
3 import (
4 "fmt"
5 "os"
6 "testing"
7 )
8
9 func TestFromEnvVars(t *testing.T) {
10
11 for _, tc := range []struct {
12 name string
13 env map[string]string
14 defaults *Config
15 want *Config
16 }{
17 {
18 name: "no vars, no default values",
19 env: map[string]string{},
20 want: &Config{},
21 },
22 {
23 name: "no vars, only defaults",
24 env: map[string]string{},
25 defaults: &Config{
26 Port: "6666",
27 UsersFile: "/default/user/path",
28 RedirectURI: []string{"re", "direct", "uris"},
29 },
30 want: &Config{
31 Port: "6666",
32 UsersFile: "/default/user/path",
33 RedirectURI: []string{"re", "direct", "uris"},
34 },
35 },
36 {
37 name: "overriding default values",
38 env: map[string]string{
39 "PORT": "1234",
40 "USERS_FILE": "/path/to/users",
41 "REDIRECT_URI": "http://redirect/redirect",
42 },
43 defaults: &Config{
44 Port: "6666",
45 UsersFile: "/default/user/path",
46 RedirectURI: []string{"re", "direct", "uris"},
47 },
48 want: &Config{
49 Port: "1234",
50 UsersFile: "/path/to/users",
51 RedirectURI: []string{"http://redirect/redirect"},
52 },
53 },
54 {
55 name: "multiple redirect uris",
56 env: map[string]string{
57 "REDIRECT_URI": "http://host_1,http://host_2,http://host_3",
58 },
59 want: &Config{
60 RedirectURI: []string{
61 "http://host_1", "http://host_2", "http://host_3",
62 },
63 },
64 },
65 } {
66 t.Run(tc.name, func(t *testing.T) {
67 os.Clearenv()
68 for k, v := range tc.env {
69 os.Setenv(k, v)
70 }
71 cfg := FromEnvVars(tc.defaults)
72 if fmt.Sprint(cfg) != fmt.Sprint(tc.want) {
73 t.Errorf("Expected FromEnvVars()=%q, but got %q", tc.want, cfg)
74 }
75 })
76 }
77 }