github.com/boomhut/fiber/v2@v2.0.0-20230603160335-b65c856e57d3/helpers_test.go (about)

     1  // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
     2  // 📝 Github Repository: https://github.com/gofiber/fiber
     3  // 📌 API Documentation: https://docs.gofiber.io
     4  
     5  package fiber
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/boomhut/fiber/v2/utils"
    14  
    15  	"github.com/valyala/fasthttp"
    16  )
    17  
    18  // go test -v -run=Test_Utils_ -count=3
    19  func Test_Utils_ETag(t *testing.T) {
    20  	t.Parallel()
    21  	app := New()
    22  	t.Run("Not Status OK", func(t *testing.T) {
    23  		t.Parallel()
    24  		c := app.AcquireCtx(&fasthttp.RequestCtx{})
    25  		defer app.ReleaseCtx(c)
    26  		err := c.SendString("Hello, World!")
    27  		utils.AssertEqual(t, nil, err)
    28  		c.Status(201)
    29  		setETag(c, false)
    30  		utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
    31  	})
    32  
    33  	t.Run("No Body", func(t *testing.T) {
    34  		t.Parallel()
    35  		c := app.AcquireCtx(&fasthttp.RequestCtx{})
    36  		defer app.ReleaseCtx(c)
    37  		setETag(c, false)
    38  		utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
    39  	})
    40  
    41  	t.Run("Has HeaderIfNoneMatch", func(t *testing.T) {
    42  		t.Parallel()
    43  		c := app.AcquireCtx(&fasthttp.RequestCtx{})
    44  		defer app.ReleaseCtx(c)
    45  		err := c.SendString("Hello, World!")
    46  		utils.AssertEqual(t, nil, err)
    47  		c.Request().Header.Set(HeaderIfNoneMatch, `"13-1831710635"`)
    48  		setETag(c, false)
    49  		utils.AssertEqual(t, 304, c.Response().StatusCode())
    50  		utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
    51  		utils.AssertEqual(t, "", string(c.Response().Body()))
    52  	})
    53  
    54  	t.Run("No HeaderIfNoneMatch", func(t *testing.T) {
    55  		t.Parallel()
    56  		c := app.AcquireCtx(&fasthttp.RequestCtx{})
    57  		defer app.ReleaseCtx(c)
    58  		err := c.SendString("Hello, World!")
    59  		utils.AssertEqual(t, nil, err)
    60  		setETag(c, false)
    61  		utils.AssertEqual(t, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
    62  	})
    63  }
    64  
    65  // go test -v -run=^$ -bench=Benchmark_App_ETag -benchmem -count=4
    66  func Benchmark_Utils_ETag(b *testing.B) {
    67  	app := New()
    68  	c := app.AcquireCtx(&fasthttp.RequestCtx{})
    69  	defer app.ReleaseCtx(c)
    70  	err := c.SendString("Hello, World!")
    71  	utils.AssertEqual(b, nil, err)
    72  	for n := 0; n < b.N; n++ {
    73  		setETag(c, false)
    74  	}
    75  	utils.AssertEqual(b, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
    76  }
    77  
    78  // go test -v -run=Test_Utils_ETag_Weak -count=1
    79  func Test_Utils_ETag_Weak(t *testing.T) {
    80  	t.Parallel()
    81  	app := New()
    82  	t.Run("Set Weak", func(t *testing.T) {
    83  		t.Parallel()
    84  		c := app.AcquireCtx(&fasthttp.RequestCtx{})
    85  		defer app.ReleaseCtx(c)
    86  		err := c.SendString("Hello, World!")
    87  		utils.AssertEqual(t, nil, err)
    88  		setETag(c, true)
    89  		utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
    90  	})
    91  
    92  	t.Run("Match Weak ETag", func(t *testing.T) {
    93  		t.Parallel()
    94  		c := app.AcquireCtx(&fasthttp.RequestCtx{})
    95  		defer app.ReleaseCtx(c)
    96  		err := c.SendString("Hello, World!")
    97  		utils.AssertEqual(t, nil, err)
    98  		c.Request().Header.Set(HeaderIfNoneMatch, `W/"13-1831710635"`)
    99  		setETag(c, true)
   100  		utils.AssertEqual(t, 304, c.Response().StatusCode())
   101  		utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
   102  		utils.AssertEqual(t, "", string(c.Response().Body()))
   103  	})
   104  
   105  	t.Run("Not Match Weak ETag", func(t *testing.T) {
   106  		t.Parallel()
   107  		c := app.AcquireCtx(&fasthttp.RequestCtx{})
   108  		defer app.ReleaseCtx(c)
   109  		err := c.SendString("Hello, World!")
   110  		utils.AssertEqual(t, nil, err)
   111  		c.Request().Header.Set(HeaderIfNoneMatch, `W/"13-1831710635xx"`)
   112  		setETag(c, true)
   113  		utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
   114  	})
   115  }
   116  
   117  func Test_Utils_UniqueRouteStack(t *testing.T) {
   118  	t.Parallel()
   119  	route1 := &Route{}
   120  	route2 := &Route{}
   121  	route3 := &Route{}
   122  	utils.AssertEqual(
   123  		t,
   124  		[]*Route{
   125  			route1,
   126  			route2,
   127  			route3,
   128  		},
   129  		uniqueRouteStack([]*Route{
   130  			route1,
   131  			route1,
   132  			route1,
   133  			route2,
   134  			route2,
   135  			route2,
   136  			route3,
   137  			route3,
   138  			route3,
   139  			route1,
   140  			route2,
   141  			route3,
   142  		}),
   143  	)
   144  }
   145  
   146  // go test -v -run=^$ -bench=Benchmark_App_ETag_Weak -benchmem -count=4
   147  func Benchmark_Utils_ETag_Weak(b *testing.B) {
   148  	app := New()
   149  	c := app.AcquireCtx(&fasthttp.RequestCtx{})
   150  	defer app.ReleaseCtx(c)
   151  	err := c.SendString("Hello, World!")
   152  	utils.AssertEqual(b, nil, err)
   153  	for n := 0; n < b.N; n++ {
   154  		setETag(c, true)
   155  	}
   156  	utils.AssertEqual(b, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
   157  }
   158  
   159  func Test_Utils_getGroupPath(t *testing.T) {
   160  	t.Parallel()
   161  	res := getGroupPath("/v1", "/")
   162  	utils.AssertEqual(t, "/v1/", res)
   163  
   164  	res = getGroupPath("/v1/", "/")
   165  	utils.AssertEqual(t, "/v1/", res)
   166  
   167  	res = getGroupPath("/", "/")
   168  	utils.AssertEqual(t, "/", res)
   169  
   170  	res = getGroupPath("/v1/api/", "/")
   171  	utils.AssertEqual(t, "/v1/api/", res)
   172  
   173  	res = getGroupPath("/v1/api", "group")
   174  	utils.AssertEqual(t, "/v1/api/group", res)
   175  
   176  	res = getGroupPath("/v1/api", "")
   177  	utils.AssertEqual(t, "/v1/api", res)
   178  }
   179  
   180  // go test -v -run=^$ -bench=Benchmark_Utils_ -benchmem -count=3
   181  
   182  func Benchmark_Utils_getGroupPath(b *testing.B) {
   183  	var res string
   184  	for n := 0; n < b.N; n++ {
   185  		_ = getGroupPath("/v1/long/path/john/doe", "/why/this/name/is/so/awesome")
   186  		_ = getGroupPath("/v1", "/")
   187  		_ = getGroupPath("/v1", "/api")
   188  		res = getGroupPath("/v1", "/api/register/:project")
   189  	}
   190  	utils.AssertEqual(b, "/v1/api/register/:project", res)
   191  }
   192  
   193  func Benchmark_Utils_Unescape(b *testing.B) {
   194  	unescaped := ""
   195  	dst := make([]byte, 0)
   196  
   197  	for n := 0; n < b.N; n++ {
   198  		source := "/cr%C3%A9er"
   199  		pathBytes := utils.UnsafeBytes(source)
   200  		pathBytes = fasthttp.AppendUnquotedArg(dst[:0], pathBytes)
   201  		unescaped = utils.UnsafeString(pathBytes)
   202  	}
   203  
   204  	utils.AssertEqual(b, "/créer", unescaped)
   205  }
   206  
   207  func Test_Utils_Parse_Address(t *testing.T) {
   208  	t.Parallel()
   209  	testCases := []struct {
   210  		addr, host, port string
   211  	}{
   212  		{"[::1]:3000", "[::1]", "3000"},
   213  		{"127.0.0.1:3000", "127.0.0.1", "3000"},
   214  		{"/path/to/unix/socket", "/path/to/unix/socket", ""},
   215  	}
   216  
   217  	for _, c := range testCases {
   218  		host, port := parseAddr(c.addr)
   219  		utils.AssertEqual(t, c.host, host, "addr host")
   220  		utils.AssertEqual(t, c.port, port, "addr port")
   221  	}
   222  }
   223  
   224  func Test_Utils_GetOffset(t *testing.T) {
   225  	t.Parallel()
   226  	utils.AssertEqual(t, "", getOffer("hello", acceptsOffer))
   227  	utils.AssertEqual(t, "1", getOffer("", acceptsOffer, "1"))
   228  	utils.AssertEqual(t, "", getOffer("2", acceptsOffer, "1"))
   229  }
   230  
   231  func Test_Utils_TestConn_Deadline(t *testing.T) {
   232  	t.Parallel()
   233  	conn := &testConn{}
   234  	utils.AssertEqual(t, nil, conn.SetDeadline(time.Time{}))
   235  	utils.AssertEqual(t, nil, conn.SetReadDeadline(time.Time{}))
   236  	utils.AssertEqual(t, nil, conn.SetWriteDeadline(time.Time{}))
   237  }
   238  
   239  func Test_Utils_IsNoCache(t *testing.T) {
   240  	t.Parallel()
   241  	testCases := []struct {
   242  		string
   243  		bool
   244  	}{
   245  		{"public", false},
   246  		{"no-cache", true},
   247  		{"public, no-cache, max-age=30", true},
   248  		{"public,no-cache", true},
   249  		{"public,no-cacheX", false},
   250  		{"no-cache, public", true},
   251  		{"Xno-cache, public", false},
   252  		{"max-age=30, no-cache,public", true},
   253  	}
   254  
   255  	for _, c := range testCases {
   256  		ok := isNoCache(c.string)
   257  		utils.AssertEqual(t, c.bool, ok,
   258  			fmt.Sprintf("want %t, got isNoCache(%s)=%t", c.bool, c.string, ok))
   259  	}
   260  }
   261  
   262  // go test -v -run=^$ -bench=Benchmark_Utils_IsNoCache -benchmem -count=4
   263  func Benchmark_Utils_IsNoCache(b *testing.B) {
   264  	var ok bool
   265  	for i := 0; i < b.N; i++ {
   266  		_ = isNoCache("public")
   267  		_ = isNoCache("no-cache")
   268  		_ = isNoCache("public, no-cache, max-age=30")
   269  		_ = isNoCache("public,no-cache")
   270  		_ = isNoCache("no-cache, public")
   271  		ok = isNoCache("max-age=30, no-cache,public")
   272  	}
   273  	utils.AssertEqual(b, true, ok)
   274  }
   275  
   276  // go test -v -run=^$ -bench=Benchmark_SlashRecognition -benchmem -count=4
   277  func Benchmark_SlashRecognition(b *testing.B) {
   278  	search := "wtf/1234"
   279  	var result bool
   280  	b.Run("indexBytes", func(b *testing.B) {
   281  		result = false
   282  		for i := 0; i < b.N; i++ {
   283  			if strings.IndexByte(search, slashDelimiter) != -1 {
   284  				result = true
   285  			}
   286  		}
   287  		utils.AssertEqual(b, true, result)
   288  	})
   289  	b.Run("forEach", func(b *testing.B) {
   290  		result = false
   291  		c := int32(slashDelimiter)
   292  		for i := 0; i < b.N; i++ {
   293  			for _, b := range search {
   294  				if b == c {
   295  					result = true
   296  					break
   297  				}
   298  			}
   299  		}
   300  		utils.AssertEqual(b, true, result)
   301  	})
   302  	b.Run("IndexRune", func(b *testing.B) {
   303  		result = false
   304  		c := int32(slashDelimiter)
   305  		for i := 0; i < b.N; i++ {
   306  			result = IndexRune(search, c)
   307  		}
   308  		utils.AssertEqual(b, true, result)
   309  	})
   310  }