mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
🔥 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:
parent
f08ebf4335
commit
359343625b
@ -680,8 +680,16 @@ func New() *Client {
|
|||||||
// trie to use a pool to reduce the cost of memory allocation
|
// trie to use a pool to reduce the cost of memory allocation
|
||||||
// for the fiber client and the fasthttp client
|
// for the fiber client and the fasthttp client
|
||||||
// if possible also for other structs -> request header, cookie, query param, path param...
|
// 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{
|
return &Client{
|
||||||
fasthttp: &fasthttp.Client{},
|
fasthttp: c,
|
||||||
header: &Header{
|
header: &Header{
|
||||||
RequestHeader: &fasthttp.RequestHeader{},
|
RequestHeader: &fasthttp.RequestHeader{},
|
||||||
},
|
},
|
||||||
|
@ -55,6 +55,29 @@ func startTestServerWithPort(t *testing.T, beforeStarting func(app *fiber.App))
|
|||||||
return nil, ""
|
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) {
|
func Test_Client_Add_Hook(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ type Client struct {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
New
|
### New
|
||||||
|
|
||||||
New creates and returns a new Client object.
|
New creates and returns a new Client object.
|
||||||
|
|
||||||
@ -103,6 +103,14 @@ New creates and returns a new Client object.
|
|||||||
func New() *Client
|
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
|
## REST Methods
|
||||||
|
|
||||||
### Get
|
### Get
|
||||||
|
Loading…
x
Reference in New Issue
Block a user