@@ -1372,6 +1372,100 @@ func Benchmark_Ctx_Fresh_WithNoCache(b *testing.B) {
1372
1372
}
1373
1373
}
1374
1374
1375
+ // go test -run Test_Ctx_Parsers -v
1376
+ func Test_Ctx_Parsers (t * testing.T ) {
1377
+ t .Parallel ()
1378
+ // setup
1379
+ app := New ()
1380
+
1381
+ type TestStruct struct {
1382
+ Name string
1383
+ Class int
1384
+ NameWithDefault string `json:"name2" xml:"Name2" form:"name2" cookie:"name2" query:"name2" params:"name2" reqHeader:"name2"`
1385
+ ClassWithDefault int `json:"class2" xml:"Class2" form:"class2" cookie:"class2" query:"class2" params:"class2" reqHeader:"class2"`
1386
+ }
1387
+
1388
+ withValues := func (t * testing.T , actionFn func (c * Ctx , testStruct * TestStruct ) error ) {
1389
+ t .Helper ()
1390
+ c := app .AcquireCtx (& fasthttp.RequestCtx {})
1391
+ defer app .ReleaseCtx (c )
1392
+ testStruct := new (TestStruct )
1393
+
1394
+ utils .AssertEqual (t , nil , actionFn (c , testStruct ))
1395
+ utils .AssertEqual (t , "foo" , testStruct .Name )
1396
+ utils .AssertEqual (t , 111 , testStruct .Class )
1397
+ utils .AssertEqual (t , "bar" , testStruct .NameWithDefault )
1398
+ utils .AssertEqual (t , 222 , testStruct .ClassWithDefault )
1399
+ }
1400
+
1401
+ t .Run ("BodyParser:xml" , func (t * testing.T ) {
1402
+ t .Parallel ()
1403
+ withValues (t , func (c * Ctx , testStruct * TestStruct ) error {
1404
+ c .Request ().Header .SetContentType (MIMEApplicationXML )
1405
+ c .Request ().SetBody ([]byte (`<TestStruct><Name>foo</Name><Class>111</Class><Name2>bar</Name2><Class2>222</Class2></TestStruct>` ))
1406
+ return c .BodyParser (testStruct )
1407
+ })
1408
+ })
1409
+ t .Run ("BodyParser:form" , func (t * testing.T ) {
1410
+ t .Parallel ()
1411
+ withValues (t , func (c * Ctx , testStruct * TestStruct ) error {
1412
+ c .Request ().Header .SetContentType (MIMEApplicationForm )
1413
+ c .Request ().SetBody ([]byte (`name=foo&class=111&name2=bar&class2=222` ))
1414
+ return c .BodyParser (testStruct )
1415
+ })
1416
+ })
1417
+ t .Run ("BodyParser:json" , func (t * testing.T ) {
1418
+ t .Parallel ()
1419
+ withValues (t , func (c * Ctx , testStruct * TestStruct ) error {
1420
+ c .Request ().Header .SetContentType (MIMEApplicationJSON )
1421
+ c .Request ().SetBody ([]byte (`{"name":"foo","class":111,"name2":"bar","class2":222}` ))
1422
+ return c .BodyParser (testStruct )
1423
+ })
1424
+ })
1425
+ t .Run ("BodyParser:multiform" , func (t * testing.T ) {
1426
+ t .Parallel ()
1427
+ withValues (t , func (c * Ctx , testStruct * TestStruct ) error {
1428
+ body := []byte ("--b\r \n Content-Disposition: form-data; name=\" name\" \r \n \r \n foo\r \n --b\r \n Content-Disposition: form-data; name=\" class\" \r \n \r \n 111\r \n --b\r \n Content-Disposition: form-data; name=\" name2\" \r \n \r \n bar\r \n --b\r \n Content-Disposition: form-data; name=\" class2\" \r \n \r \n 222\r \n --b--" )
1429
+ c .Request ().SetBody (body )
1430
+ c .Request ().Header .SetContentType (MIMEMultipartForm + `;boundary="b"` )
1431
+ c .Request ().Header .SetContentLength (len (body ))
1432
+ return c .BodyParser (testStruct )
1433
+ })
1434
+ })
1435
+ t .Run ("CookieParser" , func (t * testing.T ) {
1436
+ t .Parallel ()
1437
+ withValues (t , func (c * Ctx , testStruct * TestStruct ) error {
1438
+ c .Request ().Header .Set ("Cookie" , "name=foo;name2=bar;class=111;class2=222" )
1439
+ return c .CookieParser (testStruct )
1440
+ })
1441
+ })
1442
+ t .Run ("QueryParser" , func (t * testing.T ) {
1443
+ t .Parallel ()
1444
+ withValues (t , func (c * Ctx , testStruct * TestStruct ) error {
1445
+ c .Request ().URI ().SetQueryString ("name=foo&name2=bar&class=111&class2=222" )
1446
+ return c .QueryParser (testStruct )
1447
+ })
1448
+ })
1449
+ t .Run ("ParamsParser" , func (t * testing.T ) {
1450
+ t .Parallel ()
1451
+ withValues (t , func (c * Ctx , testStruct * TestStruct ) error {
1452
+ c .route = & Route {Params : []string {"name" , "name2" , "class" , "class2" }}
1453
+ c .values = [30 ]string {"foo" , "bar" , "111" , "222" }
1454
+ return c .ParamsParser (testStruct )
1455
+ })
1456
+ })
1457
+ t .Run ("ReqHeaderParser" , func (t * testing.T ) {
1458
+ t .Parallel ()
1459
+ withValues (t , func (c * Ctx , testStruct * TestStruct ) error {
1460
+ c .Request ().Header .Add ("name" , "foo" )
1461
+ c .Request ().Header .Add ("name2" , "bar" )
1462
+ c .Request ().Header .Add ("class" , "111" )
1463
+ c .Request ().Header .Add ("class2" , "222" )
1464
+ return c .ReqHeaderParser (testStruct )
1465
+ })
1466
+ })
1467
+ }
1468
+
1375
1469
// go test -run Test_Ctx_Get
1376
1470
func Test_Ctx_Get (t * testing.T ) {
1377
1471
t .Parallel ()
0 commit comments