💯 cover error statement

pull/611/head
kiyon 2020-07-15 14:22:31 +08:00
parent 8bdb67e8b7
commit 558917f147
2 changed files with 13 additions and 7 deletions

9
ctx.go
View File

@ -15,7 +15,6 @@ import (
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
@ -235,10 +234,10 @@ func (ctx *Ctx) BodyParser(out interface{}) error {
return xml.Unmarshal(ctx.Fasthttp.Request.Body(), out)
case MIMEApplicationForm: // application/x-www-form-urlencoded
schemaDecoder.SetAliasTag("form")
data, err := url.ParseQuery(getString(ctx.Fasthttp.PostBody()))
if err != nil {
return err
}
data := make(map[string][]string)
ctx.Fasthttp.PostArgs().VisitAll(func(key []byte, val []byte) {
data[getString(key)] = append(data[getString(key)], getString(val))
})
return schemaDecoder.Decode(out, data)
}

View File

@ -308,8 +308,15 @@ func Test_Ctx_BodyParser(t *testing.T) {
testDecodeParser(MIMEApplicationForm, "name=john")
testDecodeParser(MIMEMultipartForm+`;boundary="b"`, "--b\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\njohn\r\n--b--")
ctx.Fasthttp.Request.Header.SetContentType("invalid-content-type")
utils.AssertEqual(t, false, ctx.BodyParser(nil) == nil)
testDecodeParserError := func(contentType, body string) {
ctx.Fasthttp.Request.Header.SetContentType(contentType)
ctx.Fasthttp.Request.SetBody([]byte(body))
ctx.Fasthttp.Request.Header.SetContentLength(len(body))
utils.AssertEqual(t, false, ctx.BodyParser(nil) == nil)
}
testDecodeParserError("invalid-content-type", "")
testDecodeParserError(MIMEMultipartForm+`;boundary="b"`, "--b")
type Query struct {
ID int