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

  1. Create a folder inside your go source path.
  2. Copy the codes below into files handler.go and handler_test.go and store them inside that folder.
  3. cd yourfolder
  4. 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

Most upvoted comments

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

func (c *context) SetParamValues(values ...string) {
    for _, val := range values {
    c.pvalues = append(c.pvalues, val)
  }
}

@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?