1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 16:03:48 +00:00
fiber/middleware/proxy/proxy_test.go

173 lines
3.9 KiB
Go
Raw Normal View History

2020-09-13 11:20:11 +02:00
package proxy
import (
"fmt"
"io/ioutil"
"net/http/httptest"
"strings"
"testing"
2020-09-14 12:34:31 +02:00
"time"
2020-09-13 11:20:11 +02:00
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
2020-09-13 11:20:11 +02:00
)
// go test -run Test_Proxy_Empty_Host
func Test_Proxy_Empty_Host(t *testing.T) {
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
app.Use(New(
Config{Hosts: ""},
))
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
}
// go test -run Test_Proxy_Next
func Test_Proxy_Next(t *testing.T) {
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
app.Use(New(Config{
Hosts: "next",
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)
}
// go test -run Test_Proxy
func Test_Proxy(t *testing.T) {
target := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
target.Get("/", func(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusTeapot)
})
go func() {
utils.AssertEqual(t, nil, target.Listen(":3001"))
}()
2020-09-14 12:34:31 +02:00
time.Sleep(time.Second)
2020-09-13 11:20:11 +02: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(fiber.Config{
DisableStartupMessage: true,
})
host := "localhost:3001"
app.Use(New(Config{
Hosts: host,
}))
req := httptest.NewRequest("GET", "/", nil)
req.Host = host
resp, err = app.Test(req)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusTeapot, resp.StatusCode)
}
// go test -run Test_Proxy_Before_With_Error
func Test_Proxy_Before_With_Error(t *testing.T) {
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
errStr := "error after Before"
app.Use(
New(Config{
Hosts: "host",
Before: func(c *fiber.Ctx) error {
return fmt.Errorf(errStr)
},
}))
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
b, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, errStr, string(b))
}
// go test -run Test_Proxy_After_With_Error
func Test_Proxy_After_With_Error(t *testing.T) {
target := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
target.Get("/", func(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusTeapot)
})
go func() {
utils.AssertEqual(t, nil, target.Listen(":3002"))
}()
2020-09-14 12:34:31 +02:00
time.Sleep(time.Second)
2020-09-13 11:20:11 +02: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(fiber.Config{
DisableStartupMessage: true,
})
host := "localhost:3001"
errStr := "error after After"
app.Use(New(Config{
Hosts: host,
After: func(ctx *fiber.Ctx) error {
utils.AssertEqual(t, fiber.StatusTeapot, ctx.Response().StatusCode())
return fmt.Errorf(errStr)
},
}))
req := httptest.NewRequest("GET", "/", nil)
req.Host = host
resp, err = app.Test(req)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
b, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, errStr, string(b))
}
// go test -run Test_Proxy_Do_With_Error
func Test_Proxy_Do_With_Error(t *testing.T) {
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
app.Use(
New(Config{
Hosts: "localhost:90000",
}))
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
b, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, strings.Contains(string(b), "127.0.0.1:90000"))
}