Skip to content

Commit

Permalink
optimize: add WithClient
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Sep 24, 2022
1 parent d461bf2 commit 73ce2b4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions middleware/proxy/README.md
Expand Up @@ -37,6 +37,12 @@ proxy.WithTlsConfig(&tls.Config{
InsecureSkipVerify: true,
})

// if you need to use self-custom client,you should use proxy.WithClient
proxy.WithClient(&fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
})

// Forward to url
app.Get("/gif", proxy.Forward("https://i.imgur.com/IWaBepg.gif"))

Expand Down
9 changes: 8 additions & 1 deletion middleware/proxy/proxy.go
Expand Up @@ -97,17 +97,24 @@ func Balancer(config Config) fiber.Handler {
}
}

var client = fasthttp.Client{
var client = &fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
}

// WithTlsConfig update http client with a user specified tls.config
// This function should be called before Do and Forward.
// Deprecated: use WithClient instead.
func WithTlsConfig(tlsConfig *tls.Config) {
client.TLSConfig = tlsConfig
}

// WithClient sets the proxy client.
// This function should be called before Do and Forward.
func WithClient(cli *fasthttp.Client) {
client = cli
}

// Forward performs the given http request and fills the given http response.
// This method will return an fiber.Handler
func Forward(addr string) fiber.Handler {
Expand Down
35 changes: 35 additions & 0 deletions middleware/proxy/proxy_test.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/internal/tlstest"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
)

func createProxyTestServer(handler fiber.Handler, t *testing.T) (*fiber.App, string) {
Expand Down Expand Up @@ -364,6 +365,7 @@ func Test_Proxy_Do_RestoreOriginalURL(t *testing.T) {
utils.AssertEqual(t, nil, err2)
}

// go test -race -run Test_Proxy_Do_HTTP_Prefix_URL
func Test_Proxy_Do_HTTP_Prefix_URL(t *testing.T) {
t.Parallel()

Expand All @@ -390,3 +392,36 @@ func Test_Proxy_Do_HTTP_Prefix_URL(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "hello world", string(s))
}

// go test -race -run Test_Proxy_Forward_With_Client
func Test_Proxy_Forward_With_Client(t *testing.T) {
t.Parallel()
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:7777")
utils.AssertEqual(t, nil, err)
WithClient(&fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
Dial: func(addr string) (net.Conn, error) {
utils.AssertEqual(t, "127.0.0.1:7777", addr)
return fasthttp.Dial(addr)
},
})
// reset global client
defer WithClient(&fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
})
app := fiber.New(fiber.Config{DisableStartupMessage: true})
app.Get("/test_client", func(c *fiber.Ctx) error {
return c.SendString("test_client")
})

addr := ln.Addr().String()
app.Use(Forward("http://" + addr + "/test_client"))
go func() { utils.AssertEqual(t, nil, app.Listener(ln)) }()

code, body, errs := fiber.Get("http://" + addr).String()
utils.AssertEqual(t, 0, len(errs))
utils.AssertEqual(t, fiber.StatusOK, code)
utils.AssertEqual(t, "test_client", body)
}

0 comments on commit 73ce2b4

Please sign in to comment.