-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🔥 feat: Add support for CBOR encoding #3173
base: main
Are you sure you want to change the base?
Changes from all commits
7f7fb38
ea59267
db00468
84c71a5
ba23305
7ee9548
ae8407c
988fd26
b316b36
a8192d1
2b11150
90f6ae8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package binder | ||
|
||
import ( | ||
"github.com/gofiber/utils/v2" | ||
) | ||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type cborBinding struct{} | ||
|
||
func (*cborBinding) Name() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole file is missing tests |
||
return "cbor" | ||
} | ||
|
||
func (*cborBinding) Bind(body []byte, cborDecoder utils.CBORUnmarshal, out any) error { | ||
return cborDecoder(body, out) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package client | |
import ( | ||
"context" | ||
"crypto/tls" | ||
"encoding/hex" | ||
"errors" | ||
"io" | ||
"net" | ||
|
@@ -202,6 +203,33 @@ func Test_Client_Marshal(t *testing.T) { | |
require.Equal(t, errors.New("empty xml"), err) | ||
}) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is missing a test using the default CBOR Marshall and UnMarshall functions as noted by |
||
t.Run("set cbor marshal", func(t *testing.T) { | ||
t.Parallel() | ||
bs, err := hex.DecodeString("f6") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
client := New(). | ||
SetCBORMarshal(func(_ any) ([]byte, error) { | ||
return bs, nil | ||
}) | ||
val, err := client.CBORMarshal()(nil) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, bs, val) | ||
}) | ||
|
||
t.Run("set cbor marshal error", func(t *testing.T) { | ||
t.Parallel() | ||
client := New().SetCBORMarshal(func(_ any) ([]byte, error) { | ||
return nil, errors.New("invalid struct") | ||
}) | ||
|
||
val, err := client.CBORMarshal()(nil) | ||
require.Nil(t, val) | ||
require.Equal(t, errors.New("invalid struct"), err) | ||
}) | ||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
t.Run("set xml unmarshal", func(t *testing.T) { | ||
t.Parallel() | ||
client := New(). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ var ( | |
headerAccept = "Accept" | ||
|
||
applicationJSON = "application/json" | ||
applicationCBOR = "application/cbor" | ||
applicationXML = "application/xml" | ||
applicationForm = "application/x-www-form-urlencoded" | ||
multipartFormData = "multipart/form-data" | ||
|
@@ -129,6 +130,8 @@ func parserRequestHeader(c *Client, req *Request) error { | |
req.RawRequest.Header.Set(headerAccept, applicationJSON) | ||
case xmlBody: | ||
req.RawRequest.Header.SetContentType(applicationXML) | ||
case cborBody: | ||
req.RawRequest.Header.SetContentType(applicationCBOR) | ||
case formBody: | ||
req.RawRequest.Header.SetContentType(applicationForm) | ||
case filesBody: | ||
|
@@ -189,6 +192,12 @@ func parserRequestBody(c *Client, req *Request) error { | |
return err | ||
} | ||
req.RawRequest.SetBody(body) | ||
case cborBody: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing hook test for |
||
body, err := c.cborMarshal(req.body) | ||
if err != nil { | ||
return err | ||
} | ||
req.RawRequest.SetBody(body) | ||
case formBody: | ||
req.RawRequest.SetBody(req.formData.QueryString()) | ||
case filesBody: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ const ( | |
formBody | ||
filesBody | ||
rawBody | ||
cborBody | ||
) | ||
|
||
var ErrClientNil = errors.New("client can not be nil") | ||
|
@@ -337,6 +338,12 @@ func (r *Request) SetXML(v any) *Request { | |
return r | ||
} | ||
|
||
func (r *Request) SetCBOR(v any) *Request { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test using |
||
r.body = v | ||
r.bodyType = cborBody | ||
return r | ||
} | ||
|
||
// SetRawBody method sets body with raw data in request. | ||
func (r *Request) SetRawBody(v []byte) *Request { | ||
r.body = v | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,11 @@ func (r *Response) JSON(v any) error { | |
return r.client.jsonUnmarshal(r.Body(), v) | ||
} | ||
|
||
// CBOR method will unmarshal body to cbor. | ||
func (r *Response) CBOR(v any) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test receiving |
||
return r.client.cborUnmarshal(r.Body(), v) | ||
} | ||
|
||
// XML method will unmarshal body to xml. | ||
func (r *Response) XML(v any) error { | ||
return r.client.xmlUnmarshal(r.Body(), v) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix potential binary data corruption
Converting CBOR binary data to string and back could lead to data corruption. The
testDecodeParser
function should be used with the raw byte slice.📝 Committable suggestion