@@ -303,3 +303,76 @@ func Test_Trim_Edge(t *testing.T) {
303303 })
304304 }
305305}
306+
307+ func Test_AddTrailingSlash_String (t * testing.T ) {
308+ t .Parallel ()
309+
310+ tests := []struct {
311+ in string
312+ want string
313+ }{
314+ {"" , "/" },
315+ {"abc" , "abc/" },
316+ {"abc/" , "abc/" },
317+ {"/" , "/" },
318+ }
319+
320+ for _ , tt := range tests {
321+ require .Equal (t , tt .want , AddTrailingSlash (tt .in ))
322+ }
323+ }
324+
325+ func Test_AddTrailingSlash_Bytes (t * testing.T ) {
326+ t .Parallel ()
327+
328+ tests := []struct {
329+ in []byte
330+ want []byte
331+ }{
332+ {[]byte ("" ), []byte ("/" )},
333+ {[]byte ("abc" ), []byte ("abc/" )},
334+ {[]byte ("abc/" ), []byte ("abc/" )},
335+ {[]byte ("/" ), []byte ("/" )},
336+ }
337+
338+ for _ , tt := range tests {
339+ require .Equal (t , tt .want , AddTrailingSlash (tt .in ))
340+ }
341+ }
342+
343+ func Benchmark_AddTrailingSlash (b * testing.B ) {
344+ tests := []struct {
345+ name string
346+ in any
347+ }{
348+ {"StringSmallNoSlash" , "abc" },
349+ {"StringSmallWithSlash" , "abc/" },
350+ {"StringLargeNoSlash" , string (make ([]byte , 10_000 ))},
351+ {"StringLargeWithSlash" , string (append (make ([]byte , 10_000 ), '/' ))},
352+
353+ {"BytesSmallNoSlash" , []byte ("abc" )},
354+ {"BytesSmallWithSlash" , []byte ("abc/" )},
355+ {"BytesLargeNoSlash" , make ([]byte , 10_000 )},
356+ {"BytesLargeWithSlash" , append (make ([]byte , 10_000 ), '/' )},
357+ }
358+
359+ for _ , tt := range tests {
360+ b .Run (tt .name , func (b * testing.B ) {
361+ switch v := tt .in .(type ) {
362+ case string :
363+ var out string
364+ for i := 0 ; i < b .N ; i ++ {
365+ out = AddTrailingSlash (v )
366+ _ = out
367+ }
368+
369+ case []byte :
370+ var out []byte
371+ for i := 0 ; i < b .N ; i ++ {
372+ out = AddTrailingSlash (v )
373+ _ = out
374+ }
375+ }
376+ })
377+ }
378+ }
0 commit comments