2020-09-13 11:20:11 +02:00
|
|
|
package proxy
|
|
|
|
|
2020-10-14 00:48:09 +03:00
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// go test -run Test_Proxy_Empty_Host
|
|
|
|
func Test_Proxy_Empty_Upstream_Servers(t *testing.T) {
|
2020-10-15 13:03:06 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
utils.AssertEqual(t, "Servers cannot be empty", r)
|
|
|
|
}
|
|
|
|
}()
|
2020-10-14 00:48:09 +03:00
|
|
|
app := fiber.New()
|
2020-10-15 13:03:06 +02:00
|
|
|
app.Use(Balancer(Config{Servers: []string{}}))
|
2020-10-14 00:48:09 +03:00
|
|
|
}
|
2020-09-27 18:24:05 +02:00
|
|
|
|
2020-10-15 13:15:59 +03:00
|
|
|
// go test -run Test_Proxy_Next
|
2020-10-14 00:48:09 +03:00
|
|
|
func Test_Proxy_Next(t *testing.T) {
|
|
|
|
app := fiber.New()
|
2020-10-21 23:42:03 +02:00
|
|
|
app.Use(Balancer(Config{
|
2020-10-15 16:23:13 +03:00
|
|
|
Servers: []string{"127.0.0.1"},
|
2020-10-14 00:48:09 +03:00
|
|
|
Next: func(_ *fiber.Ctx) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
|
|
|
}
|
2020-09-27 18:24:05 +02:00
|
|
|
|
2020-10-15 13:15:59 +03:00
|
|
|
// go test -run Test_Proxy
|
2020-10-14 00:48:09 +03:00
|
|
|
func Test_Proxy(t *testing.T) {
|
|
|
|
target := fiber.New()
|
|
|
|
target.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.SendStatus(fiber.StatusTeapot)
|
|
|
|
})
|
|
|
|
go func() {
|
|
|
|
utils.AssertEqual(t, nil, target.Listen(":3001"))
|
|
|
|
}()
|
|
|
|
|
2020-10-22 12:16:04 +02:00
|
|
|
time.Sleep(1 * time.Second)
|
2020-10-14 00:48:09 +03:00
|
|
|
|
|
|
|
resp, err := target.Test(httptest.NewRequest("GET", "/", nil), 2000)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, fiber.StatusTeapot, resp.StatusCode)
|
|
|
|
|
|
|
|
app := fiber.New()
|
|
|
|
|
2020-10-21 23:42:03 +02:00
|
|
|
app.Use(Balancer(Config{Servers: []string{"127.0.0.1:3001"}}))
|
2020-10-14 00:48:09 +03:00
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
2020-10-15 16:23:13 +03:00
|
|
|
req.Host = "127.0.0.1:3001"
|
2020-10-14 00:48:09 +03:00
|
|
|
resp, err = app.Test(req)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, fiber.StatusTeapot, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Proxy_Forward(t *testing.T) {
|
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
target := fiber.New(fiber.Config{DisableStartupMessage: true})
|
|
|
|
target.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.SendString("forwarded")
|
|
|
|
})
|
2020-10-22 12:16:04 +02:00
|
|
|
go func() {
|
|
|
|
utils.AssertEqual(t, nil, target.Listen(":50001"))
|
|
|
|
}()
|
|
|
|
time.Sleep(1 * time.Second)
|
2020-10-14 00:48:09 +03:00
|
|
|
|
2020-10-15 16:23:13 +03:00
|
|
|
app.Use(Forward("http://127.0.0.1:50001"))
|
2020-10-14 00:48:09 +03:00
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, "forwarded", string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Proxy_Modify_Response(t *testing.T) {
|
|
|
|
target := fiber.New(fiber.Config{DisableStartupMessage: true})
|
2020-10-22 12:16:04 +02:00
|
|
|
target.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.Status(500).SendString("not modified")
|
|
|
|
})
|
2020-10-14 00:48:09 +03:00
|
|
|
go func() {
|
2020-10-13 19:23:05 -04:00
|
|
|
utils.AssertEqual(t, nil, target.Listen(":50002"))
|
2020-10-14 00:48:09 +03:00
|
|
|
}()
|
2020-10-22 12:16:04 +02:00
|
|
|
time.Sleep(1 * time.Second)
|
2020-10-14 00:48:09 +03:00
|
|
|
|
|
|
|
app := fiber.New()
|
|
|
|
app.Use(Balancer(Config{
|
2020-10-15 16:23:13 +03:00
|
|
|
Servers: []string{"127.0.0.1:50002"},
|
2020-10-14 00:48:09 +03:00
|
|
|
ModifyResponse: func(c *fiber.Ctx) error {
|
|
|
|
c.Response().SetStatusCode(fiber.StatusOK)
|
|
|
|
return c.SendString("modified response")
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, "modified response", string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Proxy_Modify_Request(t *testing.T) {
|
|
|
|
target := fiber.New(fiber.Config{DisableStartupMessage: true})
|
2020-10-22 12:16:04 +02:00
|
|
|
target.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
b := c.Request().Body()
|
|
|
|
return c.SendString(string(b))
|
|
|
|
})
|
2020-10-14 00:48:09 +03:00
|
|
|
go func() {
|
2020-10-13 19:23:05 -04:00
|
|
|
utils.AssertEqual(t, nil, target.Listen(":50003"))
|
2020-10-14 00:48:09 +03:00
|
|
|
}()
|
2020-10-22 12:16:04 +02:00
|
|
|
time.Sleep(1 * time.Second)
|
2020-10-14 00:48:09 +03:00
|
|
|
|
|
|
|
app := fiber.New()
|
|
|
|
app.Use(Balancer(Config{
|
2020-10-15 16:23:13 +03:00
|
|
|
Servers: []string{"127.0.0.1:50003"},
|
2020-10-14 00:48:09 +03:00
|
|
|
ModifyRequest: func(c *fiber.Ctx) error {
|
|
|
|
c.Request().SetBody([]byte("modified request"))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
utils.AssertEqual(t, nil, err)
|
|
|
|
utils.AssertEqual(t, "modified request", string(b))
|
|
|
|
}
|