Refuse recursive requests
Co-authored-by: Michael <michael.matur@gmail.com>
This commit is contained in:
parent
088fe3c270
commit
186e3e1541
17 changed files with 519 additions and 243 deletions
|
@ -0,0 +1,38 @@
|
|||
package denyrouterrecursion
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestServeHTTP(t *testing.T) {
|
||||
req, err := http.NewRequest(http.MethodGet, "", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = New("", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))
|
||||
require.Error(t, err)
|
||||
|
||||
next := 0
|
||||
m, err := New("myRouter", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
|
||||
next++
|
||||
}))
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
m.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, recorder.Code)
|
||||
assert.Equal(t, "995d26092d19a224", m.routerNameHash)
|
||||
assert.Equal(t, m.routerNameHash, req.Header.Get(xTraefikRouter))
|
||||
assert.Equal(t, 1, next)
|
||||
|
||||
recorder = httptest.NewRecorder()
|
||||
m.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, 1, next)
|
||||
assert.Equal(t, http.StatusBadRequest, recorder.Code)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue