Skip to content

Commit f9aa23a

Browse files
Pavel Ivanovkisielk
authored andcommitted
Add tests for support for queries in URL reversing.
1 parent 9c9af15 commit f9aa23a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

mux_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type routeTest struct {
3636
scheme string // the expected scheme of the built URL
3737
host string // the expected host of the built URL
3838
path string // the expected path of the built URL
39+
query string // the expected query string to match
3940
pathTemplate string // the expected path template of the route
4041
hostTemplate string // the expected host template of the route
4142
methods []string // the expected route methods
@@ -744,6 +745,7 @@ func TestQueries(t *testing.T) {
744745
vars: map[string]string{},
745746
host: "",
746747
path: "",
748+
query: "foo=bar&baz=ding",
747749
shouldMatch: true,
748750
},
749751
{
@@ -753,6 +755,7 @@ func TestQueries(t *testing.T) {
753755
vars: map[string]string{},
754756
host: "",
755757
path: "",
758+
query: "foo=bar&baz=ding",
756759
pathTemplate: `/api`,
757760
hostTemplate: `www.example.com`,
758761
shouldMatch: true,
@@ -764,6 +767,7 @@ func TestQueries(t *testing.T) {
764767
vars: map[string]string{},
765768
host: "",
766769
path: "",
770+
query: "foo=bar&baz=ding",
767771
pathTemplate: `/api`,
768772
hostTemplate: `www.example.com`,
769773
shouldMatch: true,
@@ -784,6 +788,7 @@ func TestQueries(t *testing.T) {
784788
vars: map[string]string{"v1": "bar"},
785789
host: "",
786790
path: "",
791+
query: "foo=bar",
787792
shouldMatch: true,
788793
},
789794
{
@@ -793,6 +798,7 @@ func TestQueries(t *testing.T) {
793798
vars: map[string]string{"v1": "bar", "v2": "ding"},
794799
host: "",
795800
path: "",
801+
query: "foo=bar&baz=ding",
796802
shouldMatch: true,
797803
},
798804
{
@@ -802,6 +808,7 @@ func TestQueries(t *testing.T) {
802808
vars: map[string]string{"v1": "10"},
803809
host: "",
804810
path: "",
811+
query: "foo=10",
805812
shouldMatch: true,
806813
},
807814
{
@@ -820,6 +827,7 @@ func TestQueries(t *testing.T) {
820827
vars: map[string]string{"v1": "1"},
821828
host: "",
822829
path: "",
830+
query: "foo=1",
823831
shouldMatch: true,
824832
},
825833
{
@@ -829,6 +837,7 @@ func TestQueries(t *testing.T) {
829837
vars: map[string]string{"v1": "1"},
830838
host: "",
831839
path: "",
840+
query: "foo=1",
832841
shouldMatch: true,
833842
},
834843
{
@@ -847,6 +856,7 @@ func TestQueries(t *testing.T) {
847856
vars: map[string]string{"v1": "1a"},
848857
host: "",
849858
path: "",
859+
query: "foo=1a",
850860
shouldMatch: true,
851861
},
852862
{
@@ -865,6 +875,7 @@ func TestQueries(t *testing.T) {
865875
vars: map[string]string{"v-1": "bar"},
866876
host: "",
867877
path: "",
878+
query: "foo=bar",
868879
shouldMatch: true,
869880
},
870881
{
@@ -874,6 +885,7 @@ func TestQueries(t *testing.T) {
874885
vars: map[string]string{"v-1": "bar", "v-2": "ding"},
875886
host: "",
876887
path: "",
888+
query: "foo=bar&baz=ding",
877889
shouldMatch: true,
878890
},
879891
{
@@ -883,6 +895,7 @@ func TestQueries(t *testing.T) {
883895
vars: map[string]string{"v-1": "10"},
884896
host: "",
885897
path: "",
898+
query: "foo=10",
886899
shouldMatch: true,
887900
},
888901
{
@@ -892,6 +905,7 @@ func TestQueries(t *testing.T) {
892905
vars: map[string]string{"v-1": "1a"},
893906
host: "",
894907
path: "",
908+
query: "foo=1a",
895909
shouldMatch: true,
896910
},
897911
{
@@ -901,6 +915,7 @@ func TestQueries(t *testing.T) {
901915
vars: map[string]string{},
902916
host: "",
903917
path: "",
918+
query: "foo=",
904919
shouldMatch: true,
905920
},
906921
{
@@ -919,6 +934,7 @@ func TestQueries(t *testing.T) {
919934
vars: map[string]string{},
920935
host: "",
921936
path: "",
937+
query: "foo=",
922938
shouldMatch: true,
923939
},
924940
{
@@ -946,6 +962,7 @@ func TestQueries(t *testing.T) {
946962
vars: map[string]string{"foo": ""},
947963
host: "",
948964
path: "",
965+
query: "foo=",
949966
shouldMatch: true,
950967
},
951968
{
@@ -1537,6 +1554,7 @@ func testRoute(t *testing.T, test routeTest) {
15371554
route := test.route
15381555
vars := test.vars
15391556
shouldMatch := test.shouldMatch
1557+
query := test.query
15401558
shouldRedirect := test.shouldRedirect
15411559
uri := url.URL{
15421560
Scheme: test.scheme,
@@ -1606,6 +1624,13 @@ func testRoute(t *testing.T, test routeTest) {
16061624
return
16071625
}
16081626
}
1627+
if query != "" {
1628+
u, _ := route.URL(mapToPairs(match.Vars)...)
1629+
if query != u.RawQuery {
1630+
t.Errorf("(%v) URL query not equal: expected %v, got %v", test.title, query, u.RawQuery)
1631+
return
1632+
}
1633+
}
16091634
if shouldRedirect && match.Handler == nil {
16101635
t.Errorf("(%v) Did not redirect", test.title)
16111636
return

0 commit comments

Comments
 (0)