echo: SetParamValues function throwing index out of range [0] with length 0 error
Issue Description
Hi,
I’ currently encountering an issue when setting parameter values via SetParamValues function. This is frequently used by my test cases, hence, tests are all failing.
Checklist
- Dependencies installed
- No typos
- Searched existing issues and docs
Expected behaviour
Should be able to set parameter values via SetParamValues function
Actual behaviour
SetParamValues function throws runtime error: index out of range [0] with length 0
Steps to reproduce
- Create a folder inside your go source path.
- Copy the codes below into files handler.go and handler_test.go and store them inside that folder.
- cd yourfolder
- go test -run ^(TestGetUser)$ -v
It will show this test logs afterwards:
=== RUN TestGetUser — FAIL: TestGetUser (0.00s) panic: runtime error: index out of range [0] with length 0 [recovered] panic: runtime error: index out of range [0] with length 0
goroutine 20 [running]: testing.tRunner.func1(0xc00010e100) /usr/local/Cellar/go/1.13.3/libexec/src/testing/testing.go:874 +0x3a3 panic(0x1387d40, 0xc0000c4380) /usr/local/Cellar/go/1.13.3/libexec/src/runtime/panic.go:679 +0x1b2 github.com/labstack/echo.(*context).SetParamValues(0xc0000bd180, 0xc00008d2b0, 0x1, 0x1) /Users/sonnysidramos/go/src/github.com/labstack/echo/context.go:317 +0x93 bitbucket.org/pulseid/echotest.TestGetUser(0xc00010e100) /Users/sonnysidramos/go/src/bitbucket.org/pulseid/echotest/handler_test.go:27 +0x307 testing.tRunner(0xc00010e100, 0x13ce2a0) /usr/local/Cellar/go/1.13.3/libexec/src/testing/testing.go:909 +0xc9 created by testing.(*T).Run /usr/local/Cellar/go/1.13.3/libexec/src/testing/testing.go:960 +0x350 exit status 2
Working code to debug
handler.go
package handler
import (
"net/http"
"github.com/labstack/echo"
)
type (
User struct {
Name string `json:"name" form:"name"`
Email string `json:"email" form:"email"`
}
handler struct {
db map[string]*User
}
)
func (h *handler) getUser(c echo.Context) error {
email := c.Param("email")
user := h.db[email]
if user == nil {
return echo.NewHTTPError(http.StatusNotFound, "user not found")
}
return c.JSON(http.StatusOK, user)
}
handler_test.go:
package handler
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
var (
mockDB = map[string]*User{
"jon@labstack.com": &User{"Jon Snow", "jon@labstack.com"},
}
userJSON = `{"name":"Jon Snow","email":"jon@labstack.com"}`
)
func TestGetUser(t *testing.T) {
// Setup
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/users/:email")
c.SetParamNames("email")
c.SetParamValues("jon@labstack.com")
h := &handler{mockDB}
// Assertions
if assert.NoError(t, h.getUser(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, userJSON, rec.Body.String())
}
}
Version/commit
https://github.com/labstack/echo/commit/8d7f05e5336fa9a05c6ca5a610a0c5140c01bfc3
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 20
- Comments: 24 (6 by maintainers)
Commits related to this issue
- Downgrade echo to v4.0.0 to fix issue with request tests See https://github.com/labstack/echo/issues/1492 — committed to pcriv/mancala by pcriv 4 years ago
- Set maxParam with SetParamNames Fixes #1492 — committed to 178inaba/echo by 178inaba 4 years ago
- Initial test mocking out TestGerUserID Currently not working, i think it's due to a bug described in: https://github.com/labstack/echo/issues/1492 so i will wait for it to be merged. If it isn't i do... — committed to NissesSenap/goapi by NissesSenap 4 years ago
- Set maxParam with SetParamNames (#1535) * Set maxParam with SetParamNames Fixes #1492 * Revert go.mod — committed to labstack/echo by 178inaba 4 years ago
- Upgrade Echo V4 because of an issue with SetParamValues() * https://github.com/labstack/echo/issues/1492 — committed to riehlegroup/product-model-toolkit by deleted user 4 years ago
Review started already for the PR#1535. Thanks @178inaba
I took the change to modify the code where the error is taking place and monkey patch it. I tried to pull request it, but It fails for some reason on other side I cannot find yet.
labstack > echo > context.go
@likejehu I’ve worked around this issue with v4.1.14 and v4.1.15 by downgrading to v4.1.13 for now.
@eddwinpaz Whether the priority of this issue should be raised. Since there is no alternative solution, all unit tests for echo are reported to be wrong. Especially with travis, it is difficult to control the echo version.
I have the same issue.
I found that I set maxParam with the number of path names. https://github.com/labstack/echo/blob/5ddc3a68ba1678147e3779bc80d3b744125d22fc/router.go#L101
So I modified to set maxParam when calling SetParamNames. https://github.com/labstack/echo/pull/1535 Can review this?