go test -run Test_Bind_Header_WithSetParserDecoder -v
(t *testing.T)
| 787 | |
| 788 | // go test -run Test_Bind_Header_WithSetParserDecoder -v |
| 789 | func Test_Bind_Header_WithSetParserDecoder(t *testing.T) { |
| 790 | type NonRFCTime time.Time |
| 791 | |
| 792 | nonRFCConverter := func(value string) reflect.Value { |
| 793 | if v, err := time.Parse("2006-01-02", value); err == nil { |
| 794 | return reflect.ValueOf(v) |
| 795 | } |
| 796 | return reflect.Value{} |
| 797 | } |
| 798 | |
| 799 | nonRFCTime := binder.ParserType{ |
| 800 | CustomType: NonRFCTime{}, |
| 801 | Converter: nonRFCConverter, |
| 802 | } |
| 803 | |
| 804 | binder.SetParserDecoder(binder.ParserConfig{ |
| 805 | IgnoreUnknownKeys: true, |
| 806 | ParserType: []binder.ParserType{nonRFCTime}, |
| 807 | ZeroEmpty: true, |
| 808 | SetAliasTag: "req", |
| 809 | }) |
| 810 | |
| 811 | app := New() |
| 812 | c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 813 | |
| 814 | type NonRFCTimeInput struct { |
| 815 | Date NonRFCTime `req:"date"` |
| 816 | Title string `req:"title"` |
| 817 | Body string `req:"body"` |
| 818 | } |
| 819 | |
| 820 | c.Request().SetBody([]byte(``)) |
| 821 | c.Request().Header.SetContentType("") |
| 822 | r := new(NonRFCTimeInput) |
| 823 | |
| 824 | c.Request().Header.Add("Date", "2021-04-10") |
| 825 | c.Request().Header.Add("Title", "CustomDateTest") |
| 826 | c.Request().Header.Add("Body", "October") |
| 827 | |
| 828 | require.NoError(t, c.Bind().Header(r)) |
| 829 | require.Equal(t, "CustomDateTest", r.Title) |
| 830 | date := fmt.Sprintf("%v", r.Date) |
| 831 | require.Equal(t, "{0 63753609600 <nil>}", date) |
| 832 | require.Equal(t, "October", r.Body) |
| 833 | |
| 834 | c.Request().Header.Add("Title", "") |
| 835 | r = &NonRFCTimeInput{ |
| 836 | Title: "Existing title", |
| 837 | Body: "Existing Body", |
| 838 | } |
| 839 | require.NoError(t, c.Bind().Header(r)) |
| 840 | require.Empty(t, r.Title) |
| 841 | } |
| 842 | |
| 843 | // go test -run Test_Bind_Header_Schema -v |
| 844 | func Test_Bind_Header_Schema(t *testing.T) { |
nothing calls this directly
no test coverage detected