2018-10-13 08:10:19 +02:00
|
|
|
// +build go1.11
|
|
|
|
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
2018-10-14 17:16:58 +02:00
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-10-14 17:16:58 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-13 08:10:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProxy_1_11(t *testing.T) {
|
|
|
|
// Setup
|
|
|
|
url1, _ := url.Parse("http://127.0.0.1:27121")
|
|
|
|
url2, _ := url.Parse("http://127.0.0.1:27122")
|
|
|
|
|
|
|
|
targets := []*ProxyTarget{
|
|
|
|
{
|
|
|
|
Name: "target 1",
|
|
|
|
URL: url1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "target 2",
|
|
|
|
URL: url2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rb := NewRandomBalancer(nil)
|
|
|
|
// must add targets:
|
|
|
|
for _, target := range targets {
|
|
|
|
assert.True(t, rb.AddTarget(target))
|
|
|
|
}
|
|
|
|
|
|
|
|
// must ignore duplicates:
|
|
|
|
for _, target := range targets {
|
|
|
|
assert.False(t, rb.AddTarget(target))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(Proxy(rb))
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2019-01-30 12:56:56 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2018-10-13 08:10:19 +02:00
|
|
|
|
|
|
|
// Remote unreachable
|
2019-01-30 12:56:56 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2018-10-13 08:10:19 +02:00
|
|
|
req.URL.Path = "/api/users"
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, "/api/users", req.URL.Path)
|
2019-10-07 01:57:17 +02:00
|
|
|
assert.Equal(t, http.StatusBadGateway, rec.Code)
|
2018-10-13 08:10:19 +02:00
|
|
|
}
|