@@ -12,6 +12,7 @@ import (
1212 "testing"
1313 "time"
1414
15+ "github.com/fxamacker/cbor/v2"
1516 "github.com/gofiber/fiber/v3/binder"
1617 "github.com/stretchr/testify/require"
1718 "github.com/valyala/fasthttp"
@@ -157,7 +158,7 @@ func Test_Bind_Query_WithSetParserDecoder(t *testing.T) {
157158 }
158159
159160 nonRFCTime := binder.ParserType {
160- Customtype : NonRFCTime {},
161+ CustomType : NonRFCTime {},
161162 Converter : nonRFCConverter ,
162163 }
163164
@@ -411,7 +412,7 @@ func Test_Bind_Header_WithSetParserDecoder(t *testing.T) {
411412 }
412413
413414 nonRFCTime := binder.ParserType {
414- Customtype : NonRFCTime {},
415+ CustomType : NonRFCTime {},
415416 Converter : nonRFCConverter ,
416417 }
417418
@@ -922,31 +923,48 @@ func Test_Bind_Body(t *testing.T) {
922923 testCompressedBody (t , compressedBody , "zstd" )
923924 })
924925
925- testDecodeParser := func (t * testing.T , contentType , body string ) {
926+ testDecodeParser := func (t * testing.T , contentType string , body [] byte ) {
926927 t .Helper ()
927928 c := app .AcquireCtx (& fasthttp.RequestCtx {})
928929 c .Request ().Header .SetContentType (contentType )
929- c .Request ().SetBody ([] byte ( body ) )
930+ c .Request ().SetBody (body )
930931 c .Request ().Header .SetContentLength (len (body ))
931932 d := new (Demo )
932933 require .NoError (t , c .Bind ().Body (d ))
933934 require .Equal (t , "john" , d .Name )
934935 }
935936
936937 t .Run ("JSON" , func (t * testing.T ) {
937- testDecodeParser (t , MIMEApplicationJSON , `{"name":"john"}` )
938+ testDecodeParser (t , MIMEApplicationJSON , []byte (`{"name":"john"}` ))
939+ })
940+ t .Run ("CBOR" , func (t * testing.T ) {
941+ enc , err := cbor .Marshal (& Demo {Name : "john" })
942+ if err != nil {
943+ t .Error (err )
944+ }
945+ testDecodeParser (t , MIMEApplicationCBOR , enc )
946+
947+ // Test invalid CBOR data
948+ t .Run ("Invalid" , func (t * testing.T ) {
949+ invalidData := []byte {0xFF , 0xFF } // Invalid CBOR data
950+ c := app .AcquireCtx (& fasthttp.RequestCtx {})
951+ c .Request ().Header .SetContentType (MIMEApplicationCBOR )
952+ c .Request ().SetBody (invalidData )
953+ d := new (Demo )
954+ require .Error (t , c .Bind ().Body (d ))
955+ })
938956 })
939957
940958 t .Run ("XML" , func (t * testing.T ) {
941- testDecodeParser (t , MIMEApplicationXML , `<Demo><name>john</name></Demo>` )
959+ testDecodeParser (t , MIMEApplicationXML , [] byte ( `<Demo><name>john</name></Demo>` ) )
942960 })
943961
944962 t .Run ("Form" , func (t * testing.T ) {
945- testDecodeParser (t , MIMEApplicationForm , "name=john" )
963+ testDecodeParser (t , MIMEApplicationForm , [] byte ( "name=john" ) )
946964 })
947965
948966 t .Run ("MultipartForm" , func (t * testing.T ) {
949- testDecodeParser (t , MIMEMultipartForm + `;boundary="b"` , "--b\r \n Content-Disposition: form-data; name=\" name\" \r \n \r \n john\r \n --b--" )
967+ testDecodeParser (t , MIMEMultipartForm + `;boundary="b"` , [] byte ( "--b\r \n Content-Disposition: form-data; name=\" name\" \r \n \r \n john\r \n --b--" ) )
950968 })
951969
952970 testDecodeParserError := func (t * testing.T , contentType , body string ) {
@@ -1009,7 +1027,7 @@ func Test_Bind_Body_WithSetParserDecoder(t *testing.T) {
10091027 }
10101028
10111029 customTime := binder.ParserType {
1012- Customtype : CustomTime {},
1030+ CustomType : CustomTime {},
10131031 Converter : timeConverter ,
10141032 }
10151033
@@ -1100,6 +1118,35 @@ func Benchmark_Bind_Body_XML(b *testing.B) {
11001118 require .Equal (b , "john" , d .Name )
11011119}
11021120
1121+ // go test -v -run=^$ -bench=Benchmark_Bind_Body_CBOR -benchmem -count=4
1122+ func Benchmark_Bind_Body_CBOR (b * testing.B ) {
1123+ var err error
1124+
1125+ app := New ()
1126+ c := app .AcquireCtx (& fasthttp.RequestCtx {})
1127+
1128+ type Demo struct {
1129+ Name string `json:"name"`
1130+ }
1131+ body , err := cbor .Marshal (& Demo {Name : "john" })
1132+ if err != nil {
1133+ b .Error (err )
1134+ }
1135+ c .Request ().SetBody (body )
1136+ c .Request ().Header .SetContentType (MIMEApplicationCBOR )
1137+ c .Request ().Header .SetContentLength (len (body ))
1138+ d := new (Demo )
1139+
1140+ b .ReportAllocs ()
1141+ b .ResetTimer ()
1142+
1143+ for n := 0 ; n < b .N ; n ++ {
1144+ err = c .Bind ().Body (d )
1145+ }
1146+ require .NoError (b , err )
1147+ require .Equal (b , "john" , d .Name )
1148+ }
1149+
11031150// go test -v -run=^$ -bench=Benchmark_Bind_Body_Form -benchmem -count=4
11041151func Benchmark_Bind_Body_Form (b * testing.B ) {
11051152 var err error
@@ -1404,7 +1451,7 @@ func Test_Bind_Cookie_WithSetParserDecoder(t *testing.T) {
14041451 }
14051452
14061453 nonRFCTime := binder.ParserType {
1407- Customtype : NonRFCTime {},
1454+ CustomType : NonRFCTime {},
14081455 Converter : nonRFCConverter ,
14091456 }
14101457
@@ -1720,8 +1767,12 @@ func Test_Bind_RepeatParserWithSameStruct(t *testing.T) {
17201767 require .Equal (t , "body_param" , r .BodyParam )
17211768 }
17221769
1770+ cb , err := cbor .Marshal (& Request {BodyParam : "body_param" })
1771+ require .NoError (t , err , "Failed to marshal CBOR data" )
1772+
17231773 testDecodeParser (MIMEApplicationJSON , `{"body_param":"body_param"}` )
17241774 testDecodeParser (MIMEApplicationXML , `<Demo><body_param>body_param</body_param></Demo>` )
1775+ testDecodeParser (MIMEApplicationCBOR , string (cb ))
17251776 testDecodeParser (MIMEApplicationForm , "body_param=body_param" )
17261777 testDecodeParser (MIMEMultipartForm + `;boundary="b"` , "--b\r \n Content-Disposition: form-data; name=\" body_param\" \r \n \r \n body_param\r \n --b--" )
17271778}
0 commit comments