Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Aug 7, 2023
1 parent e4a79a6 commit 41ae1a5
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 91 deletions.
20 changes: 4 additions & 16 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/x509"
"encoding/json"
"encoding/xml"
"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/utils/v2"
"io"
"net/url"
"os"
Expand All @@ -14,7 +16,6 @@ import (
"sync"
"time"

"github.com/gofiber/utils/v2"
"github.com/valyala/fasthttp"
)

Expand Down Expand Up @@ -239,6 +240,8 @@ func (c *Client) SetRootCertificateFromString(pem string) *Client {
func (c *Client) SetProxyURL(proxyURL string) *Client {
pUrl, err := url.Parse(proxyURL)

Check warning on line 241 in client/client.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var pUrl should be pURL (revive)
if err != nil {
log.Errorf("%v", err)

return c
}
c.proxyURL = pUrl.String()
Expand Down Expand Up @@ -474,21 +477,6 @@ func (c *Client) SetTimeout(t time.Duration) *Client {
return c
}

func (c *Client) Logger() Logger {
if c.logger == nil {
return &disableLogger{}
}

return c.logger
}

// SetLogger set logger field in client.
// The logger would output relate info with request.
func (c *Client) SetLogger(logger Logger) *Client {
c.logger = logger
return c
}

// Debug enable log debug level output.
func (c *Client) Debug() *Client {
c.debug = true
Expand Down
226 changes: 159 additions & 67 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package client

import (
"bytes"
"context"
"crypto/tls"
"fmt"
"github.com/gofiber/fiber/v3/addon/retry"
"github.com/gofiber/fiber/v3/log"
"io"
"net"
"os"
"reflect"
"testing"
"time"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/internal/tlstest"
Expand Down Expand Up @@ -813,37 +819,38 @@ func Test_Client_PathParam_With_Server(t *testing.T) {
require.Equal(t, "ok", resp.String())
}

// func Test_Client_Cert(t *testing.T) {
// t.Parallel()
func Test_Client_TLS(t *testing.T) {
t.Parallel()

// serverTLSConf, clientTLSConf, err := tlstest.GetTLSConfigs()
// require.Nil(t, err)
serverTLSConf, clientTLSConf, err := tlstest.GetTLSConfigs()
require.Nil(t, err)

// ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
// require.Nil(t, err)
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
require.Nil(t, err)

// ln = tls.NewListener(ln, serverTLSConf)
ln = tls.NewListener(ln, serverTLSConf)

// app := fiber.New()
// app.Get("/", func(c fiber.Ctx) error {
// return c.SendString("tls")
// })
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("tls")
})

// go func() {
// require.Nil(t, nil, app.Listener(ln, fiber.ListenConfig{
// DisableStartupMessage: true,
// }))
// }()
go func() {
require.Nil(t, app.Listener(ln, fiber.ListenConfig{
DisableStartupMessage: true,
}))
}()

// client := AcquireClient().SetCertificates(clientTLSConf.Certificates...)
// resp, err := client.Get("https://" + ln.Addr().String())
client := AcquireClient()
resp, err := client.SetTLSConfig(clientTLSConf).Get("https://" + ln.Addr().String())

// require.Nil(t, err)
// require.Equal(t, fiber.StatusOK, resp.StatusCode())
// require.Equal(t, "tls", resp.String())
// }
require.Nil(t, err)
require.Equal(t, clientTLSConf, client.TLSConfig())
require.Equal(t, fiber.StatusOK, resp.StatusCode())
require.Equal(t, "tls", resp.String())
}

func Test_Client_TLS(t *testing.T) {
func Test_Client_TLS_Empty_TLSConfig(t *testing.T) {
t.Parallel()

serverTLSConf, clientTLSConf, err := tlstest.GetTLSConfigs()
Expand All @@ -866,12 +873,42 @@ func Test_Client_TLS(t *testing.T) {
}()

client := AcquireClient()
resp, err := client.SetTLSConfig(clientTLSConf).Get("https://" + ln.Addr().String())
resp, err := client.Get("https://" + ln.Addr().String())

require.Error(t, err)
require.NotEqual(t, clientTLSConf, client.TLSConfig())
require.Nil(t, resp)
}

func Test_Client_SetCertificates(t *testing.T) {
t.Parallel()

serverTLSConf, _, err := tlstest.GetTLSConfigs()
require.Nil(t, err)
require.Equal(t, clientTLSConf, client.TLSConfig())
require.Equal(t, fiber.StatusOK, resp.StatusCode())
require.Equal(t, "tls", resp.String())

client := AcquireClient().SetCertificates(serverTLSConf.Certificates...)
require.Equal(t, 1, len(client.tlsConfig.Certificates))
}

func Test_Client_SetRootCertificate(t *testing.T) {
t.Parallel()

client := AcquireClient().SetRootCertificate("../.github/testdata/ssl.pem")
require.NotNil(t, client.tlsConfig.RootCAs)
}

func Test_Client_SetRootCertificateFromString(t *testing.T) {
t.Parallel()

file, err := os.Open("../.github/testdata/ssl.pem")
defer func() { _ = file.Close() }()
require.NoError(t, err)

pem, err := io.ReadAll(file)
require.NoError(t, err)

client := AcquireClient().SetRootCertificateFromString(string(pem))
require.NotNil(t, client.tlsConfig.RootCAs)
}

func Test_Client_R(t *testing.T) {
Expand Down Expand Up @@ -968,70 +1005,125 @@ func Test_Set_Config_To_Request(t *testing.T) {
require.Equal(t, "v1", req.Param("k1")[0])
})

// t.Run("set ctx", func(t *testing.T) {
// key := struct{}{}
t.Run("set cookies", func(t *testing.T) {
req := AcquireRequest()

setConfigToRequest(req, Config{Cookie: map[string]string{
"k1": "v1",
}})

require.Equal(t, "v1", req.Cookie("k1"))
})

t.Run("set pathparam", func(t *testing.T) {
req := AcquireRequest()

setConfigToRequest(req, Config{PathParam: map[string]string{
"k1": "v1",
}})

require.Equal(t, "v1", req.PathParam("k1"))
})

t.Run("set timeout", func(t *testing.T) {
req := AcquireRequest()

setConfigToRequest(req, Config{Timeout: 1 * time.Second})

require.Equal(t, 1*time.Second, req.Timeout())
})

t.Run("set maxredirects", func(t *testing.T) {
req := AcquireRequest()

// ctx := context.Background()
// ctx = context.WithValue(ctx, key, "v1")
setConfigToRequest(req, Config{MaxRedirects: 1})

// req := AcquireRequest()
require.Equal(t, 1, req.MaxRedirects())
})

// setConfigToRequest(req, Config{Ctx: ctx})
t.Run("set body", func(t *testing.T) {
req := AcquireRequest()

// require.Equal(t, "v1", req.Context().Value(key))
// })
setConfigToRequest(req, Config{Body: "test"})

// t.Run("set ctx", func(t *testing.T) {
// key := struct{}{}
require.Equal(t, "test", req.body)
})

// ctx := context.Background()
// ctx = context.WithValue(ctx, key, "v1")
t.Run("set file", func(t *testing.T) {
req := AcquireRequest()

// req := AcquireRequest()
setConfigToRequest(req, Config{File: []*File{
{
name: "test",
path: "path",
},
}})

// setConfigToRequest(req, Config{Ctx: ctx})
require.Equal(t, "path", req.File("test").path)
})
}

// require.Equal(t, "v1", req.Context().Value(key))
// })
func Test_Client_SetProxyURL(t *testing.T) {
t.Parallel()

// t.Run("set ctx", func(t *testing.T) {
// key := struct{}{}
app, dial, start := createHelperServer(t)
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("hello world")
})

// ctx := context.Background()
// ctx = context.WithValue(ctx, key, "v1")
go start()

// req := AcquireRequest()
defer func(app *fiber.App) {
_ = app.Shutdown()
}(app)

// setConfigToRequest(req, Config{Ctx: ctx})
time.Sleep(1 * time.Second)

// require.Equal(t, "v1", req.Context().Value(key))
// })
t.Run("success", func(t *testing.T) {
client := AcquireClient()
client.SetProxyURL("http://test.com")
_, err := client.Get("http://localhost:3000", Config{Dial: dial})

// t.Run("set ctx", func(t *testing.T) {
// key := struct{}{}
require.NoError(t, err)
})

// ctx := context.Background()
// ctx = context.WithValue(ctx, key, "v1")
t.Run("wrong url", func(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)

// req := AcquireRequest()
client := AcquireClient()
client.SetProxyURL(":this is not a url")
_, err := client.Get("http://localhost:3000", Config{Dial: dial})

// setConfigToRequest(req, Config{Ctx: ctx})
require.Contains(t, buf.String(), "missing protocol scheme")
require.NoError(t, err)
})

