Skip to content

Commit 7f7fd13

Browse files
config: Allow configuring from another ModuleConfig instance.
1 parent 52c1a57 commit 7f7fd13

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,10 @@ func ServerIdentifier(id string) ModuleConfigOption {
334334
return nil
335335
}
336336
}
337+
338+
func FromModuleConfig(mcfg *ModuleConfig) ModuleConfigOption {
339+
return func(c *ModuleConfig) error {
340+
*c = *mcfg
341+
return nil
342+
}
343+
}

config_test.go

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"time"
77
)
88

9-
func TestModuleConfig(t *testing.T) {
10-
// Everything default
9+
func TestDefaultModuleConfig(t *testing.T) {
1110
c, err := NewModuleConfig()
1211
if err != nil {
1312
t.Fatalf("Failed to create module config: %s", err)
@@ -75,9 +74,92 @@ func TestModuleConfig(t *testing.T) {
7574
if c.IsBlockCode(200) {
7675
t.Errorf("Unexpected IsBlockCode(200): %v", c.IsBlockCode(200))
7776
}
77+
}
78+
79+
func TestConfiguredModuleConfig(t *testing.T) {
80+
c, err := NewModuleConfig(
81+
AllowUnknownContentLength(true),
82+
AltResponseCodes(403),
83+
AnomalyDuration(10*time.Second),
84+
AnomalySize(8192),
85+
CustomInspector(&RPCInspector{}, func(_ *http.Request) bool { return true }, func(_ *http.Request) {}),
86+
CustomHeaderExtractor(func(_ *http.Request) (http.Header, error) { return nil, nil }),
87+
Debug(true),
88+
MaxContentLength(500000),
89+
Socket("tcp", "0.0.0.0:1234"),
90+
Timeout(10*time.Millisecond),
91+
)
92+
if err != nil {
93+
t.Fatalf("Failed to create module config: %s", err)
94+
}
95+
if c.AllowUnknownContentLength() != true {
96+
t.Errorf("Unexpected AllowUnknownContentLength: %v", c.AllowUnknownContentLength())
97+
}
98+
altResponseCodes := c.AltResponseCodes()
99+
if len(altResponseCodes) != 1 || altResponseCodes[0] != 403 {
100+
t.Errorf("Unexpected AltResponseCodes: %v", altResponseCodes)
101+
}
102+
if c.AnomalyDuration() != 10*time.Second {
103+
t.Errorf("Unexpected AnomalyDuration: %v", c.AnomalyDuration())
104+
}
105+
if c.AnomalySize() != 8192 {
106+
t.Errorf("Unexpected AnomalySize: %v", c.AnomalySize())
107+
}
108+
if c.Debug() != true {
109+
t.Errorf("Unexpected Debug: %v", c.Debug())
110+
}
111+
if c.HeaderExtractor() == nil {
112+
t.Errorf("Unexpected HeaderExtractor: %p", c.HeaderExtractor())
113+
}
114+
if c.Inspector() == DefaultInspector {
115+
t.Errorf("Unexpected Inspector: %v", c.Inspector())
116+
}
117+
if c.InspectorInit() == nil {
118+
t.Errorf("Unexpected InspectorInit: %p", c.InspectorInit())
119+
}
120+
if c.InspectorFini() == nil {
121+
t.Errorf("Unexpected InspectorFini: %p", c.InspectorFini())
122+
}
123+
if c.MaxContentLength() != 500000 {
124+
t.Errorf("Unexpected MaxContentLength: %v", c.MaxContentLength())
125+
}
126+
if c.ModuleIdentifier() != DefaultModuleIdentifier {
127+
t.Errorf("Unexpected ModuleIdentifier: %v", c.ModuleIdentifier())
128+
}
129+
if c.RPCAddress() != "0.0.0.0:1234" {
130+
t.Errorf("Unexpected RPCAddress: %v", c.RPCAddress())
131+
}
132+
if c.RPCNetwork() != "tcp" {
133+
t.Errorf("Unexpected RPCNetwork: %v", c.RPCNetwork())
134+
}
135+
if c.RPCAddressString() != "tcp:0.0.0.0:1234" {
136+
t.Errorf("Unexpected RPCAddressString: %v", c.RPCAddressString())
137+
}
138+
if c.ServerIdentifier() != DefaultServerIdentifier {
139+
t.Errorf("Unexpected ServerIdentifier: %v", c.ServerIdentifier())
140+
}
141+
if c.Timeout() != 10*time.Millisecond {
142+
t.Errorf("Unexpected Timeout: %v", c.Timeout())
143+
}
144+
if c.IsAllowCode(406) {
145+
t.Errorf("Unexpected IsAllowCode(406): %v", c.IsAllowCode(406))
146+
}
147+
if !c.IsAllowCode(200) {
148+
t.Errorf("Unexpected IsAllowCode(200): %v", c.IsAllowCode(200))
149+
}
150+
if !c.IsBlockCode(406) {
151+
t.Errorf("Unexpected IsBlockCode(406): %v", c.IsBlockCode(406))
152+
}
153+
if !c.IsBlockCode(403) {
154+
t.Errorf("Unexpected IsBlockCode(403): %v", c.IsBlockCode(403))
155+
}
156+
if c.IsBlockCode(200) {
157+
t.Errorf("Unexpected IsBlockCode(200): %v", c.IsBlockCode(200))
158+
}
159+
}
78160

79-
// Everything configured non-default
80-
c, err = NewModuleConfig(
161+
func TestFromModuleConfig(t *testing.T) {
162+
c0, err := NewModuleConfig(
81163
AllowUnknownContentLength(true),
82164
AltResponseCodes(403),
83165
AnomalyDuration(10*time.Second),
@@ -92,6 +174,13 @@ func TestModuleConfig(t *testing.T) {
92174
if err != nil {
93175
t.Fatalf("Failed to create module config: %s", err)
94176
}
177+
178+
c, err := NewModuleConfig(
179+
FromModuleConfig(c0),
180+
)
181+
if err != nil {
182+
t.Fatalf("Failed to create module config: %s", err)
183+
}
95184
if c.AllowUnknownContentLength() != true {
96185
t.Errorf("Unexpected AllowUnknownContentLength: %v", c.AllowUnknownContentLength())
97186
}

0 commit comments

Comments
 (0)