From ecb4f572370048bffd475a5ff580accac7f21a7b Mon Sep 17 00:00:00 2001 From: Mitul Agrawal Date: Sat, 23 Nov 2024 17:20:48 +0530 Subject: [PATCH 1/7] add support to create client from existing client --- client/client.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index d9b9c84c63..6de8078e34 100644 --- a/client/client.go +++ b/client/client.go @@ -680,8 +680,13 @@ 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 eisting client object. +func NewWithClient(c *fasthttp.Client) *Client { return &Client{ - fasthttp: &fasthttp.Client{}, + fasthttp: c, header: &Header{ RequestHeader: &fasthttp.RequestHeader{}, }, From 4a70013e983312af5721379b968d8c08c8ef4f79 Mon Sep 17 00:00:00 2001 From: Mitul Agrawal Date: Sat, 23 Nov 2024 17:30:24 +0530 Subject: [PATCH 2/7] add NewWithClient to documentation --- docs/client/rest.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/client/rest.md b/docs/client/rest.md index 3590407e68..9718131120 100644 --- a/docs/client/rest.md +++ b/docs/client/rest.md @@ -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 From 56b72a4510d8f4d41e7f61d2599c6aadb0ab260b Mon Sep 17 00:00:00 2001 From: Mitul Agrawal Date: Sat, 23 Nov 2024 17:56:31 +0530 Subject: [PATCH 3/7] fix typo in comment --- client/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index 6de8078e34..f1fcfa5071 100644 --- a/client/client.go +++ b/client/client.go @@ -683,7 +683,7 @@ func New() *Client { return NewWithClient(&fasthttp.Client{}) } -// NewWithClient creates and returns a new Client object from an eisting client object. +// NewWithClient creates and returns a new Client object from an existing client object. func NewWithClient(c *fasthttp.Client) *Client { return &Client{ fasthttp: c, From 37a5e6e2581cc7d99dfc76376be4b25c1b9b73cc Mon Sep 17 00:00:00 2001 From: Mitul Agrawal Date: Sat, 23 Nov 2024 17:58:32 +0530 Subject: [PATCH 4/7] fix and shorten comment --- client/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index f1fcfa5071..643fd89154 100644 --- a/client/client.go +++ b/client/client.go @@ -683,7 +683,7 @@ func New() *Client { return NewWithClient(&fasthttp.Client{}) } -// NewWithClient creates and returns a new Client object from an existing client object. +// NewWithClient creates and returns a new Client object from an existing client. func NewWithClient(c *fasthttp.Client) *Client { return &Client{ fasthttp: c, From ec07efc24bef73d9fee4d10c38fed8ffff2d0e83 Mon Sep 17 00:00:00 2001 From: Mitul Agrawal Date: Sat, 23 Nov 2024 18:40:01 +0530 Subject: [PATCH 5/7] add unit test for NewWithClient --- client/client_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/client/client_test.go b/client/client_test.go index 0323f70ccd..70a90793da 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -55,6 +55,17 @@ func startTestServerWithPort(t *testing.T, beforeStarting func(app *fiber.App)) return nil, "" } +func Test_New_With_Client(t *testing.T) { + t.Parallel() + + c := &fasthttp.Client{ + MaxConnsPerHost: 5, + } + client := NewWithClient(c) + + require.NotNil(t, client) +} + func Test_Client_Add_Hook(t *testing.T) { t.Parallel() From d2f7db5d017cac2cb6d2fb9705099af1a7016261 Mon Sep 17 00:00:00 2001 From: Mitul Agrawal Date: Sat, 23 Nov 2024 23:14:52 +0530 Subject: [PATCH 6/7] add nil check and test --- client/client.go | 3 +++ client/client_test.go | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/client/client.go b/client/client.go index 643fd89154..820be4ee41 100644 --- a/client/client.go +++ b/client/client.go @@ -685,6 +685,9 @@ func New() *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: c, header: &Header{ diff --git a/client/client_test.go b/client/client_test.go index 70a90793da..772a89e481 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -58,12 +58,24 @@ func startTestServerWithPort(t *testing.T, beforeStarting func(app *fiber.App)) func Test_New_With_Client(t *testing.T) { t.Parallel() - c := &fasthttp.Client{ - MaxConnsPerHost: 5, - } - client := NewWithClient(c) + t.Run("with valid client", func(t *testing.T) { + t.Parallel() + + c := &fasthttp.Client{ + MaxConnsPerHost: 5, + } + client := NewWithClient(c) + + require.NotNil(t, client) + }) - 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) { From 1cb5ee030992d352ccdf3c3050717eef7f18ebd8 Mon Sep 17 00:00:00 2001 From: Mitul Agrawal Date: Sat, 23 Nov 2024 23:28:31 +0530 Subject: [PATCH 7/7] fix lint check --- client/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/client_test.go b/client/client_test.go index 772a89e481..0a1ad14ab5 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -65,7 +65,7 @@ func Test_New_With_Client(t *testing.T) { MaxConnsPerHost: 5, } client := NewWithClient(c) - + require.NotNil(t, client) })