MCPcopy Index your code
hub / github.com/coder/coder / testQueryParams

Function testQueryParams

coderd/httpapi/queryparams_test.go:555–588  ·  view source on GitHub ↗
(t *testing.T, testCases []queryParamTestCase[T], parser *httpapi.QueryParamParser, parse func(vals url.Values, def T, queryParam string) T)

Source from the content-addressed store, hash-verified

553}
554
555func testQueryParams[T any](t *testing.T, testCases []queryParamTestCase[T], parser *httpapi.QueryParamParser, parse func(vals url.Values, def T, queryParam string) T) {
556 v := url.Values{}
557 for _, c := range testCases {
558 if c.NoSet {
559 continue
560 }
561 if len(c.Values) > 0 && c.Value != "" {
562 t.Errorf("test case %q has both value and values, choose one, not both!", c.QueryParam)
563 t.FailNow()
564 }
565 if c.Value != "" {
566 c.Values = append(c.Values, c.Value)
567 }
568
569 for _, value := range c.Values {
570 v.Add(c.QueryParam, value)
571 }
572 }
573
574 for _, c := range testCases {
575 // !! Do not run these in parallel !!
576 t.Run(c.QueryParam, func(t *testing.T) {
577 value := parse(v, c.Default, c.QueryParam)
578 require.Equal(t, c.Expected, value, fmt.Sprintf("param=%q value=%q", c.QueryParam, c.Value))
579 if c.ExpectedErrorContains != "" {
580 errors := parser.Errors
581 require.True(t, len(errors) > 0, "error exist")
582 last := errors[len(errors)-1]
583 require.True(t, last.Field == c.QueryParam, fmt.Sprintf("query param %q did not fail", c.QueryParam))
584 require.Contains(t, last.Detail, c.ExpectedErrorContains, "correct error")
585 }
586 })
587 }
588}

Callers 1

TestParseQueryParamsFunction · 0.85

Calls 7

FailNowMethod · 0.80
AddMethod · 0.65
RunMethod · 0.65
parseFunction · 0.50
ErrorfMethod · 0.45
EqualMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected