2017-06-03 02:55:59 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-06-04 02:37:35 +02:00
|
|
|
"net/url"
|
2018-02-21 20:44:17 +02:00
|
|
|
"testing"
|
2017-06-04 02:37:35 +02:00
|
|
|
|
2017-06-03 02:55:59 +02:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
closeNotifyRecorder struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
closed chan bool
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func newCloseNotifyRecorder() *closeNotifyRecorder {
|
|
|
|
return &closeNotifyRecorder{
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
make(chan bool, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *closeNotifyRecorder) close() {
|
|
|
|
c.closed <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *closeNotifyRecorder) CloseNotify() <-chan bool {
|
|
|
|
return c.closed
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxy(t *testing.T) {
|
|
|
|
// Setup
|
|
|
|
t1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprint(w, "target 1")
|
|
|
|
}))
|
|
|
|
defer t1.Close()
|
2017-06-04 02:37:35 +02:00
|
|
|
url1, _ := url.Parse(t1.URL)
|
2017-06-03 02:55:59 +02:00
|
|
|
t2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprint(w, "target 2")
|
|
|
|
}))
|
|
|
|
defer t2.Close()
|
2017-06-04 02:37:35 +02:00
|
|
|
url2, _ := url.Parse(t2.URL)
|
|
|
|
|
|
|
|
targets := []*ProxyTarget{
|
2018-02-21 20:44:17 +02:00
|
|
|
{
|
|
|
|
Name: "target 1",
|
|
|
|
URL: url1,
|
2017-06-04 02:37:35 +02:00
|
|
|
},
|
2018-02-21 20:44:17 +02:00
|
|
|
{
|
|
|
|
Name: "target 2",
|
|
|
|
URL: url2,
|
2017-06-04 02:37:35 +02:00
|
|
|
},
|
|
|
|
}
|
2018-02-21 20:44:17 +02:00
|
|
|
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))
|
|
|
|
}
|
2017-06-03 02:55:59 +02:00
|
|
|
|
|
|
|
// Random
|
|
|
|
e := echo.New()
|
2017-06-15 17:19:18 +02:00
|
|
|
e.Use(Proxy(rb))
|
2017-06-03 02:55:59 +02:00
|
|
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := newCloseNotifyRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
body := rec.Body.String()
|
2017-06-04 02:37:35 +02:00
|
|
|
expected := map[string]bool{
|
2017-06-03 02:55:59 +02:00
|
|
|
"target 1": true,
|
|
|
|
"target 2": true,
|
|
|
|
}
|
|
|
|
assert.Condition(t, func() bool {
|
2017-06-04 02:37:35 +02:00
|
|
|
return expected[body]
|
2017-06-03 02:55:59 +02:00
|
|
|
})
|
|
|
|
|
2018-02-21 20:44:17 +02:00
|
|
|
for _, target := range targets {
|
|
|
|
assert.True(t, rb.RemoveTarget(target.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.False(t, rb.RemoveTarget("unknown target"))
|
|
|
|
|
2017-06-03 02:55:59 +02:00
|
|
|
// Round-robin
|
2017-12-26 00:39:37 +02:00
|
|
|
rrb := NewRoundRobinBalancer(targets)
|
2017-06-03 02:55:59 +02:00
|
|
|
e = echo.New()
|
2017-06-15 17:19:18 +02:00
|
|
|
e.Use(Proxy(rrb))
|
2017-06-03 02:55:59 +02:00
|
|
|
rec = newCloseNotifyRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
body = rec.Body.String()
|
|
|
|
assert.Equal(t, "target 1", body)
|
|
|
|
rec = newCloseNotifyRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
body = rec.Body.String()
|
|
|
|
assert.Equal(t, "target 2", body)
|
2017-12-28 01:14:44 +02:00
|
|
|
|
|
|
|
// Rewrite
|
|
|
|
e = echo.New()
|
2017-12-28 21:24:34 +02:00
|
|
|
e.Use(ProxyWithConfig(ProxyConfig{
|
2017-12-28 01:14:44 +02:00
|
|
|
Balancer: rrb,
|
|
|
|
Rewrite: map[string]string{
|
|
|
|
"/old": "/new",
|
|
|
|
"/api/*": "/$1",
|
|
|
|
"/js/*": "/public/javascripts/$1",
|
|
|
|
"/users/*/orders/*": "/user/$1/order/$2",
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
req.URL.Path = "/api/users"
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, "/users", req.URL.Path)
|
|
|
|
req.URL.Path = "/js/main.js"
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, "/public/javascripts/main.js", req.URL.Path)
|
|
|
|
req.URL.Path = "/old"
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, "/new", req.URL.Path)
|
|
|
|
req.URL.Path = "/users/jack/orders/1"
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, "/user/jack/order/1", req.URL.Path)
|
2018-10-09 05:43:39 +02:00
|
|
|
|
|
|
|
// ProxyTarget is set in context
|
|
|
|
contextObserver := func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) (err error) {
|
|
|
|
next(c)
|
|
|
|
assert.Contains(t, targets, c.Get("target"), "target is not set in context")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rrb1 := NewRoundRobinBalancer(targets)
|
|
|
|
e = echo.New()
|
|
|
|
e.Use(contextObserver)
|
|
|
|
e.Use(Proxy(rrb1))
|
|
|
|
rec = newCloseNotifyRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
2017-06-03 02:55:59 +02:00
|
|
|
}
|