(t *testing.T)
| 1390 | } |
| 1391 | |
| 1392 | func 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 | |
| 1446 | func TestQueryParamOr(t *testing.T) { |
| 1447 | var testCases = []struct { |
nothing calls this directly
no test coverage detected
searching dependent graphs…