-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon_test.go
126 lines (93 loc) · 2.58 KB
/
common_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// nolint
package frame_test
import (
"bytes"
"github.com/pitabwire/frame"
"net/http"
"os"
"slices"
"strings"
"testing"
)
func TestGetLocalIP(t *testing.T) {
localIp := frame.GetLocalIP()
if localIp == "" {
t.Error("Could not get a local ip even localhost")
} else if !strings.Contains(localIp, ".") && !strings.Contains(localIp, ":") {
t.Errorf(" The obtained ip %v is not valid ", localIp)
}
}
func TestGetMacAddress(t *testing.T) {
macAddress := frame.GetMacAddress()
if macAddress == "" {
t.Error("Could not get a mac address for this machine")
}
}
func TestGetEnv(t *testing.T) {
env := frame.GetEnv("RANDOM_MISSING_TEST_VALUE", "fallback")
if env != "fallback" {
t.Errorf("The environment variable value is expected to be blank but found %v", env)
}
err := os.Setenv("RANDOM_EXISTING_TEST_VALUE", "1")
if err != nil {
t.Error(err)
}
env = frame.GetEnv("RANDOM_EXISTING_TEST_VALUE", "")
if env != "1" {
t.Errorf("The environment variable value is expected to be 1 but found %v", env)
}
}
func TestGetIp(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "", bytes.NewReader([]byte("")))
ip := frame.GetIp(req)
if ip != "" {
t.Errorf("Somehow we found the ip %v yet not expected", ip)
}
req.RemoteAddr = "testamentor:80"
ip = frame.GetIp(req)
if ip != "testamentor" {
t.Errorf("Somehow we found the ip %v yet testamentor is expected", ip)
}
req.Header.Add("X-FORWARDED-FOR", "testamento")
ip = frame.GetIp(req)
if ip != "testamento" {
t.Errorf("Somehow we found the ip %v yet testamento is expected", ip)
}
}
type name struct {
frame.ConfigurationDefault
}
func Test_Config_Process(t *testing.T) {
os.Setenv("PORT", "testingp")
os.Setenv("DATABASE_URL", "testingu")
var conf name
err := frame.ConfigProcess("", &conf)
if err != nil {
t.Errorf(" could not load config from env : %v", err)
}
if conf.ServerPort != "testingp" {
t.Errorf("inherited PORT config not processed")
}
if !slices.Contains(conf.GetDatabasePrimaryHostURL(), "testingu") {
t.Errorf("inherited Database URL config not processed")
}
}
func Test_ConfigCastingIssues(t *testing.T) {
os.Setenv("PORT", "testingp")
err := os.Setenv("DATABASE_URL", "testingu")
if err != nil {
t.Errorf(" could not set config to env : %v", err)
return
}
var conf name
err = frame.ConfigProcess("", &conf)
if err != nil {
t.Errorf(" could not load config from env : %v", err)
return
}
_, srv := frame.NewService("Test Srv", frame.Config(&conf))
_, ok := srv.Config().(frame.ConfigurationOAUTH2)
if !ok {
t.Errorf("could not cast config to default settings")
}
}