go test -run Test_Bind_Body_Compression
(t *testing.T)
| 1268 | |
| 1269 | // go test -run Test_Bind_Body_Compression |
| 1270 | func Test_Bind_Body(t *testing.T) { |
| 1271 | t.Parallel() |
| 1272 | app := New(Config{ |
| 1273 | MsgPackEncoder: msgpack.Marshal, |
| 1274 | MsgPackDecoder: msgpack.Unmarshal, |
| 1275 | CBOREncoder: cbor.Marshal, |
| 1276 | CBORDecoder: cbor.Unmarshal, |
| 1277 | }) |
| 1278 | reqBody := []byte(`{"name":"john"}`) |
| 1279 | |
| 1280 | type Demo struct { |
| 1281 | Name string `json:"name" xml:"name" form:"name" query:"name" msgpack:"name"` |
| 1282 | Names []string `json:"names" xml:"names" form:"names" query:"names" msgpack:"names"` |
| 1283 | } |
| 1284 | |
| 1285 | // Helper function to test compressed bodies |
| 1286 | testCompressedBody := func(t *testing.T, compressedBody []byte, encoding string) { |
| 1287 | t.Helper() |
| 1288 | c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 1289 | c.Request().Header.SetContentType(MIMEApplicationJSON) |
| 1290 | c.Request().Header.Set(fasthttp.HeaderContentEncoding, encoding) |
| 1291 | c.Request().SetBody(compressedBody) |
| 1292 | c.Request().Header.SetContentLength(len(compressedBody)) |
| 1293 | d := new(Demo) |
| 1294 | require.NoError(t, c.Bind().Body(d)) |
| 1295 | require.Equal(t, "john", d.Name) |
| 1296 | c.Request().Header.Del(fasthttp.HeaderContentEncoding) |
| 1297 | } |
| 1298 | |
| 1299 | t.Run("Gzip", func(t *testing.T) { |
| 1300 | t.Parallel() |
| 1301 | compressedBody := fasthttp.AppendGzipBytes(nil, reqBody) |
| 1302 | require.NotEqual(t, reqBody, compressedBody) |
| 1303 | testCompressedBody(t, compressedBody, "gzip") |
| 1304 | }) |
| 1305 | |
| 1306 | t.Run("Deflate", func(t *testing.T) { |
| 1307 | t.Parallel() |
| 1308 | compressedBody := fasthttp.AppendDeflateBytes(nil, reqBody) |
| 1309 | require.NotEqual(t, reqBody, compressedBody) |
| 1310 | testCompressedBody(t, compressedBody, "deflate") |
| 1311 | }) |
| 1312 | |
| 1313 | t.Run("Brotli", func(t *testing.T) { |
| 1314 | t.Parallel() |
| 1315 | compressedBody := fasthttp.AppendBrotliBytes(nil, reqBody) |
| 1316 | require.NotEqual(t, reqBody, compressedBody) |
| 1317 | testCompressedBody(t, compressedBody, "br") |
| 1318 | }) |
| 1319 | |
| 1320 | t.Run("Zstd", func(t *testing.T) { |
| 1321 | t.Parallel() |
| 1322 | compressedBody := fasthttp.AppendZstdBytes(nil, reqBody) |
| 1323 | require.NotEqual(t, reqBody, compressedBody) |
| 1324 | testCompressedBody(t, compressedBody, "zstd") |
| 1325 | }) |
| 1326 | |
| 1327 | testDecodeParser := func(t *testing.T, contentType string, body []byte) { |