-
Notifications
You must be signed in to change notification settings - Fork 14
/
config_test.go
113 lines (98 loc) · 2.24 KB
/
config_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
package lua
import (
"os"
"testing"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)
func TestParse(t *testing.T) {
source := "lua/factorial.lua"
key := "1234"
in := config.ExtraConfig{
key: map[string]interface{}{
"sources": []interface{}{
source,
},
"md5": map[string]interface{}{
source: "49ae50f58e35f4821ad4550e1a4d1de0",
},
"pre": "pre",
"post": "post",
"skip_next": true,
},
}
cfg, err := Parse(logging.NoOp, in, key)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
b, _ := os.ReadFile(source)
if src, ok := cfg.Get(source); !ok || src != string(b) {
t.Errorf("wrong content %s", string(b))
}
if !cfg.SkipNext {
t.Errorf("the skip next flag is not enabled")
}
if cfg.PreCode != "pre" {
t.Errorf("wrong pre code %s", cfg.PreCode)
}
if cfg.PostCode != "post" {
t.Errorf("wrong post code %s", cfg.PostCode)
}
}
func TestParse_live(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test_parse_live")
if err != nil {
t.Error(err)
return
}
source := tmpfile.Name()
defer os.Remove(source) // clean up
initialContent := `print("hello, lua")`
finalContent := `print("bye, lua")`
if _, err := tmpfile.Write([]byte(initialContent)); err != nil {
t.Error(err)
return
}
tmpfile.Close()
key := "1234"
in := config.ExtraConfig{
key: map[string]interface{}{
"sources": []interface{}{
source,
},
"live": true,
},
}
cfg, err := Parse(logging.NoOp, in, key)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if src, ok := cfg.Get(source); !ok || src != initialContent {
t.Errorf("wrong content %s", src)
}
if err := os.WriteFile(source, []byte(finalContent), 0644); err != nil {
t.Error(err)
return
}
if src, ok := cfg.Get(source); !ok || src != finalContent {
t.Errorf("wrong content %s", src)
}
}
func TestParse_noExtra(t *testing.T) {
_, err := Parse(logging.NoOp, config.ExtraConfig{}, "1234")
if err != ErrNoExtraConfig {
t.Errorf("unexpected error: %v", err)
}
}
func TestParse_wrongExtra(t *testing.T) {
key := "1234"
in := config.ExtraConfig{
key: 42,
}
_, err := Parse(logging.NoOp, in, key)
if err != ErrWrongExtraConfig {
t.Errorf("unexpected error: %v", err)
}
}