2019-08-26 17:00:04 +00:00
|
|
|
package mirror
|
|
|
|
|
|
|
|
import (
|
2020-03-05 17:03:08 +00:00
|
|
|
"bytes"
|
2019-08-26 17:00:04 +00:00
|
|
|
"context"
|
2021-03-04 19:08:03 +00:00
|
|
|
"io"
|
2019-08-26 17:00:04 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/safe"
|
2019-08-26 17:00:04 +00:00
|
|
|
)
|
|
|
|
|
2020-03-05 17:03:08 +00:00
|
|
|
const defaultMaxBodySize int64 = -1
|
|
|
|
|
2019-08-26 17:00:04 +00:00
|
|
|
func TestMirroringOn100(t *testing.T) {
|
|
|
|
var countMirror1, countMirror2 int32
|
|
|
|
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
pool := safe.NewPool(context.Background())
|
2024-09-02 14:36:06 +00:00
|
|
|
mirror := New(handler, pool, true, defaultMaxBodySize, nil)
|
2019-08-26 17:00:04 +00:00
|
|
|
err := mirror.AddMirror(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
atomic.AddInt32(&countMirror1, 1)
|
|
|
|
}), 10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = mirror.AddMirror(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
atomic.AddInt32(&countMirror2, 1)
|
|
|
|
}), 50)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-02-19 14:44:03 +00:00
|
|
|
for range 100 {
|
2019-08-26 17:00:04 +00:00
|
|
|
mirror.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
|
|
|
|
val1 := atomic.LoadInt32(&countMirror1)
|
|
|
|
val2 := atomic.LoadInt32(&countMirror2)
|
|
|
|
assert.Equal(t, 10, int(val1))
|
|
|
|
assert.Equal(t, 50, int(val2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMirroringOn10(t *testing.T) {
|
|
|
|
var countMirror1, countMirror2 int32
|
|
|
|
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
pool := safe.NewPool(context.Background())
|
2024-09-02 14:36:06 +00:00
|
|
|
mirror := New(handler, pool, true, defaultMaxBodySize, nil)
|
2019-08-26 17:00:04 +00:00
|
|
|
err := mirror.AddMirror(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
atomic.AddInt32(&countMirror1, 1)
|
|
|
|
}), 10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = mirror.AddMirror(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
atomic.AddInt32(&countMirror2, 1)
|
|
|
|
}), 50)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-02-19 14:44:03 +00:00
|
|
|
for range 10 {
|
2019-08-26 17:00:04 +00:00
|
|
|
mirror.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
|
|
|
|
val1 := atomic.LoadInt32(&countMirror1)
|
|
|
|
val2 := atomic.LoadInt32(&countMirror2)
|
|
|
|
assert.Equal(t, 1, int(val1))
|
|
|
|
assert.Equal(t, 5, int(val2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidPercent(t *testing.T) {
|
2024-09-02 14:36:06 +00:00
|
|
|
mirror := New(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}), safe.NewPool(context.Background()), true, defaultMaxBodySize, nil)
|
2019-08-26 17:00:04 +00:00
|
|
|
err := mirror.AddMirror(nil, -1)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = mirror.AddMirror(nil, 101)
|
|
|
|
assert.Error(t, err)
|
2019-08-29 08:28:05 +00:00
|
|
|
|
|
|
|
err = mirror.AddMirror(nil, 100)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = mirror.AddMirror(nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHijack(t *testing.T) {
|
|
|
|
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
pool := safe.NewPool(context.Background())
|
2024-09-02 14:36:06 +00:00
|
|
|
mirror := New(handler, pool, true, defaultMaxBodySize, nil)
|
2019-08-29 08:28:05 +00:00
|
|
|
|
|
|
|
var mirrorRequest bool
|
|
|
|
err := mirror.AddMirror(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
hijacker, ok := rw.(http.Hijacker)
|
2023-11-17 00:50:06 +00:00
|
|
|
assert.True(t, ok)
|
2019-08-29 08:28:05 +00:00
|
|
|
|
|
|
|
_, _, err := hijacker.Hijack()
|
|
|
|
assert.Error(t, err)
|
|
|
|
mirrorRequest = true
|
|
|
|
}), 100)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
mirror.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
|
|
|
|
pool.Stop()
|
2023-11-17 00:50:06 +00:00
|
|
|
assert.True(t, mirrorRequest)
|
2019-08-29 08:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlush(t *testing.T) {
|
|
|
|
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
pool := safe.NewPool(context.Background())
|
2024-09-02 14:36:06 +00:00
|
|
|
mirror := New(handler, pool, true, defaultMaxBodySize, nil)
|
2019-08-29 08:28:05 +00:00
|
|
|
|
|
|
|
var mirrorRequest bool
|
|
|
|
err := mirror.AddMirror(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
hijacker, ok := rw.(http.Flusher)
|
2023-11-17 00:50:06 +00:00
|
|
|
assert.True(t, ok)
|
2019-08-29 08:28:05 +00:00
|
|
|
|
|
|
|
hijacker.Flush()
|
|
|
|
|
|
|
|
mirrorRequest = true
|
|
|
|
}), 100)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
mirror.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
|
|
|
|
pool.Stop()
|
2023-11-17 00:50:06 +00:00
|
|
|
assert.True(t, mirrorRequest)
|
2019-08-26 17:00:04 +00:00
|
|
|
}
|
2020-03-05 17:03:08 +00:00
|
|
|
|
|
|
|
func TestMirroringWithBody(t *testing.T) {
|
|
|
|
const numMirrors = 10
|
|
|
|
|
|
|
|
var (
|
|
|
|
countMirror int32
|
|
|
|
body = []byte(`body`)
|
|
|
|
)
|
|
|
|
|
|
|
|
pool := safe.NewPool(context.Background())
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.NotNil(t, r.Body)
|
2021-03-04 19:08:03 +00:00
|
|
|
bb, err := io.ReadAll(r.Body)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, body, bb)
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
|
2024-09-02 14:36:06 +00:00
|
|
|
mirror := New(handler, pool, true, defaultMaxBodySize, nil)
|
2020-03-05 17:03:08 +00:00
|
|
|
|
2024-02-19 14:44:03 +00:00
|
|
|
for range numMirrors {
|
2020-03-05 17:03:08 +00:00
|
|
|
err := mirror.AddMirror(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.NotNil(t, r.Body)
|
2021-03-04 19:08:03 +00:00
|
|
|
bb, err := io.ReadAll(r.Body)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, body, bb)
|
|
|
|
atomic.AddInt32(&countMirror, 1)
|
|
|
|
}), 100)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewBuffer(body))
|
|
|
|
|
|
|
|
mirror.ServeHTTP(httptest.NewRecorder(), req)
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
|
|
|
|
val := atomic.LoadInt32(&countMirror)
|
|
|
|
assert.Equal(t, numMirrors, int(val))
|
|
|
|
}
|
|
|
|
|
2024-09-02 14:36:06 +00:00
|
|
|
func TestMirroringWithIgnoredBody(t *testing.T) {
|
|
|
|
const numMirrors = 10
|
|
|
|
|
|
|
|
var (
|
|
|
|
countMirror int32
|
|
|
|
body = []byte(`body`)
|
|
|
|
emptyBody = []byte(``)
|
|
|
|
)
|
|
|
|
|
|
|
|
pool := safe.NewPool(context.Background())
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.NotNil(t, r.Body)
|
|
|
|
bb, err := io.ReadAll(r.Body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, body, bb)
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
|
|
|
|
mirror := New(handler, pool, false, defaultMaxBodySize, nil)
|
|
|
|
|
|
|
|
for range numMirrors {
|
|
|
|
err := mirror.AddMirror(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.NotNil(t, r.Body)
|
|
|
|
bb, err := io.ReadAll(r.Body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, emptyBody, bb)
|
|
|
|
atomic.AddInt32(&countMirror, 1)
|
|
|
|
}), 100)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewBuffer(body))
|
|
|
|
|
|
|
|
mirror.ServeHTTP(httptest.NewRecorder(), req)
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
|
|
|
|
val := atomic.LoadInt32(&countMirror)
|
|
|
|
assert.Equal(t, numMirrors, int(val))
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:03:08 +00:00
|
|
|
func TestCloneRequest(t *testing.T) {
|
|
|
|
t.Run("http request body is nil", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest(http.MethodPost, "/", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := req.Context()
|
2024-09-02 14:36:06 +00:00
|
|
|
rr, _, err := newReusableRequest(req, true, defaultMaxBodySize)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// first call
|
|
|
|
cloned := rr.clone(ctx)
|
|
|
|
assert.Equal(t, cloned, req)
|
|
|
|
assert.Nil(t, cloned.Body)
|
|
|
|
|
|
|
|
// second call
|
|
|
|
cloned = rr.clone(ctx)
|
|
|
|
assert.Equal(t, cloned, req)
|
|
|
|
assert.Nil(t, cloned.Body)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("http request body is not nil", func(t *testing.T) {
|
|
|
|
bb := []byte(`¯\_(ツ)_/¯`)
|
|
|
|
contentLength := len(bb)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(bb)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, "/", buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := req.Context()
|
|
|
|
req.ContentLength = int64(contentLength)
|
|
|
|
|
2024-09-02 14:36:06 +00:00
|
|
|
rr, _, err := newReusableRequest(req, true, defaultMaxBodySize)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// first call
|
|
|
|
cloned := rr.clone(ctx)
|
2021-03-04 19:08:03 +00:00
|
|
|
body, err := io.ReadAll(cloned.Body)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, bb, body)
|
|
|
|
|
|
|
|
// second call
|
|
|
|
cloned = rr.clone(ctx)
|
2021-03-04 19:08:03 +00:00
|
|
|
body, err = io.ReadAll(cloned.Body)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, bb, body)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failed case", func(t *testing.T) {
|
|
|
|
bb := []byte(`1234567890`)
|
|
|
|
buf := bytes.NewBuffer(bb)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, "/", buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-02 14:36:06 +00:00
|
|
|
_, expectedBytes, err := newReusableRequest(req, true, 2)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.Error(t, err)
|
2023-11-17 00:50:06 +00:00
|
|
|
assert.Equal(t, expectedBytes, bb[:3])
|
2020-03-05 17:03:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("valid case with maxBodySize", func(t *testing.T) {
|
|
|
|
bb := []byte(`1234567890`)
|
|
|
|
buf := bytes.NewBuffer(bb)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, "/", buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-02 14:36:06 +00:00
|
|
|
rr, expectedBytes, err := newReusableRequest(req, true, 20)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, expectedBytes)
|
2021-09-02 08:46:13 +00:00
|
|
|
assert.Len(t, rr.body, 10)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("valid GET case with maxBodySize", func(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "/", buf)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-02 14:36:06 +00:00
|
|
|
rr, expectedBytes, err := newReusableRequest(req, true, 20)
|
2021-09-02 08:46:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, expectedBytes)
|
2023-11-17 00:50:06 +00:00
|
|
|
assert.Empty(t, rr.body)
|
2020-03-05 17:03:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no request given", func(t *testing.T) {
|
2024-09-02 14:36:06 +00:00
|
|
|
_, _, err := newReusableRequest(nil, true, defaultMaxBodySize)
|
2020-03-05 17:03:08 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|