🔥 feat: Add support for creating Fiber client from existing FastHTTP client (#3214)

* add support to create client from existing client

* add NewWithClient to documentation

* fix typo in comment

* fix and shorten comment

* add unit test for NewWithClient

* add nil check and test

* fix lint check
This commit is contained in:
Mitul Agrawal 2024-11-25 16:24:07 +05:30 committed by GitHub
parent f08ebf4335
commit 359343625b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 2 deletions

View File

@ -680,8 +680,16 @@ func New() *Client {
// trie to use a pool to reduce the cost of memory allocation
// for the fiber client and the fasthttp client
// if possible also for other structs -> request header, cookie, query param, path param...
return NewWithClient(&fasthttp.Client{})
}
// NewWithClient creates and returns a new Client object from an existing client.
func NewWithClient(c *fasthttp.Client) *Client {
if c == nil {
panic("fasthttp.Client must not be nil")
}
return &Client{
fasthttp: &fasthttp.Client{},
fasthttp: c,
header: &Header{
RequestHeader: &fasthttp.RequestHeader{},
},

View File

@ -55,6 +55,29 @@ func startTestServerWithPort(t *testing.T, beforeStarting func(app *fiber.App))
return nil, ""
}
func Test_New_With_Client(t *testing.T) {
t.Parallel()
t.Run("with valid client", func(t *testing.T) {
t.Parallel()
c := &fasthttp.Client{
MaxConnsPerHost: 5,
}
client := NewWithClient(c)
require.NotNil(t, client)
})
t.Run("with nil client", func(t *testing.T) {
t.Parallel()
require.PanicsWithValue(t, "fasthttp.Client must not be nil", func() {
NewWithClient(nil)
})
})
}
func Test_Client_Add_Hook(t *testing.T) {
t.Parallel()

View File

@ -95,7 +95,7 @@ type Client struct {
}
```
New
### New
New creates and returns a new Client object.
@ -103,6 +103,14 @@ New creates and returns a new Client object.
func New() *Client
```
### NewWithClient
NewWithClient creates and returns a new Client object from an existing client object.
```go title="Signature"
func NewWithClient(c *fasthttp.Client) *Client
```
## REST Methods
### Get