go test -run Test_Bind_Cookie_WithSetParserDecoder -v
(t *testing.T)
| 2000 | |
| 2001 | // go test -run Test_Bind_Cookie_WithSetParserDecoder -v |
| 2002 | func Test_Bind_Cookie_WithSetParserDecoder(t *testing.T) { |
| 2003 | type NonRFCTime time.Time |
| 2004 | |
| 2005 | nonRFCConverter := func(value string) reflect.Value { |
| 2006 | if v, err := time.Parse("2006-01-02", value); err == nil { |
| 2007 | return reflect.ValueOf(v) |
| 2008 | } |
| 2009 | return reflect.Value{} |
| 2010 | } |
| 2011 | |
| 2012 | nonRFCTime := binder.ParserType{ |
| 2013 | CustomType: NonRFCTime{}, |
| 2014 | Converter: nonRFCConverter, |
| 2015 | } |
| 2016 | |
| 2017 | binder.SetParserDecoder(binder.ParserConfig{ |
| 2018 | IgnoreUnknownKeys: true, |
| 2019 | ParserType: []binder.ParserType{nonRFCTime}, |
| 2020 | ZeroEmpty: true, |
| 2021 | SetAliasTag: "cerez", |
| 2022 | }) |
| 2023 | |
| 2024 | app := New() |
| 2025 | c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 2026 | |
| 2027 | type NonRFCTimeInput struct { |
| 2028 | Date NonRFCTime `cerez:"date"` |
| 2029 | Title string `cerez:"title"` |
| 2030 | Body string `cerez:"body"` |
| 2031 | } |
| 2032 | |
| 2033 | c.Request().SetBody([]byte(``)) |
| 2034 | c.Request().Header.SetContentType("") |
| 2035 | r := new(NonRFCTimeInput) |
| 2036 | |
| 2037 | c.Request().Header.SetCookie("Date", "2021-04-10") |
| 2038 | c.Request().Header.SetCookie("Title", "CustomDateTest") |
| 2039 | c.Request().Header.SetCookie("Body", "October") |
| 2040 | |
| 2041 | require.NoError(t, c.Bind().Cookie(r)) |
| 2042 | require.Equal(t, "CustomDateTest", r.Title) |
| 2043 | date := fmt.Sprintf("%v", r.Date) |
| 2044 | require.Equal(t, "{0 63753609600 <nil>}", date) |
| 2045 | require.Equal(t, "October", r.Body) |
| 2046 | |
| 2047 | c.Request().Header.SetCookie("Title", "") |
| 2048 | r = &NonRFCTimeInput{ |
| 2049 | Title: "Existing title", |
| 2050 | Body: "Existing Body", |
| 2051 | } |
| 2052 | require.NoError(t, c.Bind().Cookie(r)) |
| 2053 | require.Empty(t, r.Title) |
| 2054 | } |
| 2055 | |
| 2056 | // go test -run Test_Bind_Cookie_Schema -v |
| 2057 | func Test_Bind_Cookie_Schema(t *testing.T) { |
nothing calls this directly
no test coverage detected