@@ -574,6 +574,152 @@ func TestContextQueryAndPostForm(t *testing.T) {
574
574
assert .Empty (t , dicts )
575
575
}
576
576
577
+ func TestContextQueryNestedMap (t * testing.T ) {
578
+ var emptyQueryMap map [string ]interface {}
579
+
580
+ tests := map [string ]struct {
581
+ url string
582
+ expectedResult map [string ]interface {}
583
+ exists bool
584
+ }{
585
+ "no searched map key in query string" : {
586
+ url : "?foo=bar" ,
587
+ expectedResult : emptyQueryMap ,
588
+ exists : false ,
589
+ },
590
+ "searched map key is not a map" : {
591
+ url : "?mapkey=value" ,
592
+ expectedResult : emptyQueryMap ,
593
+ exists : false ,
594
+ },
595
+ "searched map key is array" : {
596
+ url : "?mapkey[]=value1&mapkey[]=value2" ,
597
+ expectedResult : emptyQueryMap ,
598
+ exists : false ,
599
+ },
600
+ "searched map key with invalid map access" : {
601
+ url : "?mapkey[key]nested=value" ,
602
+ expectedResult : emptyQueryMap ,
603
+ exists : false ,
604
+ },
605
+ "searched map key with valid and invalid map access" : {
606
+ url : "?mapkey[key]invalidNested=value&mapkey[key][nested]=value1" ,
607
+ expectedResult : map [string ]interface {}{
608
+ "key" : map [string ]interface {}{
609
+ "nested" : "value1" ,
610
+ },
611
+ },
612
+ exists : true ,
613
+ },
614
+ "searched map key after other query params" : {
615
+ url : "?foo=bar&mapkey[key]=value" ,
616
+ expectedResult : map [string ]interface {}{
617
+ "key" : "value" ,
618
+ },
619
+ exists : true ,
620
+ },
621
+ "searched map key before other query params" : {
622
+ url : "?mapkey[key]=value&foo=bar" ,
623
+ expectedResult : map [string ]interface {}{
624
+ "key" : "value" ,
625
+ },
626
+ exists : true ,
627
+ },
628
+ "single key in searched map key" : {
629
+ url : "?mapkey[key]=value" ,
630
+ expectedResult : map [string ]interface {}{
631
+ "key" : "value" ,
632
+ },
633
+ exists : true ,
634
+ },
635
+ "multiple keys in searched map key" : {
636
+ url : "?mapkey[key1]=value1&mapkey[key2]=value2&mapkey[key3]=value3" ,
637
+ expectedResult : map [string ]interface {}{
638
+ "key1" : "value1" ,
639
+ "key2" : "value2" ,
640
+ "key3" : "value3" ,
641
+ },
642
+ exists : true ,
643
+ },
644
+ "nested key in searched map key" : {
645
+ url : "?mapkey[foo][nested]=value1" ,
646
+ expectedResult : map [string ]interface {}{
647
+ "foo" : map [string ]interface {}{
648
+ "nested" : "value1" ,
649
+ },
650
+ },
651
+ exists : true ,
652
+ },
653
+ "multiple nested keys in single key of searched map key" : {
654
+ url : "?mapkey[foo][nested1]=value1&mapkey[foo][nested2]=value2" ,
655
+ expectedResult : map [string ]interface {}{
656
+ "foo" : map [string ]interface {}{
657
+ "nested1" : "value1" ,
658
+ "nested2" : "value2" ,
659
+ },
660
+ },
661
+ exists : true ,
662
+ },
663
+ "multiple keys with nested keys of searched map key" : {
664
+ url : "?mapkey[key1][nested]=value1&mapkey[key2][nested]=value2" ,
665
+ expectedResult : map [string ]interface {}{
666
+ "key1" : map [string ]interface {}{
667
+ "nested" : "value1" ,
668
+ },
669
+ "key2" : map [string ]interface {}{
670
+ "nested" : "value2" ,
671
+ },
672
+ },
673
+ exists : true ,
674
+ },
675
+ "multiple levels of nesting in searched map key" : {
676
+ url : "?mapkey[key][nested][moreNested]=value1" ,
677
+ expectedResult : map [string ]interface {}{
678
+ "key" : map [string ]interface {}{
679
+ "nested" : map [string ]interface {}{
680
+ "moreNested" : "value1" ,
681
+ },
682
+ },
683
+ },
684
+ exists : true ,
685
+ },
686
+ "query keys similar to searched map key" : {
687
+ url : "?mapkey[key]=value&mapkeys[key1]=value1&mapkey1=foo" ,
688
+ expectedResult : map [string ]interface {}{
689
+ "key" : "value" ,
690
+ },
691
+ exists : true ,
692
+ },
693
+ }
694
+ for name , test := range tests {
695
+ t .Run ("getQueryMap: " + name , func (t * testing.T ) {
696
+ u , err := url .Parse (test .url )
697
+ require .NoError (t , err )
698
+
699
+ c := Context {
700
+ Request : & http.Request {
701
+ URL : u ,
702
+ },
703
+ }
704
+ dicts , exists := c .GetQueryNestedMap ("mapkey" )
705
+ require .Equal (t , test .expectedResult , dicts )
706
+ require .Equal (t , test .exists , exists )
707
+ })
708
+ t .Run ("queryMap: " + name , func (t * testing.T ) {
709
+ u , err := url .Parse (test .url )
710
+ require .NoError (t , err )
711
+
712
+ c := Context {
713
+ Request : & http.Request {
714
+ URL : u ,
715
+ },
716
+ }
717
+ dicts := c .QueryNestedMap ("mapkey" )
718
+ require .Equal (t , test .expectedResult , dicts )
719
+ })
720
+ }
721
+ }
722
+
577
723
func TestContextPostFormMultipart (t * testing.T ) {
578
724
c , _ := CreateTestContext (httptest .NewRecorder ())
579
725
c .Request = createMultipartRequest ()
0 commit comments