Skip to content

Commit f12f2bd

Browse files
committed
test(middleware): lock in array-param order preservation
The normalizer's docstring and stripBracketSuffix's pair-by-pair walk promise left-to-right order preservation (load-bearing for sort_by / order_by), but the only coverage used order-insensitive assert.Contains after 02e10b2 dropped the dedicated test. Add exact-match assertions that a mix of plain and bracketed forms re-emits values in send order.
1 parent f8d3a0d commit f12f2bd

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

pkg/routes/middleware/array_param_normalizer_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,40 @@ func TestNormalizeArrayParams(t *testing.T) {
6161
})
6262
}
6363
}
64+
65+
// TestNormalizeArrayParamsPreservesOrder locks in the left-to-right
66+
// ordering the normalizer promises. Sort precedence (sort_by/order_by) is
67+
// load-bearing, so a client mixing plain and bracketed forms of the same
68+
// key must see its values in the exact order it sent them — not whatever a
69+
// map would yield. Asserts the full rewritten query, not just membership.
70+
func TestNormalizeArrayParamsPreservesOrder(t *testing.T) {
71+
cases := []struct {
72+
name string
73+
rawQuery string
74+
wantQuery string
75+
}{
76+
{"plain then bracketed", "foo=a&foo[]=b&foo=c", "foo=a&foo=b&foo=c"},
77+
{
78+
"interleaved percent-encoded",
79+
"sort_by=a&sort_by%5B%5D=b&sort_by=c&sort_by%5B%5D=d",
80+
"sort_by=a&sort_by=b&sort_by=c&sort_by=d",
81+
},
82+
}
83+
84+
for _, tc := range cases {
85+
t.Run(tc.name, func(t *testing.T) {
86+
e := echo.New()
87+
e.Use(middleware.NormalizeArrayParams())
88+
e.GET("/", func(c *echo.Context) error {
89+
return (*c).String(200, (*c).Request().URL.RawQuery)
90+
})
91+
92+
req := httptest.NewRequest("GET", "/?"+tc.rawQuery, nil)
93+
rec := httptest.NewRecorder()
94+
e.ServeHTTP(rec, req)
95+
96+
assert.Equal(t, 200, rec.Code)
97+
assert.Equal(t, tc.wantQuery, rec.Body.String())
98+
})
99+
}
100+
}

0 commit comments

Comments
 (0)