@@ -4,8 +4,10 @@ import (
44 "errors"
55 "mime/multipart"
66 "reflect"
7+ "strconv"
78 "testing"
89
10+ "github.com/gofiber/schema"
911 "github.com/stretchr/testify/require"
1012)
1113
@@ -344,6 +346,130 @@ func Test_formatBindData_ErrorCases(t *testing.T) {
344346 })
345347}
346348
349+ func Test_decoderBuilder (t * testing.T ) {
350+ t .Parallel ()
351+ type customInt int
352+ conv := func (s string ) reflect.Value {
353+ i , err := strconv .Atoi (s )
354+ if err != nil {
355+ panic (err )
356+ }
357+ return reflect .ValueOf (customInt (i ))
358+ }
359+ parserConfig := ParserConfig {
360+ SetAliasTag : "custom" ,
361+ ParserType : []ParserType {{
362+ CustomType : customInt (0 ),
363+ Converter : conv ,
364+ }},
365+ IgnoreUnknownKeys : false ,
366+ ZeroEmpty : false ,
367+ }
368+ decAny := decoderBuilder (parserConfig )
369+ dec , ok := decAny .(* schema.Decoder )
370+ require .True (t , ok )
371+ var out struct {
372+ X customInt `custom:"x"`
373+ }
374+ err := dec .Decode (& out , map [string ][]string {"x" : {"7" }})
375+ require .NoError (t , err )
376+ require .Equal (t , customInt (7 ), out .X )
377+ }
378+
379+ func Test_parseToMap_Extended (t * testing.T ) {
380+ t .Parallel ()
381+ data := map [string ][]string {
382+ "empty" : {},
383+ "key1" : {"value1" },
384+ }
385+
386+ m := make (map [string ]string )
387+ err := parseToMap (m , data )
388+ require .NoError (t , err )
389+ require .Equal (t , "" , m ["empty" ])
390+
391+ m2 := make (map [string ][]int )
392+ err = parseToMap (m2 , data )
393+ require .ErrorIs (t , err , ErrMapNotConvertible )
394+
395+ m3 := make (map [string ]int )
396+ err = parseToMap (m3 , data )
397+ require .NoError (t , err )
398+ }
399+
400+ func Test_decoderPoolMapInit (t * testing.T ) {
401+ for _ , tag := range tags {
402+ decAny := decoderPoolMap [tag ].Get ()
403+ dec , ok := decAny .(* schema.Decoder )
404+ require .True (t , ok )
405+ require .NotNil (t , dec )
406+ }
407+ }
408+
409+ func Test_getFieldCache (t * testing.T ) {
410+ t .Parallel ()
411+ require .NotNil (t , getFieldCache ("header" ))
412+ require .NotNil (t , getFieldCache ("respHeader" ))
413+ require .NotNil (t , getFieldCache ("cookie" ))
414+ require .NotNil (t , getFieldCache ("form" ))
415+ require .NotNil (t , getFieldCache ("uri" ))
416+ require .NotNil (t , getFieldCache ("query" ))
417+ require .Panics (t , func () { getFieldCache ("unknown" ) })
418+ }
419+
420+ func Test_EqualFieldType_Map (t * testing.T ) {
421+ t .Parallel ()
422+ m := map [string ]int {}
423+ require .True (t , equalFieldType (& m , reflect .Int , "any" , "query" ))
424+ }
425+
426+ func Test_equalFieldType_CacheTypeMismatch (t * testing.T ) {
427+ type Sample struct {
428+ Field string `query:"field"`
429+ }
430+ cache := getFieldCache ("query" )
431+ typ := reflect .TypeOf (Sample {})
432+ cache .Store (typ , 1 )
433+ defer cache .Delete (typ )
434+ var s Sample
435+ require .False (t , equalFieldType (& s , reflect .String , "field" , "query" ))
436+ }
437+
438+ func Test_buildFieldInfo_Unexported (t * testing.T ) {
439+ t .Parallel ()
440+ type nested struct {
441+ export int
442+ Exported int
443+ }
444+ _ = nested {export : 0 }
445+ type outer struct {
446+ Name string
447+ Nested nested
448+ }
449+ info := buildFieldInfo (reflect .TypeOf (outer {}), "query" )
450+ require .Contains (t , info .names , "name" )
451+ _ , ok := info .nestedKinds [reflect .Int ]
452+ require .True (t , ok )
453+ }
454+
455+ func Test_formatBindData_BracketNotationSuccess (t * testing.T ) {
456+ t .Parallel ()
457+ out := struct {}{}
458+ data := make (map [string ][]string )
459+ err := formatBindData ("query" , out , data , "user[name]" , "john" , false , true )
460+ require .NoError (t , err )
461+ require .Equal (t , "john" , data ["user.name" ][0 ])
462+ }
463+
464+ func Test_formatBindData_FileHeaderTypeMismatch (t * testing.T ) {
465+ t .Parallel ()
466+ out := struct {}{}
467+ data := map [string ][]int {}
468+ files := []* multipart.FileHeader {{Filename : "file1.txt" }}
469+ err := formatBindData ("query" , out , data , "file" , files , false , false )
470+ require .EqualError (t , err , "unsupported value type: []*multipart.FileHeader" )
471+ }
472+
347473func Benchmark_equalFieldType (b * testing.B ) {
348474 type Nested struct {
349475 Name string `query:"name"`
0 commit comments