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

Function TestPathParamOr

binder_generic_test.go:1392–1444  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1390}
1391
1392func TestPathParamOr(t *testing.T) {
1393 var testCases = []struct {
1394 name string
1395 givenKey string
1396 givenValue string
1397 defaultValue int
1398 expect int
1399 expectErr string
1400 }{
1401 {
1402 name: "ok, param exists",
1403 givenKey: "id",
1404 givenValue: "123",
1405 defaultValue: 999,
1406 expect: 123,
1407 },
1408 {
1409 name: "ok, param missing - returns default",
1410 givenKey: "other",
1411 givenValue: "123",
1412 defaultValue: 999,
1413 expect: 999,
1414 },
1415 {
1416 name: "ok, param exists but empty - returns default",
1417 givenKey: "id",
1418 givenValue: "",
1419 defaultValue: 999,
1420 expect: 999,
1421 },
1422 {
1423 name: "nok, invalid value",
1424 givenKey: "id",
1425 givenValue: "invalid",
1426 defaultValue: 999,
1427 expectErr: "code=400, message=path value, err=failed to parse value",
1428 },
1429 }
1430 for _, tc := range testCases {
1431 t.Run(tc.name, func(t *testing.T) {
1432 c := NewContext(nil, nil)
1433 c.SetPathValues(PathValues{{Name: tc.givenKey, Value: tc.givenValue}})
1434
1435 v, err := PathParamOr[int](c, "id", tc.defaultValue)
1436 if tc.expectErr != "" {
1437 assert.ErrorContains(t, err, tc.expectErr)
1438 } else {
1439 assert.NoError(t, err)
1440 }
1441 assert.Equal(t, tc.expect, v)
1442 })
1443 }
1444}
1445
1446func TestQueryParamOr(t *testing.T) {
1447 var testCases = []struct {

Callers

nothing calls this directly

Calls 2

SetPathValuesMethod · 0.95
NewContextFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…