6
6
"time"
7
7
)
8
8
9
- func TestModuleConfig (t * testing.T ) {
10
- // Everything default
9
+ func TestDefaultModuleConfig (t * testing.T ) {
11
10
c , err := NewModuleConfig ()
12
11
if err != nil {
13
12
t .Fatalf ("Failed to create module config: %s" , err )
@@ -75,9 +74,92 @@ func TestModuleConfig(t *testing.T) {
75
74
if c .IsBlockCode (200 ) {
76
75
t .Errorf ("Unexpected IsBlockCode(200): %v" , c .IsBlockCode (200 ))
77
76
}
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
+ }
78
160
79
- // Everything configured non-default
80
- c , err = NewModuleConfig (
161
+ func TestFromModuleConfig ( t * testing. T ) {
162
+ c0 , err : = NewModuleConfig (
81
163
AllowUnknownContentLength (true ),
82
164
AltResponseCodes (403 ),
83
165
AnomalyDuration (10 * time .Second ),
@@ -92,6 +174,13 @@ func TestModuleConfig(t *testing.T) {
92
174
if err != nil {
93
175
t .Fatalf ("Failed to create module config: %s" , err )
94
176
}
177
+
178
+ c , err := NewModuleConfig (
179
+ FromModuleConfig (c0 ),
180
+ )
181
+ if err != nil {
182
+ t .Fatalf ("Failed to create module config: %s" , err )
183
+ }
95
184
if c .AllowUnknownContentLength () != true {
96
185
t .Errorf ("Unexpected AllowUnknownContentLength: %v" , c .AllowUnknownContentLength ())
97
186
}
0 commit comments