mirror of https://github.com/gofiber/fiber.git
💯 cover error statement
parent
8bdb67e8b7
commit
558917f147
9
ctx.go
9
ctx.go
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
11
ctx_test.go
11
ctx_test.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue