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"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/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())
|
2021-06-25 19:08:11 +00:00
|
|
|
mirror := New(handler, pool, 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)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
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())
|
2021-06-25 19:08:11 +00:00
|
|
|
mirror := New(handler, pool, 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)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
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) {
|
2021-06-25 19:08:11 +00:00
|
|
|
mirror := New(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}), safe.NewPool(context.Background()), 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())
|
2021-06-25 19:08:11 +00:00
|
|
|
mirror := New(handler, pool, 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)
|
|
|
|
assert.Equal(t, true, ok)
|
|
|
|
|
|
|
|
_, _, 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()
|
|
|
|
assert.Equal(t, true, mirrorRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlush(t *testing.T) {
|
|
|
|
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
pool := safe.NewPool(context.Background())
|
2021-06-25 19:08:11 +00:00
|
|
|
mirror := New(handler, pool, 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)
|
|
|
|
assert.Equal(t, true, ok)
|
|
|
|
|
|
|
|
hijacker.Flush()
|
|
|
|
|
|
|
|
mirrorRequest = true
|
|
|
|
}), 100)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
mirror.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
assert.Equal(t, true, 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)
|
|
|
|
})
|
|
|
|
|
2021-06-25 19:08:11 +00:00
|
|
|
mirror := New(handler, pool, defaultMaxBodySize, nil)
|
2020-03-05 17:03:08 +00:00
|
|
|
|
|
|
|
for i := 0; i < numMirrors; i++ {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
rr, _, err := newReusableRequest(req, defaultMaxBodySize)
|
|
|
|
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)
|
|
|
|
|
|
|
|
rr, _, err := newReusableRequest(req, defaultMaxBodySize)
|
|
|
|
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)
|
|
|
|
|
|
|
|
_, expectedBytes, err := newReusableRequest(req, 2)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, bb[:3], expectedBytes)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
_, expectedBytes, err := newReusableRequest(req, 20)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, expectedBytes)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no request given", func(t *testing.T) {
|
|
|
|
_, _, err := newReusableRequest(nil, defaultMaxBodySize)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|