mirror of
https://github.com/hanzoai/oxy.git
synced 2026-09-30 23:42:31 +00:00
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
package buffer
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/vulcand/oxy/v2/forward"
|
|
"github.com/vulcand/oxy/v2/roundrobin"
|
|
"github.com/vulcand/oxy/v2/testutils"
|
|
)
|
|
|
|
func TestBuffer_success(t *testing.T) {
|
|
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = w.Write([]byte("hello"))
|
|
})
|
|
t.Cleanup(srv.Close)
|
|
|
|
lb, rt := newBufferMiddleware(t, `IsNetworkError() && Attempts() <= 2`)
|
|
|
|
proxy := httptest.NewServer(rt)
|
|
t.Cleanup(proxy.Close)
|
|
|
|
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI(srv.URL)))
|
|
|
|
re, body, err := testutils.Get(proxy.URL)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, re.StatusCode)
|
|
assert.Equal(t, "hello", string(body))
|
|
}
|
|
|
|
func TestBuffer_retryOnError(t *testing.T) {
|
|
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = w.Write([]byte("hello"))
|
|
})
|
|
t.Cleanup(srv.Close)
|
|
|
|
lb, rt := newBufferMiddleware(t, `IsNetworkError() && Attempts() <= 2`)
|
|
|
|
proxy := httptest.NewServer(rt)
|
|
t.Cleanup(proxy.Close)
|
|
|
|
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI("http://localhost:64321")))
|
|
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI(srv.URL)))
|
|
|
|
re, body, err := testutils.Get(proxy.URL, testutils.Body("some request parameters"))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, re.StatusCode)
|
|
assert.Equal(t, "hello", string(body))
|
|
}
|
|
|
|
func TestBuffer_retryExceedAttempts(t *testing.T) {
|
|
srv := testutils.NewHandler(func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = w.Write([]byte("hello"))
|
|
})
|
|
t.Cleanup(srv.Close)
|
|
|
|
lb, rt := newBufferMiddleware(t, `IsNetworkError() && Attempts() <= 2`)
|
|
|
|
proxy := httptest.NewServer(rt)
|
|
t.Cleanup(proxy.Close)
|
|
|
|
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI("http://localhost:64321")))
|
|
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI("http://localhost:64322")))
|
|
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI("http://localhost:64323")))
|
|
require.NoError(t, lb.UpsertServer(testutils.MustParseRequestURI(srv.URL)))
|
|
|
|
re, _, err := testutils.Get(proxy.URL)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusBadGateway, re.StatusCode)
|
|
}
|
|
|
|
func newBufferMiddleware(t *testing.T, p string) (*roundrobin.RoundRobin, *Buffer) {
|
|
t.Helper()
|
|
|
|
// forwarder will proxy the request to whatever destination
|
|
fwd := forward.New(false)
|
|
|
|
// load balancer will round robin request
|
|
lb, err := roundrobin.New(fwd)
|
|
require.NoError(t, err)
|
|
|
|
// stream handler will forward requests to redirect, make sure it uses files
|
|
st, err := New(lb, Retry(p), MemRequestBodyBytes(1))
|
|
require.NoError(t, err)
|
|
|
|
return lb, st
|
|
}
|