Skip to content

Commit

Permalink
Add test case for QueryParser for SetParserDecoder (#1562)
Browse files Browse the repository at this point in the history
* 🔥 add function to overide form decoder setting

🔥 Feature : SetBodyParserDecoder map all option to form decoder, use with BodyParserConfig, BodyParserType

🚨 Test : Test_Ctx_BodyParser_WithSetBodyParserDecoder

* 🔥 Use decoder builder function with default setting on init decoderPool

* ♻️ rename SetBodyParserDecoder to SetParserDecoder

BodyParserType > ParserType
bodyParserConfig > parserConfig
BodyParserConfig > ParserConfig

* Add Test case for QueryParser WithSetParserDecoder

* Update for Test_Ctx_QueryParser_WithSetParserDecoder

* Update  Test_Ctx_QueryParser_WithSetParserDecoder  remove t.Parallel()

debug unit test  - may close this pull request for cleaner commit

* 🧹 Clean Up
  • Loading branch information
rockcreation7 committed Oct 5, 2021
1 parent a9b66b3 commit c098ada
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions ctx_test.go
Expand Up @@ -410,21 +410,18 @@ func Test_Ctx_BodyParser_WithSetParserDecoder(t *testing.T) {
return reflect.Value{}
}


customTime := ParserType{
Customtype: CustomTime{},
Converter: timeConverter,
}


SetParserDecoder(ParserConfig{
IgnoreUnknownKeys: true,
ParserType: []ParserType{customTime},
ZeroEmpty: true,
SetAliasTag: "form",
})

t.Parallel()
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
Expand All @@ -445,7 +442,6 @@ func Test_Ctx_BodyParser_WithSetParserDecoder(t *testing.T) {
}
utils.AssertEqual(t, nil, c.BodyParser(&d))
date := fmt.Sprintf("%v", d.Date)
fmt.Println(date, d.Title, d.Body)
utils.AssertEqual(t, "{0 63743587200 <nil>}", date)
utils.AssertEqual(t, "", d.Title)
utils.AssertEqual(t, "New Body", d.Body)
Expand Down Expand Up @@ -2384,6 +2380,60 @@ func Test_Ctx_QueryParser(t *testing.T) {
utils.AssertEqual(t, "name is empty", c.QueryParser(rq).Error())
}

// go test -run Test_Ctx_QueryParser_WithSetParserDecoder -v
func Test_Ctx_QueryParser_WithSetParserDecoder(t *testing.T) {
type NonRFCTime time.Time

var NonRFCConverter = func(value string) reflect.Value {
if v, err := time.Parse("2006-01-02", value); err == nil {
return reflect.ValueOf(v)
}
return reflect.Value{}
}

nonRFCTime := ParserType{
Customtype: NonRFCTime{},
Converter: NonRFCConverter,
}

SetParserDecoder(ParserConfig{
IgnoreUnknownKeys: true,
ParserType: []ParserType{nonRFCTime},
ZeroEmpty: true,
SetAliasTag: "query",
})

app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)

type NonRFCTimeInput struct {
Date NonRFCTime `query:"date"`
Title string `query:"title"`
Body string `query:"body"`
}

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
q := new(NonRFCTimeInput)

c.Request().URI().SetQueryString("date=2021-04-10&title=CustomDateTest&Body=October")
utils.AssertEqual(t, nil, c.QueryParser(q))
fmt.Println(q.Date, "q.Date")
utils.AssertEqual(t, "CustomDateTest", q.Title)
date := fmt.Sprintf("%v", q.Date)
utils.AssertEqual(t, "{0 63753609600 <nil>}", date)
utils.AssertEqual(t, "October", q.Body)

c.Request().URI().SetQueryString("date=2021-04-10&title&Body=October")
q = &NonRFCTimeInput{
Title: "Existing title",
Body: "Existing Body",
}
utils.AssertEqual(t, nil, c.QueryParser(q))
utils.AssertEqual(t, "", q.Title)
}

// go test -run Test_Ctx_QueryParser_Schema -v
func Test_Ctx_QueryParser_Schema(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit c098ada

Please sign in to comment.