File: retry_test.go

package info (click to toggle)
golang-github-vulcand-oxy 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 728 kB
  • sloc: makefile: 14
file content (90 lines) | stat: -rw-r--r-- 2,673 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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 TestSuccess(t *testing.T) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *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.ParseURI(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 TestRetryOnError(t *testing.T) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *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.ParseURI("http://localhost:64321")))
	require.NoError(t, lb.UpsertServer(testutils.ParseURI(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 TestRetryExceedAttempts(t *testing.T) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *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.ParseURI("http://localhost:64321")))
	require.NoError(t, lb.UpsertServer(testutils.ParseURI("http://localhost:64322")))
	require.NoError(t, lb.UpsertServer(testutils.ParseURI("http://localhost:64323")))
	require.NoError(t, lb.UpsertServer(testutils.ParseURI(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
}