@@ -3350,3 +3350,134 @@ func TestContextSetCookieData(t *testing.T) {
33503350 assert .Contains (t , setCookie , "SameSite=None" )
33513351 })
33523352}
3353+
3354+ func TestGetMapFromFormData (t * testing.T ) {
3355+ testCases := []struct {
3356+ name string
3357+ data map [string ][]string
3358+ key string
3359+ expected map [string ]string
3360+ found bool
3361+ }{
3362+ {
3363+ name : "Basic bracket notation" ,
3364+ data : map [string ][]string {
3365+ "ids[a]" : {"hi" },
3366+ "ids[b]" : {"3.14" },
3367+ },
3368+ key : "ids" ,
3369+ expected : map [string ]string {
3370+ "a" : "hi" ,
3371+ "b" : "3.14" ,
3372+ },
3373+ found : true ,
3374+ },
3375+ {
3376+ name : "Mixed data with bracket notation" ,
3377+ data : map [string ][]string {
3378+ "ids[a]" : {"hi" },
3379+ "ids[b]" : {"3.14" },
3380+ "names[a]" : {"mike" },
3381+ "names[b]" : {"maria" },
3382+ "other[key]" : {"value" },
3383+ "simple" : {"data" },
3384+ },
3385+ key : "ids" ,
3386+ expected : map [string ]string {
3387+ "a" : "hi" ,
3388+ "b" : "3.14" ,
3389+ },
3390+ found : true ,
3391+ },
3392+ {
3393+ name : "Names key" ,
3394+ data : map [string ][]string {
3395+ "ids[a]" : {"hi" },
3396+ "ids[b]" : {"3.14" },
3397+ "names[a]" : {"mike" },
3398+ "names[b]" : {"maria" },
3399+ "other[key]" : {"value" },
3400+ },
3401+ key : "names" ,
3402+ expected : map [string ]string {
3403+ "a" : "mike" ,
3404+ "b" : "maria" ,
3405+ },
3406+ found : true ,
3407+ },
3408+ {
3409+ name : "Key not found" ,
3410+ data : map [string ][]string {
3411+ "ids[a]" : {"hi" },
3412+ "names[b]" : {"maria" },
3413+ },
3414+ key : "notfound" ,
3415+ expected : map [string ]string {},
3416+ found : false ,
3417+ },
3418+ {
3419+ name : "Empty data" ,
3420+ data : map [string ][]string {},
3421+ key : "ids" ,
3422+ expected : map [string ]string {},
3423+ found : false ,
3424+ },
3425+ {
3426+ name : "Malformed bracket notation" ,
3427+ data : map [string ][]string {
3428+ "ids[a" : {"hi" }, // Missing closing bracket
3429+ "ids]b" : {"3.14" }, // Missing opening bracket
3430+ "idsab" : {"value" }, // No brackets
3431+ },
3432+ key : "ids" ,
3433+ expected : map [string ]string {},
3434+ found : false ,
3435+ },
3436+ {
3437+ name : "Nested bracket notation" ,
3438+ data : map [string ][]string {
3439+ "ids[a][b]" : {"nested" },
3440+ "ids[c]" : {"simple" },
3441+ },
3442+ key : "ids" ,
3443+ expected : map [string ]string {
3444+ "a" : "nested" ,
3445+ "c" : "simple" ,
3446+ },
3447+ found : true ,
3448+ },
3449+ {
3450+ name : "Simple key without brackets" ,
3451+ data : map [string ][]string {
3452+ "simple" : {"data" },
3453+ "ids[a]" : {"hi" },
3454+ },
3455+ key : "simple" ,
3456+ expected : map [string ]string {},
3457+ found : false ,
3458+ },
3459+ {
3460+ name : "Mixed simple and bracket keys" ,
3461+ data : map [string ][]string {
3462+ "simple" : {"data" },
3463+ "ids[a]" : {"hi" },
3464+ "ids[b]" : {"3.14" },
3465+ "other" : {"value" },
3466+ },
3467+ key : "ids" ,
3468+ expected : map [string ]string {
3469+ "a" : "hi" ,
3470+ "b" : "3.14" ,
3471+ },
3472+ found : true ,
3473+ },
3474+ }
3475+
3476+ for _ , tc := range testCases {
3477+ t .Run (tc .name , func (t * testing.T ) {
3478+ result , found := getMapFromFormData (tc .data , tc .key )
3479+ assert .Equal (t , tc .expected , result , "result mismatch" )
3480+ assert .Equal (t , tc .found , found , "found mismatch" )
3481+ })
3482+ }
3483+ }
0 commit comments