Skip to content

Commit c0e10cb

Browse files
Merge pull request #17 from signalsciences/deprecate-altresponsecodes-concept
config: Deprecate AltResponseCode configuration in favor of using all responses >=300 as blocking codes.
2 parents 4a8bace + 63cbb0a commit c0e10cb

File tree

4 files changed

+57
-46
lines changed

4 files changed

+57
-46
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# GoLang Module Release Notes
22

3+
## Unreleased
4+
5+
* Deprecated the `AltResponseCodes` concept in favor of using all codes 300-599 as "blocking"
6+
37
## 1.7.1 2020-04-06
48

59
* Updated the response recorder to implement the io.ReaderFrom interface

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.7.1
1+
1.8.0

config.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ type HeaderExtractorFunc func(*http.Request) (http.Header, error)
4141
// ModuleConfig is a configuration object for a Module
4242
type ModuleConfig struct {
4343
allowUnknownContentLength bool
44-
altResponseCodes map[int]bool
4544
anomalyDuration time.Duration
4645
anomalySize int64
4746
debug bool
@@ -61,7 +60,6 @@ type ModuleConfig struct {
6160
func NewModuleConfig(options ...ModuleConfigOption) (*ModuleConfig, error) {
6261
c := &ModuleConfig{
6362
allowUnknownContentLength: DefaultAllowUnknownContentLength,
64-
altResponseCodes: nil,
6563
anomalyDuration: DefaultAnomalyDuration,
6664
anomalySize: DefaultAnomalySize,
6765
debug: DefaultDebug,
@@ -85,6 +83,9 @@ func NewModuleConfig(options ...ModuleConfigOption) (*ModuleConfig, error) {
8583
// SetOptions sets options specified as functional arguments
8684
func (c *ModuleConfig) SetOptions(options ...ModuleConfigOption) error {
8785
for _, opt := range options {
86+
if opt == nil {
87+
continue
88+
}
8889
err := opt(c)
8990
if err != nil {
9091
return err
@@ -95,10 +96,7 @@ func (c *ModuleConfig) SetOptions(options ...ModuleConfigOption) error {
9596

9697
// IsBlockCode returns true if the code is a configured block code
9798
func (c *ModuleConfig) IsBlockCode(code int) bool {
98-
if code == 406 {
99-
return true
100-
}
101-
return c.altResponseCodes[code]
99+
return code >= 300 && code <= 599
102100
}
103101

104102
// IsAllowCode returns true if the code is an allow code
@@ -112,15 +110,14 @@ func (c *ModuleConfig) AllowUnknownContentLength() bool {
112110
}
113111

114112
// AltResponseCodes returns the configuration value
113+
//
114+
// Deprecated: The `AltResponseCodes` concept has
115+
// been deprecated in favor of treating all response
116+
// codes 300-599 as blocking codes. Due to
117+
// this, this method will always return nil. It is left
118+
// here to avoid breakage, but will eventually be removed.
115119
func (c *ModuleConfig) AltResponseCodes() []int {
116-
if len(c.altResponseCodes) == 0 {
117-
return nil
118-
}
119-
codes := make([]int, 0, len(c.altResponseCodes))
120-
for k := range c.altResponseCodes {
121-
codes = append(codes, k)
122-
}
123-
return codes
120+
return nil
124121
}
125122

126123
// AnomalyDuration returns the configuration value
@@ -216,14 +213,14 @@ func AllowUnknownContentLength(allow bool) ModuleConfigOption {
216213

217214
// AltResponseCodes is a function argument to set the alternative
218215
// response codes from the agent that are considered "blocking"
216+
//
217+
// Deprecated: The `AltResponseCodes` concept has
218+
// been deprecated in favor of treating all response
219+
// codes 300-599 as blocking codes. Due to
220+
// this, this method will always return nil. It is left
221+
// here to avoid breakage, but will eventually be removed.
219222
func AltResponseCodes(codes ...int) ModuleConfigOption {
220-
return func(c *ModuleConfig) error {
221-
c.altResponseCodes = make(map[int]bool)
222-
for _, code := range codes {
223-
c.altResponseCodes[code] = true
224-
}
225-
return nil
226-
}
223+
return nil
227224
}
228225

229226
// AnomalyDuration is a function argument to indicate when to send data

config_test.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,21 @@ func TestDefaultModuleConfig(t *testing.T) {
5959
if c.Timeout() != DefaultTimeout {
6060
t.Errorf("Unexpected Timeout: %v", c.Timeout())
6161
}
62-
if c.IsAllowCode(406) {
63-
t.Errorf("Unexpected IsAllowCode(406): %v", c.IsAllowCode(406))
62+
for code := 300; code < 600; code++ {
63+
if c.IsAllowCode(code) {
64+
t.Errorf("Unexpected IsAllowCode(%d): %v", code, c.IsAllowCode(code))
65+
}
6466
}
6567
if !c.IsAllowCode(200) {
6668
t.Errorf("Unexpected IsAllowCode(200): %v", c.IsAllowCode(200))
6769
}
68-
if !c.IsBlockCode(406) {
69-
t.Errorf("Unexpected IsBlockCode(406): %v", c.IsBlockCode(406))
70+
for code := 300; code < 600; code++ {
71+
if !c.IsBlockCode(code) {
72+
t.Errorf("Unexpected IsBlockCode(%d): %v", code, c.IsBlockCode(code))
73+
}
7074
}
71-
if c.IsBlockCode(403) {
72-
t.Errorf("Unexpected IsBlockCode(403): %v", c.IsBlockCode(403))
75+
if c.IsBlockCode(600) {
76+
t.Errorf("Unexpected IsBlockCode(600): %v", c.IsBlockCode(600))
7377
}
7478
if c.IsBlockCode(200) {
7579
t.Errorf("Unexpected IsBlockCode(200): %v", c.IsBlockCode(200))
@@ -95,9 +99,8 @@ func TestConfiguredModuleConfig(t *testing.T) {
9599
if c.AllowUnknownContentLength() != true {
96100
t.Errorf("Unexpected AllowUnknownContentLength: %v", c.AllowUnknownContentLength())
97101
}
98-
altResponseCodes := c.AltResponseCodes()
99-
if len(altResponseCodes) != 1 || altResponseCodes[0] != 403 {
100-
t.Errorf("Unexpected AltResponseCodes: %v", altResponseCodes)
102+
if c.AltResponseCodes() != nil {
103+
t.Errorf("Unexpected AltResponseCodes from deprecated option (should be nil): %v", c.AltResponseCodes())
101104
}
102105
if c.AnomalyDuration() != 10*time.Second {
103106
t.Errorf("Unexpected AnomalyDuration: %v", c.AnomalyDuration())
@@ -141,17 +144,21 @@ func TestConfiguredModuleConfig(t *testing.T) {
141144
if c.Timeout() != 10*time.Millisecond {
142145
t.Errorf("Unexpected Timeout: %v", c.Timeout())
143146
}
144-
if c.IsAllowCode(406) {
145-
t.Errorf("Unexpected IsAllowCode(406): %v", c.IsAllowCode(406))
147+
for code := 300; code < 600; code++ {
148+
if c.IsAllowCode(code) {
149+
t.Errorf("Unexpected IsAllowCode(%d): %v", code, c.IsAllowCode(code))
150+
}
146151
}
147152
if !c.IsAllowCode(200) {
148153
t.Errorf("Unexpected IsAllowCode(200): %v", c.IsAllowCode(200))
149154
}
150-
if !c.IsBlockCode(406) {
151-
t.Errorf("Unexpected IsBlockCode(406): %v", c.IsBlockCode(406))
155+
for code := 300; code < 600; code++ {
156+
if !c.IsBlockCode(code) {
157+
t.Errorf("Unexpected IsBlockCode(%d): %v", code, c.IsBlockCode(code))
158+
}
152159
}
153-
if !c.IsBlockCode(403) {
154-
t.Errorf("Unexpected IsBlockCode(403): %v", c.IsBlockCode(403))
160+
if c.IsBlockCode(600) {
161+
t.Errorf("Unexpected IsBlockCode(600): %v", c.IsBlockCode(600))
155162
}
156163
if c.IsBlockCode(200) {
157164
t.Errorf("Unexpected IsBlockCode(200): %v", c.IsBlockCode(200))
@@ -184,9 +191,8 @@ func TestFromModuleConfig(t *testing.T) {
184191
if c.AllowUnknownContentLength() != true {
185192
t.Errorf("Unexpected AllowUnknownContentLength: %v", c.AllowUnknownContentLength())
186193
}
187-
altResponseCodes := c.AltResponseCodes()
188-
if len(altResponseCodes) != 1 || altResponseCodes[0] != 403 {
189-
t.Errorf("Unexpected AltResponseCodes: %v", altResponseCodes)
194+
if c.AltResponseCodes() != nil {
195+
t.Errorf("Unexpected AltResponseCodes from deprecated option (should be nil): %v", c.AltResponseCodes())
190196
}
191197
if c.AnomalyDuration() != 10*time.Second {
192198
t.Errorf("Unexpected AnomalyDuration: %v", c.AnomalyDuration())
@@ -230,17 +236,21 @@ func TestFromModuleConfig(t *testing.T) {
230236
if c.Timeout() != 10*time.Millisecond {
231237
t.Errorf("Unexpected Timeout: %v", c.Timeout())
232238
}
233-
if c.IsAllowCode(406) {
234-
t.Errorf("Unexpected IsAllowCode(406): %v", c.IsAllowCode(406))
239+
for code := 300; code < 600; code++ {
240+
if c.IsAllowCode(code) {
241+
t.Errorf("Unexpected IsAllowCode(%d): %v", code, c.IsAllowCode(code))
242+
}
235243
}
236244
if !c.IsAllowCode(200) {
237245
t.Errorf("Unexpected IsAllowCode(200): %v", c.IsAllowCode(200))
238246
}
239-
if !c.IsBlockCode(406) {
240-
t.Errorf("Unexpected IsBlockCode(406): %v", c.IsBlockCode(406))
247+
for code := 300; code < 600; code++ {
248+
if !c.IsBlockCode(code) {
249+
t.Errorf("Unexpected IsBlockCode(%d): %v", code, c.IsBlockCode(code))
250+
}
241251
}
242-
if !c.IsBlockCode(403) {
243-
t.Errorf("Unexpected IsBlockCode(403): %v", c.IsBlockCode(403))
252+
if c.IsBlockCode(600) {
253+
t.Errorf("Unexpected IsBlockCode(600): %v", c.IsBlockCode(600))
244254
}
245255
if c.IsBlockCode(200) {
246256
t.Errorf("Unexpected IsBlockCode(200): %v", c.IsBlockCode(200))

0 commit comments

Comments
 (0)