Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to make path params work in unit tests without having to add in request struct?#2714

Unanswered
bhambrah-asbuilt asked this question inQ&A
Discussion options

am using echo v4.12.0 and am facing this issue in unit tests.
doingc.SetParamNames(),c.SetParamValues() in test doesn't makec.Bind() work for path params

as a workaround, i need to add the param in the request struct in the unit test or addc.Param() in the actual function.

reference to setting path params in test:https://echo.labstack.com/docs/testing#setting-path-params

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

request struct:

type TestRequest struct {TestParamOne string `param:"test_param_one"`TestParamTwo string `param:"test_param_two"`TestJsonOne string `json:"test_json_param"`}

if I add unit test like this, it fails because c.Bind() doesn't work as expected for the path params; json params work as expected.

func Test() {      req := TestRequest {            TestJsonOne: "9",      }            var buf bytes.Buffer      err := json.NewEncoder(&buf).Encode(updateReq)      if err != nil {            panic(err)      }            req := httptest.NewRequest(http.MethodPost, "/test/:test_param_one/:test_param_two", &buf)      rec := httptest.NewRecorder()            e := echo.New()      c := e.NewContext(req, rec)      c.SetParamNames("test_param_one", "test_param_two")      c.SetParamValues("9", "9")}
You must be logged in to vote
2 replies
@bhambrah-asbuilt
Comment options

having to update request struct in test as a workaround.
works correctly withoutc.SetParamNames(),c.SetParamValues() , which implies it's not working as expected withc.Bind()

req := TestRequest {      TestParamOne: "9",      TestParamTwo: "9"      TestJsonOne: "9",}
@aldas
Comment options

aldasDec 4, 2024
Maintainer

The problem is that

typeReqstruct {TestParamOnestring`param:"test_param_one"`TestParamTwostring`param:"test_param_two"`TestJsonOnestring`json:"test_json_param"`}payload:=Req{TestJsonOne:"json1",}varbuf bytes.Bufferiferr:=json.NewEncoder(&buf).Encode(payload);err!=nil {t.Fatal(err)}fmt.Printf(buf.String())

will result to body being
{"TestParamOne":"","TestParamTwo":"","test_json_param":"json1"}

and when it comes to binding - json fields will overwrite path fields.

This test would work as it passes json body without these fields as empty to request

package mainimport ("bytes""github.com/labstack/echo/v4""net/http""net/http/httptest""testing")funcTestName(t*testing.T) {typeReqstruct {TestParamOnestring`param:"test_param_one"`TestParamTwostring`param:"test_param_two"`TestJsonOnestring`json:"test_json_param"`}req:=httptest.NewRequest(http.MethodPost,"/test/:test_param_one/:test_param_two",bytes.NewBuffer([]byte(`{"test_json_param":"json1"}`)))req.Header.Set(echo.HeaderContentType,echo.MIMEApplicationJSON)rec:=httptest.NewRecorder()e:=echo.New()c:=e.NewContext(req,rec)c.SetParamNames("test_param_one","test_param_two")c.SetParamValues("param1","param2")handler:=func(c echo.Context)error {payload:=Req{}iferr:=c.Bind(&payload);err!=nil {returnerr}returnc.JSON(http.StatusOK,payload)}err:=handler(c)iferr!=nil {t.Fatal(err)}body:=rec.Body.String()if"{\"TestParamOne\":\"param1\",\"TestParamTwo\":\"param2\",\"test_json_param\":\"json1\"}\n"!=body {t.Errorf("unexpected response, got: %s",body)}}
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@bhambrah-asbuilt@aldas

[8]ページ先頭

©2009-2025 Movatter.jp