// require.Equal(t, "v1", req.Context().Value(key))
// })
t.Run("error", func(t *testing.T) {
client := AcquireClient()
client.SetProxyURL("htgdftp://test.com")
_, err := client.Get("http://localhost:3000", Config{Dial: dial})

// t.Run("set ctx", func(t *testing.T) {
// key := struct{}{}
require.Error(t, err)
})
}

// ctx := context.Background()
// ctx = context.WithValue(ctx, key, "v1")
func Test_Client_SetRetryConfig(t *testing.T) {
t.Parallel()

// req := AcquireRequest()
retryConfig := &retry.Config{
InitialInterval: 1 * time.Second,
MaxRetryCount: 3,
}

// setConfigToRequest(req, Config{Ctx: ctx})
core, client, req := newCore(), AcquireClient(), AcquireRequest()
req.SetURL("http://example.com")
client.SetRetryConfig(retryConfig)
_, err := core.execute(context.Background(), client, req)

// require.Equal(t, "v1", req.Context().Value(key))
// })
require.NoError(t, err)
require.Equal(t, retryConfig.InitialInterval, client.RetryConfig().InitialInterval)
require.Equal(t, retryConfig.MaxRetryCount, client.RetryConfig().MaxRetryCount)
}

func Benchmark_Client_Request(b *testing.B) {
Expand Down
7 changes: 3 additions & 4 deletions client/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/utils/v2"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -298,10 +299,8 @@ func logger(c *Client, resp *Response, req *Request) (err error) {
return
}

logger := c.Logger()

logger.Debugf("%s\n", req.RawRequest.String())
logger.Debugf("%s\n", resp.RawResponse.String())
log.Debugf("%s\n", req.RawRequest.String())
log.Debugf("%s\n", resp.RawResponse.String())

return
}
Loading

0 comments on commit 41ae1a5

Please sign in to comment.