-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechocbor_test.go
More file actions
190 lines (153 loc) · 4.88 KB
/
Copy pathechocbor_test.go
File metadata and controls
190 lines (153 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package echocbor
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/fxamacker/cbor/v2"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/stretchr/testify/assert"
)
// Sample struct for testing
type Sample struct {
Name string `cbor:"name"`
Value int `cbor:"value"`
}
// Test Cbor response
func TestCborResponse(t *testing.T) {
e := echo.New()
e.Use(ContextWrapper)
e.GET("/cbor", func(c echo.Context) error {
return c.(Context).Cbor(http.StatusOK, Sample{Name: "test", Value: 42})
})
req := httptest.NewRequest(http.MethodGet, "/cbor", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "application/cbor", rec.Header().Get(echo.HeaderContentType))
var result Sample
err := cbor.Unmarshal(rec.Body.Bytes(), &result)
assert.NoError(t, err)
assert.Equal(t, "test", result.Name)
assert.Equal(t, 42, result.Value)
}
// Test Cbor binding
func TestCborBinding(t *testing.T) {
e := echo.New()
e.Binder = &Binder{}
e.POST("/cbor", func(c echo.Context) error {
var sample Sample
if err := c.Bind(&sample); err != nil {
return err
}
return c.String(http.StatusOK, sample.Name)
})
sample := Sample{Name: "test", Value: 42}
cborData, err := cbor.Marshal(sample)
assert.NoError(t, err)
req := httptest.NewRequest(http.MethodPost, "/cbor", bytes.NewReader(cborData))
req.Header.Set(echo.HeaderContentType, "application/cbor")
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "test", rec.Body.String())
}
// Test unsupported media type
func TestUnsupportedMediaType(t *testing.T) {
e := echo.New()
e.Binder = &Binder{}
e.POST("/cbor", func(c echo.Context) error {
var sample Sample
if err := c.Bind(&sample); err != nil {
return err
}
return c.String(http.StatusOK, sample.Name)
})
req := httptest.NewRequest(http.MethodPost, "/cbor", bytes.NewReader([]byte(`{"name": "test", "value": 42}`)))
req.Header.Set(echo.HeaderContentType, "application/json") // Unsupported media type
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusUnsupportedMediaType, echo.ErrUnsupportedMediaType.Code)
}
// Test invalid CBOR data
func TestInvalidCborData(t *testing.T) {
e := echo.New()
e.Binder = &Binder{}
e.POST("/cbor", func(c echo.Context) error {
var sample Sample
if err := c.Bind(&sample); err != nil {
return err
}
return c.String(http.StatusOK, sample.Name)
})
req := httptest.NewRequest(http.MethodPost, "/cbor", bytes.NewReader([]byte{0xFF})) // Invalid CBOR
req.Header.Set(echo.HeaderContentType, "application/cbor")
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusUnsupportedMediaType, echo.ErrUnsupportedMediaType.Code)
}
// Test Cbor with empty body
func TestCborEmptyBody(t *testing.T) {
e := echo.New()
e.Binder = &Binder{}
e.POST("/cbor", func(c echo.Context) error {
var sample Sample
if err := c.Bind(&sample); err != nil {
return err
}
return c.String(http.StatusOK, sample.Name)
})
req := httptest.NewRequest(http.MethodPost, "/cbor", bytes.NewReader([]byte{})) // Empty body
req.Header.Set(echo.HeaderContentType, "application/cbor")
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusUnsupportedMediaType, echo.ErrUnsupportedMediaType.Code)
}
// Test Cbor with body limit
func TestCborBodyLimit(t *testing.T) {
e := echo.New()
e.Binder = &Binder{}
e.Use(middleware.BodyLimit("1B"))
e.POST("/cbor", func(c echo.Context) error {
var sample Sample
if err := c.Bind(&sample); err != nil {
return err
}
return c.String(http.StatusOK, sample.Name)
})
largeBody := bytes.Repeat([]byte{0x00}, 2)
req := httptest.NewRequest(http.MethodPost, "/cbor", bytes.NewReader(largeBody))
req.Header.Set(echo.HeaderContentType, "application/cbor")
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusRequestEntityTooLarge, rec.Code)
}
// Benchmark for Cbor encode
func BenchmarkCborEncode(b *testing.B) {
e := echo.New()
data := Sample{Name: "Test", Value: 42}
for i := 0; i < b.N; i++ {
rec := httptest.NewRecorder()
c := &context{e.NewContext(httptest.NewRequest(http.MethodGet, "/", nil), rec)}
if err := c.Cbor(http.StatusOK, data); err != nil {
b.Fatalf("Failed to encode CBOR: %v", err)
}
}
}
// Benchmark for decoding a Cbor request body
func BenchmarkCborDecode(b *testing.B) {
e := echo.New()
data := Sample{Name: "Test", Value: 42}
encodedData, _ := cbor.Marshal(data)
for i := 0; i < b.N; i++ {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(encodedData))
req.Header.Set(echo.HeaderContentType, mimeType)
c := e.NewContext(req, rec)
var decodedData Sample
if err := new(Binder).Bind(&decodedData, c); err != nil {
b.Fatalf("Failed to decode CBOR: %v", err)
}
}
}