MCPcopy Index your code
hub / github.com/labstack/echo / TestBindParam

Function TestBindParam

bind_test.go:567–625  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

565}
566
567func TestBindParam(t *testing.T) {
568 e := New()
569 req := httptest.NewRequest(http.MethodGet, "/", nil)
570 rec := httptest.NewRecorder()
571 c := e.NewContext(req, rec)
572 c.InitializeRoute(
573 &RouteInfo{Path: "/users/:id/:name"},
574 &PathValues{
575 {Name: "id", Value: "1"},
576 {Name: "name", Value: "Jon Snow"},
577 },
578 )
579
580 u := new(user)
581 err := c.Bind(u)
582 if assert.NoError(t, err) {
583 assert.Equal(t, 1, u.ID)
584 assert.Equal(t, "Jon Snow", u.Name)
585 }
586
587 // Second test for the absence of a param
588 c2 := e.NewContext(req, rec)
589 c2.InitializeRoute(
590 &RouteInfo{Path: "/users/:id"},
591 &PathValues{
592 {Name: "id", Value: "1"},
593 },
594 )
595
596 u = new(user)
597 err = c2.Bind(u)
598 if assert.NoError(t, err) {
599 assert.Equal(t, 1, u.ID)
600 assert.Equal(t, "", u.Name)
601 }
602
603 // Bind something with param and post data payload
604 body := bytes.NewBufferString(`{ "name": "Jon Snow" }`)
605 e2 := New()
606 req2 := httptest.NewRequest(http.MethodPost, "/", body)
607 req2.Header.Set(HeaderContentType, MIMEApplicationJSON)
608
609 rec2 := httptest.NewRecorder()
610
611 c3 := e2.NewContext(req2, rec2)
612 c3.InitializeRoute(
613 &RouteInfo{Path: "/users/:id"},
614 &PathValues{
615 {Name: "id", Value: "1"},
616 },
617 )
618
619 u = new(user)
620 err = c3.Bind(u)
621 if assert.NoError(t, err) {
622 assert.Equal(t, 1, u.ID)
623 assert.Equal(t, "Jon Snow", u.Name)
624 }

Callers

nothing calls this directly

Calls 5

InitializeRouteMethod · 0.95
BindMethod · 0.95
NewFunction · 0.85
NewContextMethod · 0.80
SetMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…