Skip to content

Commit

Permalink
fix(binder): update CBOR name and test
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Nov 7, 2024
1 parent b316b36 commit a8192d1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
15 changes: 7 additions & 8 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,39 +914,38 @@ func Test_Bind_Body(t *testing.T) {
testCompressedBody(t, compressedBody, "zstd")
})

testDecodeParser := func(t *testing.T, contentType, body string) {
testDecodeParser := func(t *testing.T, contentType string, body []byte) {
t.Helper()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().Header.SetContentType(contentType)
c.Request().SetBody([]byte(body))
c.Request().SetBody(body)
c.Request().Header.SetContentLength(len(body))
d := new(Demo)
require.NoError(t, c.Bind().Body(d))
require.Equal(t, "john", d.Name)
}

t.Run("JSON", func(t *testing.T) {
testDecodeParser(t, MIMEApplicationJSON, `{"name":"john"}`)
testDecodeParser(t, MIMEApplicationJSON, []byte(`{"name":"john"}`))
})
t.Run("CBOR", func(t *testing.T) {
enc, err := cbor.Marshal(&Demo{Name: "john"})
if err != nil {
t.Error(err)
}
str := string(enc)
testDecodeParser(t, MIMEApplicationCBOR, str)
testDecodeParser(t, MIMEApplicationCBOR, enc)
})

t.Run("XML", func(t *testing.T) {
testDecodeParser(t, MIMEApplicationXML, `<Demo><name>john</name></Demo>`)
testDecodeParser(t, MIMEApplicationXML, []byte(`<Demo><name>john</name></Demo>`))
})

t.Run("Form", func(t *testing.T) {
testDecodeParser(t, MIMEApplicationForm, "name=john")
testDecodeParser(t, MIMEApplicationForm, []byte("name=john"))
})

t.Run("MultipartForm", func(t *testing.T) {
testDecodeParser(t, MIMEMultipartForm+`;boundary="b"`, "--b\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\njohn\r\n--b--")
testDecodeParser(t, MIMEMultipartForm+`;boundary="b"`, []byte("--b\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\njohn\r\n--b--"))
})

testDecodeParserError := func(t *testing.T, contentType, body string) {
Expand Down
2 changes: 1 addition & 1 deletion binder/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
type cborBinding struct{}

func (*cborBinding) Name() string {
return "json"
return "cbor"

Check warning on line 10 in binder/cbor.go

View check run for this annotation

Codecov / codecov/patch

binder/cbor.go#L9-L10

Added lines #L9 - L10 were not covered by tests
}

func (*cborBinding) Bind(body []byte, cborDecoder utils.CBORUnmarshal, out any) error {
Expand Down
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
"time"

"github.com/fxamacker/cbor/v2"
"github.com/gofiber/fiber/v3/log"

"github.com/gofiber/utils/v2"
Expand Down Expand Up @@ -722,6 +723,8 @@ func New() *Client {
jsonMarshal: json.Marshal,
jsonUnmarshal: json.Unmarshal,
xmlMarshal: xml.Marshal,
cborMarshal: cbor.Marshal,
cborUnmarshal: cbor.Unmarshal,
xmlUnmarshal: xml.Unmarshal,
logger: log.DefaultLogger(),
}
Expand Down

0 comments on commit a8192d1

Please sign in to comment.