MCPcopy
hub / github.com/gofiber/fiber / Test_Bind_Query_WithSetParserDecoder

Function Test_Bind_Query_WithSetParserDecoder

bind_test.go:517–566  ·  bind_test.go::Test_Bind_Query_WithSetParserDecoder

go test -run Test_Bind_Query_WithSetParserDecoder -v

(t *testing.T)

Source from the content-addressed store, hash-verified

515
516// go test -run Test_Bind_Query_WithSetParserDecoder -v
517func Test_Bind_Query_WithSetParserDecoder(t *testing.T) {
518 type NonRFCTime time.Time
519
520 nonRFCConverter := func(value string) reflect.Value {
521 if v, err := time.Parse("2006-01-02", value); err == nil {
522 return reflect.ValueOf(v)
523 }
524 return reflect.Value{}
525 }
526
527 nonRFCTime := binder.ParserType{
528 CustomType: NonRFCTime{},
529 Converter: nonRFCConverter,
530 }
531
532 binder.SetParserDecoder(binder.ParserConfig{
533 IgnoreUnknownKeys: true,
534 ParserType: []binder.ParserType{nonRFCTime},
535 ZeroEmpty: true,
536 SetAliasTag: "query",
537 })
538
539 app := New()
540 c := app.AcquireCtx(&fasthttp.RequestCtx{})
541
542 type NonRFCTimeInput struct {
543 Date NonRFCTime `query:"date"`
544 Title string `query:"title"`
545 Body string `query:"body"`
546 }
547
548 c.Request().SetBody([]byte(``))
549 c.Request().Header.SetContentType("")
550 q := new(NonRFCTimeInput)
551
552 c.Request().URI().SetQueryString("date=2021-04-10&title=CustomDateTest&Body=October")
553 require.NoError(t, c.Bind().Query(q))
554 require.Equal(t, "CustomDateTest", q.Title)
555 date := fmt.Sprintf("%v", q.Date)
556 require.Equal(t, "{0 63753609600 <nil>}", date)
557 require.Equal(t, "October", q.Body)
558
559 c.Request().URI().SetQueryString("date=2021-04-10&title&Body=October")
560 q = &NonRFCTimeInput{
561 Title: "Existing title",
562 Body: "Existing Body",
563 }
564 require.NoError(t, c.Bind().Query(q))
565 require.Empty(t, q.Title)
566}
567
568// go test -run Test_Bind_Query_Schema -v
569func Test_Bind_Query_Schema(t *testing.T) {

Callers

nothing calls this directly

Calls 8

SetParserDecoderFunction · 0.92
AcquireCtxMethod · 0.80
URIMethod · 0.80
NewFunction · 0.70
ParseMethod · 0.65
RequestMethod · 0.65
QueryMethod · 0.65
BindMethod · 0.65

Tested by

no test coverage detected