Skip to content

Commit 8d7f05e

Browse files
dlowevishr
authored andcommitted
round-trip paramValues without exploding (#1463)
1 parent 5bf6888 commit 8d7f05e

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

bind_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func TestBindbindData(t *testing.T) {
332332

333333
func TestBindParam(t *testing.T) {
334334
e := New()
335+
*e.maxParam = 2
335336
req := httptest.NewRequest(GET, "/", nil)
336337
rec := httptest.NewRecorder()
337338
c := e.NewContext(req, rec)
@@ -362,6 +363,7 @@ func TestBindParam(t *testing.T) {
362363
// Bind something with param and post data payload
363364
body := bytes.NewBufferString(`{ "name": "Jon Snow" }`)
364365
e2 := New()
366+
*e2.maxParam = 2
365367
req2 := httptest.NewRequest(POST, "/", body)
366368
req2.Header.Set(HeaderContentType, MIMEApplicationJSON)
367369

context.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ func (c *context) ParamValues() []string {
312312
}
313313

314314
func (c *context) SetParamValues(values ...string) {
315-
c.pvalues = values
315+
// NOTE: Don't just set c.pvalues = values, because it has to have length c.echo.maxParam at all times
316+
for i, val := range values {
317+
c.pvalues[i] = val
318+
}
316319
}
317320

318321
func (c *context) QueryParam(name string) string {

context_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (responseWriterErr) WriteHeader(statusCode int) {
9393

9494
func TestContext(t *testing.T) {
9595
e := New()
96+
*e.maxParam = 1
9697
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
9798
rec := httptest.NewRecorder()
9899
c := e.NewContext(req, rec).(*context)
@@ -471,6 +472,7 @@ func TestContextPath(t *testing.T) {
471472

472473
func TestContextPathParam(t *testing.T) {
473474
e := New()
475+
*e.maxParam = 2
474476
req := httptest.NewRequest(http.MethodGet, "/", nil)
475477
c := e.NewContext(req, nil)
476478

@@ -487,6 +489,26 @@ func TestContextPathParam(t *testing.T) {
487489
testify.Equal(t, "", c.Param("undefined"))
488490
}
489491

492+
func TestContextGetAndSetParam(t *testing.T) {
493+
e := New()
494+
*e.maxParam = 2
495+
req := httptest.NewRequest(http.MethodGet, "/:foo", nil)
496+
c := e.NewContext(req, nil)
497+
c.SetParamNames("foo")
498+
499+
// round-trip param values with modification
500+
paramVals := c.ParamValues()
501+
testify.EqualValues(t, []string{""}, c.ParamValues())
502+
paramVals[0] = "bar"
503+
c.SetParamValues(paramVals...)
504+
testify.EqualValues(t, []string{"bar"}, c.ParamValues())
505+
506+
// shouldn't explode during Reset() afterwards!
507+
testify.NotPanics(t, func() {
508+
c.Reset(nil, nil)
509+
})
510+
}
511+
490512
func TestContextFormValue(t *testing.T) {
491513
f := make(url.Values)
492514
f.Set("name", "Jon Snow")

middleware/jwt_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func TestJWTRace(t *testing.T) {
6060

6161
func TestJWT(t *testing.T) {
6262
e := echo.New()
63+
r := e.Router()
64+
r.Add("GET", "/:jwt", func(echo.Context) error { return nil })
6365
handler := func(c echo.Context) error {
6466
return c.String(http.StatusOK, "test")
6567
}

0 commit comments

Comments
 (0)