mirror of
https://github.com/labstack/echo.git
synced 2025-11-06 08:59:21 +02:00
round-trip paramValues without exploding (#1463)
This commit is contained in:
committed by
Vishal Rana
parent
5bf6888444
commit
8d7f05e533
@@ -332,6 +332,7 @@ func TestBindbindData(t *testing.T) {
|
|||||||
|
|
||||||
func TestBindParam(t *testing.T) {
|
func TestBindParam(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
|
*e.maxParam = 2
|
||||||
req := httptest.NewRequest(GET, "/", nil)
|
req := httptest.NewRequest(GET, "/", nil)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := e.NewContext(req, rec)
|
c := e.NewContext(req, rec)
|
||||||
@@ -362,6 +363,7 @@ func TestBindParam(t *testing.T) {
|
|||||||
// Bind something with param and post data payload
|
// Bind something with param and post data payload
|
||||||
body := bytes.NewBufferString(`{ "name": "Jon Snow" }`)
|
body := bytes.NewBufferString(`{ "name": "Jon Snow" }`)
|
||||||
e2 := New()
|
e2 := New()
|
||||||
|
*e2.maxParam = 2
|
||||||
req2 := httptest.NewRequest(POST, "/", body)
|
req2 := httptest.NewRequest(POST, "/", body)
|
||||||
req2.Header.Set(HeaderContentType, MIMEApplicationJSON)
|
req2.Header.Set(HeaderContentType, MIMEApplicationJSON)
|
||||||
|
|
||||||
|
|||||||
@@ -312,7 +312,10 @@ func (c *context) ParamValues() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetParamValues(values ...string) {
|
func (c *context) SetParamValues(values ...string) {
|
||||||
c.pvalues = values
|
// NOTE: Don't just set c.pvalues = values, because it has to have length c.echo.maxParam at all times
|
||||||
|
for i, val := range values {
|
||||||
|
c.pvalues[i] = val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) QueryParam(name string) string {
|
func (c *context) QueryParam(name string) string {
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ func (responseWriterErr) WriteHeader(statusCode int) {
|
|||||||
|
|
||||||
func TestContext(t *testing.T) {
|
func TestContext(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
|
*e.maxParam = 1
|
||||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := e.NewContext(req, rec).(*context)
|
c := e.NewContext(req, rec).(*context)
|
||||||
@@ -471,6 +472,7 @@ func TestContextPath(t *testing.T) {
|
|||||||
|
|
||||||
func TestContextPathParam(t *testing.T) {
|
func TestContextPathParam(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
|
*e.maxParam = 2
|
||||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
c := e.NewContext(req, nil)
|
c := e.NewContext(req, nil)
|
||||||
|
|
||||||
@@ -487,6 +489,26 @@ func TestContextPathParam(t *testing.T) {
|
|||||||
testify.Equal(t, "", c.Param("undefined"))
|
testify.Equal(t, "", c.Param("undefined"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextGetAndSetParam(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
*e.maxParam = 2
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/:foo", nil)
|
||||||
|
c := e.NewContext(req, nil)
|
||||||
|
c.SetParamNames("foo")
|
||||||
|
|
||||||
|
// round-trip param values with modification
|
||||||
|
paramVals := c.ParamValues()
|
||||||
|
testify.EqualValues(t, []string{""}, c.ParamValues())
|
||||||
|
paramVals[0] = "bar"
|
||||||
|
c.SetParamValues(paramVals...)
|
||||||
|
testify.EqualValues(t, []string{"bar"}, c.ParamValues())
|
||||||
|
|
||||||
|
// shouldn't explode during Reset() afterwards!
|
||||||
|
testify.NotPanics(t, func() {
|
||||||
|
c.Reset(nil, nil)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextFormValue(t *testing.T) {
|
func TestContextFormValue(t *testing.T) {
|
||||||
f := make(url.Values)
|
f := make(url.Values)
|
||||||
f.Set("name", "Jon Snow")
|
f.Set("name", "Jon Snow")
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ func TestJWTRace(t *testing.T) {
|
|||||||
|
|
||||||
func TestJWT(t *testing.T) {
|
func TestJWT(t *testing.T) {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
r := e.Router()
|
||||||
|
r.Add("GET", "/:jwt", func(echo.Context) error { return nil })
|
||||||
handler := func(c echo.Context) error {
|
handler := func(c echo.Context) error {
|
||||||
return c.String(http.StatusOK, "test")
|
return c.String(http.StatusOK, "test")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user