go test -run Test_Bind_RepeatParserWithSameStruct -v
(t *testing.T)
| 2571 | |
| 2572 | // go test -run Test_Bind_RepeatParserWithSameStruct -v |
| 2573 | func Test_Bind_RepeatParserWithSameStruct(t *testing.T) { |
| 2574 | t.Parallel() |
| 2575 | app := New(Config{ |
| 2576 | CBOREncoder: cbor.Marshal, |
| 2577 | CBORDecoder: cbor.Unmarshal, |
| 2578 | }) |
| 2579 | c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 2580 | defer app.ReleaseCtx(c) |
| 2581 | |
| 2582 | type Request struct { |
| 2583 | QueryParam string `query:"query_param"` |
| 2584 | HeaderParam string `header:"header_param"` |
| 2585 | BodyParam string `json:"body_param" xml:"body_param" form:"body_param"` |
| 2586 | } |
| 2587 | |
| 2588 | r := new(Request) |
| 2589 | |
| 2590 | c.Request().URI().SetQueryString("query_param=query_param") |
| 2591 | require.NoError(t, c.Bind().Query(r)) |
| 2592 | require.Equal(t, "query_param", r.QueryParam) |
| 2593 | |
| 2594 | c.Request().Header.Add("header_param", "header_param") |
| 2595 | require.NoError(t, c.Bind().Header(r)) |
| 2596 | require.Equal(t, "header_param", r.HeaderParam) |
| 2597 | |
| 2598 | var gzipJSON bytes.Buffer |
| 2599 | w := gzip.NewWriter(&gzipJSON) |
| 2600 | _, err := w.Write([]byte(`{"body_param":"body_param"}`)) |
| 2601 | require.NoError(t, err) |
| 2602 | err = w.Close() |
| 2603 | require.NoError(t, err) |
| 2604 | c.Request().Header.SetContentType(MIMEApplicationJSON) |
| 2605 | c.Request().Header.Set(HeaderContentEncoding, "gzip") |
| 2606 | c.Request().SetBody(gzipJSON.Bytes()) |
| 2607 | c.Request().Header.SetContentLength(len(gzipJSON.Bytes())) |
| 2608 | require.NoError(t, c.Bind().Body(r)) |
| 2609 | require.Equal(t, "body_param", r.BodyParam) |
| 2610 | c.Request().Header.Del(HeaderContentEncoding) |
| 2611 | |
| 2612 | testDecodeParser := func(contentType, body string) { |
| 2613 | c.Request().Header.SetContentType(contentType) |
| 2614 | c.Request().SetBody([]byte(body)) |
| 2615 | c.Request().Header.SetContentLength(len(body)) |
| 2616 | require.NoError(t, c.Bind().Body(r)) |
| 2617 | require.Equal(t, "body_param", r.BodyParam) |
| 2618 | } |
| 2619 | |
| 2620 | cb, err := cbor.Marshal(&Request{BodyParam: "body_param"}) |
| 2621 | require.NoError(t, err, "Failed to marshal CBOR data") |
| 2622 | |
| 2623 | testDecodeParser(MIMEApplicationJSON, `{"body_param":"body_param"}`) |
| 2624 | testDecodeParser(MIMEApplicationXML, `<Demo><body_param>body_param</body_param></Demo>`) |
| 2625 | testDecodeParser(MIMEApplicationCBOR, string(cb)) |
| 2626 | testDecodeParser(MIMEApplicationForm, "body_param=body_param") |
| 2627 | testDecodeParser(MIMEMultipartForm+`;boundary="b"`, "--b\r\nContent-Disposition: form-data; name=\"body_param\"\r\n\r\nbody_param\r\n--b--") |
| 2628 | } |
| 2629 | |
| 2630 | type RequestConfig struct { |