Skip to content

Commit

Permalink
if no codes set for repeater, treat any >400 as error
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Nov 5, 2023
1 parent 5921c97 commit 69afd00
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 55 deletions.
8 changes: 4 additions & 4 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"net/http"
)

//go:generate moq -out mocks/repeater.go -pkg mocks -skip-ensure -fmt goimports . RepeaterSvc
//go:generate moq -out mocks/circuit_breaker.go -pkg mocks -skip-ensure -fmt goimports . CircuitBreakerSvc
//go:generate moq -out mocks/logger.go -pkg mocks -skip-ensure -fmt goimports logger Service:LoggerSvc
//go:generate moq -out mocks/cache.go -pkg mocks -skip-ensure -fmt goimports cache Service:CacheSvc
//go:generate moq -out mocks/repeater.go -pkg mocks -skip-ensure -with-resets -fmt goimports . RepeaterSvc
//go:generate moq -out mocks/circuit_breaker.go -pkg mocks -skip-ensure -with-resets -fmt goimports . CircuitBreakerSvc
//go:generate moq -out mocks/logger.go -pkg mocks -skip-ensure -with-resets -fmt goimports logger Service:LoggerSvc
//go:generate moq -out mocks/cache.go -pkg mocks -skip-ensure -with-resets -fmt goimports cache Service:CacheSvc

// RoundTripperHandler is a type for middleware handler
type RoundTripperHandler func(http.RoundTripper) http.RoundTripper
Expand Down
37 changes: 26 additions & 11 deletions middleware/mocks/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 26 additions & 11 deletions middleware/mocks/circuit_breaker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 26 additions & 11 deletions middleware/mocks/logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 26 additions & 11 deletions middleware/mocks/repeater.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions middleware/mocks/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
func (r *RoundTripper) Calls() int {
return int(atomic.LoadInt32(&r.calls))
}

// ResetCalls resets calls counter
func (r *RoundTripper) ResetCalls() {
atomic.StoreInt32(&r.calls, 0)
}
5 changes: 5 additions & 0 deletions middleware/repeater.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func Repeater(repeater RepeaterSvc, failOnCodes ...int) RoundTripperHandler {
if err != nil {
return err
}
// no explicit codes provided, fail on any 4xx or 5xx
if len(failOnCodes) == 0 && resp.StatusCode >= 400 {
return errors.New(resp.Status)
}
// fail on provided codes only
for _, fc := range failOnCodes {
if resp.StatusCode == fc {
return errors.New(resp.Status)
Expand Down
26 changes: 19 additions & 7 deletions middleware/repeater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,30 @@ func TestRepeater_FailedStatus(t *testing.T) {
return err
}}

{
h := Repeater(repeater, 300, 400, 401)

t.Run("no codes", func(t *testing.T) {
rmock.ResetCalls()
h := Repeater(repeater)
req, err := http.NewRequest("GET", "http://example.com/blah", http.NoBody)
require.NoError(t, err)

_, err = h(rmock).RoundTrip(req)
require.EqualError(t, err, "repeater: 400 Bad Request")
}
assert.Equal(t, 5, rmock.Calls())
})

assert.Equal(t, 5, rmock.Calls())
t.Run("with codes", func(t *testing.T) {
rmock.ResetCalls()
h := Repeater(repeater, 300, 400, 401)
req, err := http.NewRequest("GET", "http://example.com/blah", http.NoBody)
require.NoError(t, err)

_, err = h(rmock).RoundTrip(req)
require.EqualError(t, err, "repeater: 400 Bad Request")
assert.Equal(t, 5, rmock.Calls())
})

{
t.Run("no codes, no match", func(t *testing.T) {
rmock.ResetCalls()
h := Repeater(repeater, 300, 401)

req, err := http.NewRequest("GET", "http://example.com/blah", http.NoBody)
Expand All @@ -110,6 +121,7 @@ func TestRepeater_FailedStatus(t *testing.T) {
resp, err := h(rmock).RoundTrip(req)
require.NoError(t, err)
assert.Equal(t, 400, resp.StatusCode)
}
assert.Equal(t, 1, rmock.Calls())
})

}

0 comments on commit 69afd00

Please sign in to comment.